14 Faking HTTP errors

With HTTP testing you can test the behavior of your package in case of an API error without having to actually trigger an API error. This is important for testing your package’s gracefulness (informative error message for the user) and robustness (if you e.g. use retrying in case of API errors).

14.1 How to test for API errors (e.g. 503)

Different possibilities:

  • Use webmockr as in our demo.
  • Edit a vcr cassette; be careful to skip this test when vcr is off with vcr::skip_if_vcr_is_off().
  • With httptest, edit a mock file as in our demo, or create it from scratch.
  • With webfakes, choose what to return, have a specific app for the test, see our demo.

14.2 How to test for sequence of responses (e.g. 503 then 200)

Different possibilities:

  • Use webmockr.
  • Edit a vcr cassette; be careful to skip this test when vcr is off with vcr::skip_if_vcr_is_off()
  • With httptest, this is not easy yet
  • With webfakes, follow the docs. Also have a specific app for the test as this is not the behavior you want in all your tests.