15 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).

15.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 or httptest2, 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.

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

Different possibilities: