jnicklas/capybara — pull request #1212 by mreinsch (2013-12-15 14:05:00+00:00)
Title: using proxy server instead of normal HTTP server

I'd like to start a discussion with this pull request. It's still a bit rough, please bear with me.

This introduces a proxy server implementation (based on WEBrick) and configures the browser to use that proxy server. Thus the proxy server now receives any requests made by the browser. The proxy server internally uses the rack test framework to invoke the app to test. This allows us to easily test apps which use multiple domains.

Right now the proxy server forwards all requests to the web app (similar to the RackTest adapter), but it'd be relatively easy to extend this to actually forward some requests to remote servers or use net mocking frameworks to mock requests.

Besides the selenium proxy driver included in the pull request, I also created one for Poltergeist:

```ruby
class Capybara::Poltergeist::ProxyDriver < Capybara::Poltergeist::Driver
def initialize(app, opts={})
phantomjs_options = opts.delete(:phantomjs_options) || []
phantomjs_options.push(
"--proxy=http://#{Capybara.server_host}:#{Capybara.server_port}",
"--proxy-type=http",
"--ignore-ssl-errors=true")
super(app, opts.merge(phantomjs_options: phantomjs_options))
end
def needs_server?
false
end
def needs_proxy?
true
end
end
```

Register those drivers like this:

```ruby
Capybara.register_driver :poltergeist_proxy do |app|
Capybara::Poltergeist::ProxyDriver.new(app)
end

Capybara.register_driver :selenium_proxy do |app|
Capybara::Selenium::ProxyDriver.new(app)
end
```

One limitation, we need to configure the proxy driver's host and port to make sure they are available, for instance:

```ruby
Capybara.configure do |config|
config.javascript_driver = :selenium_proxy
config.server_host = "127.0.0.1"
config.server_port = 23798
end
```

I'd love to hear your comments on how we can get this into capybara, we need this mainly to test our multi-domain app. Thanks.