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
using proxy server instead of normal HTTP server #1212
Open
mreinsch
wants to merge
23
commits into
teamcapybara:master
Choose a base branch
from
mreinsch:proxy-server
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
4144786
check correct version string for rspec composable
twalpole 05ffa1c
Fix a typo
bbatsov 2e995b8
Increase wait times for window_opened_by spec due to slow Travis CI
abotalov 8091a8d
Test should be tagged as :windows also
route 1e6663f
Update History.md
takashi 1272176
Use Rack::Lock to prevent concurrency when eager_load is false
twalpole b82ae1d
Update visit's documentation to indicate that it requires something t…
shepmaster 4e43981
Always convert visit's URL to a String before manipulation
shepmaster 3e48371
Update fixes in 2.4.2
twalpole 2b8e08c
Merge pull request #1380 from jnicklas/composable_version_test
twalpole 4756d1e
Update fixes in 2.4.2 [ci-skip]
twalpole 1d79355
tweak test timings for window API to improve reliability on travis
twalpole 4d19182
prepare for 2.4.2 release
twalpole d18047d
tagged 2.4.2
twalpole c47b5d3
Also check config.allow_concurrency to match Rails 4.2 behavior for R…
twalpole 58add00
tagged 2.4.3
twalpole 5dbb480
Remove insertion of Rack::Lock middleware in favor of a note in the R…
twalpole f9077ed
Replace hardcoded `localhost` in spec helper.
phillbaker eabbd85
Update History
twalpole 120daaa
tagged 2.4.4
twalpole 052cc93
added proxy server implementation
mreinsch 9b94465
use TLSv1 insteadl of older SSL
mreinsch 7058350
don't print stacktraces on errors
mreinsch File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
|
@@ -133,4 +133,8 @@ def reset! | |
def needs_server? | ||
false | ||
end | ||
|
||
def supports_proxy_protocol? | ||
false | ||
end | ||
end |
This file contains 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,112 @@ | ||
require 'webrick' | ||
require 'webrick/httpproxy' | ||
require 'webrick/https' | ||
require 'rack/test' | ||
|
||
module Capybara | ||
class ProxyServer < WEBrick::HTTPProxyServer | ||
|
||
class SSLHandler < WEBrick::HTTPServer | ||
def initialize(app, config) | ||
super(config.merge(:Port => 443, :DoNotListen => true)) | ||
@rack_handler = Rack::Handler::WEBrick.new(self, app) | ||
end | ||
|
||
def service(req, res) | ||
if (host_handler = Capybara::ProxyServer.host_mapping[req.host]) | ||
Rack::Handler::WEBrick.new(self, host_handler).service(req, res) | ||
else | ||
@rack_handler.service(req, res) | ||
end | ||
rescue => err | ||
$stderr.puts err | ||
end | ||
end | ||
|
||
DEFAULT_404_HANDLER = Proc.new {|env| [404, {}, []] } | ||
|
||
class << self | ||
attr_accessor :host_mapping | ||
|
||
## | ||
# | ||
# Configure Capybara ProxyServer to suit your needs. | ||
# | ||
# Capybara::ProxyServer.configure do |config| | ||
# config.route "i.kissmetrics.com" => Capybara::ProxyServer::DEFAULT_404_HANDLER | ||
# end | ||
# | ||
def configure | ||
yield self | ||
end | ||
|
||
## | ||
# | ||
# Route a request to a specific host to a separate Rack application. | ||
# This allows you to mock external services without actually doing external requests. | ||
# | ||
# You can use the Capybara::ProxyServer::DEFAULT_404_HANDLER to simply return a 404 error code, or create your own rack applications. | ||
# | ||
def route(host_map) | ||
@host_mapping ||= {} | ||
@host_mapping.merge!(host_map) | ||
end | ||
|
||
def run(app, config) | ||
server = new(app, config.merge(:OutputBufferSize => 5)) | ||
server.start | ||
server | ||
end | ||
end | ||
|
||
def initialize(app, config) | ||
super(config, WEBrick::Config::HTTP) | ||
@rack_handler = Rack::Handler::WEBrick.new(self, app) | ||
@ssl_context = generate_ssl_context | ||
@ssl_server = SSLHandler.new(app, config) | ||
end | ||
|
||
def service(req, res) | ||
if req.request_method == "CONNECT" | ||
host, port = req.unparsed_uri.split(":", 2) | ||
if port == '443' | ||
ua = Thread.current[:WEBrickSocket] | ||
res.status = WEBrick::HTTPStatus::RC_OK | ||
res.send_response(ua) | ||
req.parse(NullReader) rescue nil | ||
ssl = OpenSSL::SSL::SSLSocket.new(ua, @ssl_context) | ||
ssl.sync_close = true | ||
ssl.accept | ||
@ssl_server.run(ssl) | ||
else | ||
res.status = 501 | ||
end | ||
elsif (host_handler = Capybara::ProxyServer.host_mapping[req.host]) | ||
Rack::Handler::WEBrick.new(self, host_handler).service(req, res) | ||
else | ||
@rack_handler.service(req, res) | ||
end | ||
rescue => err | ||
$stderr.puts err | ||
end | ||
|
||
private | ||
|
||
def generate_ssl_context | ||
OpenSSL::SSL::SSLContext.new(:TLSv1_server).tap do |ssl_context| | ||
self_signed_cert, self_signed_cert_key = | ||
WEBrick::Utils.create_self_signed_cert(1024, [ [ "CN", "localhost" ] ], "Generated by Ruby/OpenSSL") | ||
ssl_context.cert = self_signed_cert | ||
ssl_context.key = self_signed_cert_key | ||
end | ||
end | ||
end | ||
end | ||
|
||
Capybara::ProxyServer.configure do |config| | ||
config.route "www.google-analytics.com" => Capybara::ProxyServer::DEFAULT_404_HANDLER, | ||
"ssl.google-analytics.com" => Capybara::ProxyServer::DEFAULT_404_HANDLER, | ||
"fonts.googleapis.com" => Capybara::ProxyServer::DEFAULT_404_HANDLER, | ||
"connect.facebook.net" => Capybara::ProxyServer::DEFAULT_404_HANDLER, | ||
"platform.twitter.com" => Capybara::ProxyServer::DEFAULT_404_HANDLER | ||
end |
This file contains 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 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 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 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 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 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
selenium supports more than just Firefox - how will this work if the user is using selenium with chrome, ie, etc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good question, I suppose all browsers will somehow support setting up a proxy. If they don't, we can have the supports_proxy_protocol return false for now.