The problem:
We want to run headless browser tests (in this case, using Poltergeist & PhantomJS) that call our Ruby code which then makes requests out to Stripe's API.
However, we don't want those API requests happening every time. In our non-headless-browser tests (ie: using vanilla Capybara), we have VCR recording those requests, so subsequent runs of the specs are kept quick. VCR is only recording in the RSpec process, though, not the Poltergeist server process - so in our headless-browser tests, Stripe calls don't get recorded by VCR (different process, different context).
What I'm thinking is:
- A HTTP/S proxy running in a separate thread/process, which is also where VCR operates.
- Requests made in the RSpec process are proxied through to the VCR process.
- Poltergeist/PhantomJS requests are proxied through to the VCR process.
- Thread-safe commands to track which VCR cassette is loaded.
- If no cassette is loaded/recording, the proxy sends requests straight through normally.
It'd be really neat if VCR's logic around capturing HTTP/S requests could be pulled out into a separate gem (and then VCR itself just focuses on how to handle those requests). The new gem could then be used to capture RSpec/Capybara requests.
This is a big undertaking though… thus far, I've not thought of any half-decent imperfect approaches. Can't mock requests with Webmock in RSpec, because the Poltergeist process won't be aware of those for the same reason it's not aware of the VCR setup.
Perhaps we can use Puffing Billy to mock those Stripe requests (and have VCR disabled)? I'm really not a fan of mocking the requests, but no inspiration for a better approach as yet, short of building out the full solution described above (and I'm not even sure if that would work!).
I've got this working with Puffing Billy, I can share the spec privately, leave me a note if interested :-)