Skip to content

Commit f2dcb6c

Browse files
committed
Add proxy option to Twingly HTTP Client
In case we are being blocked by a domain we need to be able to use a proxy.
1 parent ad52c2b commit f2dcb6c

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

lib/twingly/http.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ class Client # rubocop:disable Metrics/ClassLength
5050
attr_accessor :logger
5151
attr_accessor :retryable_exceptions
5252

53-
def initialize(base_user_agent:, logger: default_logger, user_agent: nil)
53+
def initialize(base_user_agent:, logger: default_logger, user_agent: nil, proxy: nil)
5454
@base_user_agent = base_user_agent
5555
@logger = logger
5656
@user_agent = user_agent
57+
@proxy = proxy
5758

5859
initialize_defaults
5960
end
@@ -212,6 +213,7 @@ def create_http_client # rubocop:disable Metrics/MethodLength
212213
max_size_bytes: @max_response_body_size_bytes
213214
faraday.adapter Faraday.default_adapter
214215
faraday.headers[:user_agent] = user_agent
216+
faraday.proxy = @proxy if @proxy
215217
end
216218
end
217219

spec/lib/twingly/http_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,40 @@ class CustomError < StandardError; end
688688
end
689689
end
690690
end
691+
692+
fdescribe "proxy" do
693+
let(:test_url) { "http://example.com" }
694+
695+
context "when a proxy is set" do
696+
let(:proxy_url) { "http://localhost:1080" }
697+
let(:proxy_client) do
698+
described_class.new(
699+
base_user_agent: base_user_agent,
700+
logger: logger,
701+
user_agent: user_agent,
702+
proxy: proxy_url
703+
)
704+
end
705+
706+
before(:all) do
707+
let(:toxiproxy) { Toxiproxy.new(host: "localhost", port: 8474) }
708+
let(:proxy) { toxiproxy.create("proxy", listen: "localhost:1080", upstream: "https://example.com") }
709+
end
710+
711+
after(:all) do
712+
proxy.delete
713+
toxiproxy.close
714+
end
715+
716+
it "sets the proxy URL correctly in the Twingly HTTP client" do
717+
allow(proxy_client).to receive(proxy).and_return(proxy_url)
718+
response = proxy_client.get("https://example.com")
719+
720+
expect(proxy_client.proxy).to eq(proxy_url)
721+
expect(response.status).to eq(200)
722+
end
723+
end
724+
end
691725
end
692726

693727
describe "#put", vcr: Fixture.put_httpbin_org do

0 commit comments

Comments
 (0)