Test remote javascript APIs with Capybara and puffing-billy
Testing remote APIs is easy in Ruby. Libraries like webmock, vcr and artifice give you all the tools you need to ensure that you’re sending the right requests to the right remote endpoints.
However, it becomes a lot more difficult when it’s your Javascript code that’s making the requests. Using requests specs with Capybara gives you loads of tools to control the browser, but it doesn’t let you stub or mock responses to requests that originate from within the browser.
This is where puffing-billy saves the day. It’s a request stubbing library for browsers. It spawns a HTTP(S) proxy server that it uses to intercept requests from your browser. Using simple webmock-alike syntax, you can configure the proxy to send fake responses to requests for specific URLs.
For example, the following is a simple piece of javascript code that fetches the temperature for Bath, UK from the openweathermap.org service.
And this is a request spec for that snippet. Note how it easily fakes JSONP data and error responses.
puffing-billy supports HTTP and HTTPS requests and all common HTTP verbs. Go check it out on GitHub now!