oesmith / puffing-billy

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

Use Puffing-Billy to stub Rails API requests #289

Closed patrickclery closed 2 years ago

patrickclery commented 4 years ago

Is it possible to use puffing-billy to stub requests to the local Rails API?

For example, I have a React app that I'd like to test with Capybara. It interacts with the API via fetch like so:

const uri = `/api/v1/todos/`
const response = await fetch(uri)

Adding a stub for proxy.stub("/api/v1/todos" ... doesn't seem to work. There used to be an option for Billy.config.ignore_localhost = true but that seems to be removed, and I'm not sure if this is even what I'm trying to achieve.

My goal is to test my React front-end with Capybara/RSpec, without having to structure my project to use Jest/Enzyme/etc to test. I've already been able to do a Capybara test, but I can't stub the API requests (it uses the real test-server API)

Any suggestions welcomed :) Thanks

ronwsmith commented 4 years ago

How are your Rails, React, and test suites set up, repo-wise? Can you share your RSpec and Capyabra configurations and a sample spec?

seanabrahams commented 3 years ago

If you're using Chromedriver it may be due to it not using the Puffing Billy proxy for localhost requests. You'll need to configure Capybara's driver definition with --proxy-bypass-list=<-loopback>.

For example, here's the current definition within Puffing Billy (lib/billy/browsers/capybara.rb) updated with the argument:

::Capybara.register_driver :selenium_chrome_billy do |app|
  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>") # New argument here to ensure requests to localhost are sent to Puffing Billy's proxy

  ::Capybara::Selenium::Driver.new(
    app,
    browser: :chrome,
    options: options,
    clear_local_storage: true,
    clear_session_storage: true
  )
end

There's likely an opportunity here for updating related configuration options (whitelist). I'm not yet familiar enough with the codebase to propose something specific but will do so if the opportunity arises.

Judimax commented 3 years ago

Hello I also need help with a similar issue, we have an Angular app however none of the frontend backend interactions are being picked up by puffing billy so we can see the result of those responses in our capybara tests

diego-fin commented 2 years ago

@patrickclery were you able to make it work?

ronwsmith commented 2 years ago

Stale issue, closing. Feel free to reopen if this is still desired.