-
Notifications
You must be signed in to change notification settings - Fork 1
Add proxy support to HTTP Client #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f2dcb6c
Add proxy option to Twingly HTTP Client
cequele 969325c
Add unit test for proxy
cequele 177316c
Add proxy server for testing
cequele 090ff67
Add shared examples for proxy configuration tests
cequele 7894f8d
Refactor proxy tests and remove unused methods
cequele a954011
Refactor test server to use TestServer struct
cequele File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "json" | ||
|
|
||
| run lambda { |env| | ||
| request_headers = env.select { |k, _v| k.start_with? "HTTP_" } | ||
|
|
||
| [200, { "content-type" => "application/json" }, [request_headers.to_json]] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "rack/proxy" | ||
|
|
||
| class TestProxy < Rack::Proxy | ||
| def rewrite_env(env) | ||
| env["HTTP_X_PROXIED_BY"] = "test-proxy" | ||
| env | ||
| end | ||
| end | ||
|
|
||
| run TestProxy.new |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module HttpHelpers | ||
| def with_real_http_connections | ||
| original_cassette = VCR.eject_cassette | ||
| VCR.turn_off! | ||
| WebMock.allow_net_connect! | ||
|
|
||
| yield | ||
| ensure | ||
| WebMock.disable_net_connect! | ||
| VCR.turn_on! | ||
| VCR.insert_cassette(original_cassette.name) if original_cassette | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "timeout" | ||
|
|
||
| module HttpTestServer | ||
| module_function | ||
|
|
||
| TestServer = Struct.new(:pid, :url) | ||
|
|
||
| def spawn(server_name, env: {}) # rubocop:disable Metrics/MethodLength | ||
| ip_address = PortProber.localhost | ||
| port = PortProber.random(ip_address) | ||
| url = "http://#{ip_address}:#{port}" | ||
| server = "spec/rack_servers/#{server_name}.ru" | ||
| command = "bundle exec rackup --quiet --port #{port} #{server}" | ||
|
|
||
| puts "starting HTTP test server: #{command}" | ||
| pid = fork do | ||
| $stdout.reopen File::NULL | ||
| $stderr.reopen File::NULL | ||
| exec env, command | ||
| end | ||
|
|
||
| Timeout.timeout(10.0) do | ||
| sleep 0.05 until started?(pid) && PortProber.port_open?(ip_address, port) | ||
| end | ||
|
|
||
| TestServer.new(pid, url) | ||
| end | ||
|
|
||
| def stop(pid) | ||
| Process.kill(:TERM, pid) | ||
| Process.wait(pid) | ||
| end | ||
|
|
||
| def started?(pid) | ||
| Process.getpgid(pid) | ||
| true | ||
| rescue Errno::ESRCH | ||
| false | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "socket" | ||
| require "timeout" | ||
|
|
||
| module PortProber | ||
| module_function | ||
|
|
||
| def random(host) | ||
| server = TCPServer.new(host, 0) | ||
| port = server.addr[1] | ||
|
|
||
| port | ||
| ensure | ||
| server&.close | ||
| end | ||
|
|
||
| def port_open?(ip_address, port) | ||
| Timeout.timeout(0.5) do | ||
| TCPSocket.new(ip_address, port).close | ||
| true | ||
| end | ||
| rescue StandardError | ||
| false | ||
| end | ||
|
|
||
| def localhost | ||
| info = Socket.getaddrinfo("localhost", | ||
| 80, | ||
| Socket::AF_INET, | ||
| Socket::SOCK_STREAM) | ||
|
|
||
| raise "unable to translate 'localhost' for TCP + IPv4" if info.empty? | ||
|
|
||
| info[0][3] | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.