oesmith / puffing-billy

A rewriting web proxy for testing interactions between your browser and external sites. Works with ruby + rspec.
MIT License
659 stars 170 forks source link

PB ignoring localhost requests #310

Closed pjaneiro closed 3 years ago

pjaneiro commented 3 years ago

I've tried using the selenium_chrome_billy and selenium_chrome_headless_billy, as well as appending options.add_argument("proxy-server=#{Billy.proxy.host}:#{Billy.proxy.port}") to my existing Selenium/WebDriver/Chrome driver.

I've messed quite a lot with the configurations, but currently they look like:

Billy.configure do |c|
  c.record_stub_requests = true
  c.non_successful_error_level = :error
  c.record_requests = true
end

My stub is as simple as

proxy.stub(%r{http://localhost:5000/.auth/(.*)}, method: 'all').and_return({
  code: 401
})

However, anytime I run my test, the requests to this endpoint are still being made directly to the server, and the response is unaffected. I tried the sample snippet in the README.md that prints all requests at the end of the run, and requests to localhost are not being printed, nor is there any mention in the logs that the stub was created/called. I tried stubbing other requests in the exact same way, and everything works as expected, only localhost requests seem to be ignored.

Is this expected behavior? Am I missing something? Is there any other piece of information that might be relevant here?

ronwsmith commented 3 years ago

Most (maybe all) drivers bypass proxies for localhost. I know I ran into this issue with PhantomJS in the past, but don't try to proxy localhost anymore so I'm not aware of any current workarounds. The PhantomJS workaround was to not use that driver for tests needing to proxy localhost.

I'd do some research on ways to force your driver to use a proxy which would then route through puffing-billy.

pjaneiro commented 3 years ago

It was indeed an issue with the driver. For a driver based on Chrome, the following made it work:

    options = Selenium::WebDriver::Chrome::Options.new
    options.add_argument('ignore-certificate-errors')
    options.add_argument("proxy-server=#{Billy.proxy.host}:#{Billy.proxy.port}")
    options.add_argument('proxy-bypass-list=<-loopback>')

Closing the issue