Chapter 2 Introduction

2.1 What is webmockr?

webmockr is an R package to help you “mock” HTTP requests. What does mock mean? Mock refers to the fact that we’re faking the response. Here is how it works:

  • You “stub” a request. That is, you set rules for what HTTP request you’d like to match on
  • You also can set rules for what you’d like to respond with, if anything (if nothing, then we give you NULL)
  • Then you make HTTP requests, and those that match your stub will return what you requested be returned
  • While webmockr is in use, real HTTP interactions are not allowed
  • There is no recording interactions to disk at all, just mocked responses given as the user specifies in the R session

webmockr works with both the crul package and the httr package.

Read more about webmockr in Section 2.

2.2 What is vcr?

The short version is: vcr helps you stub HTTP requests so you don’t have to repeat HTTP requests.

The main use case is for unit tests of R packages.

vcr works with both the crul package and the httr package.

vcr works by hooking into webmockr. However, when webmockr finds a match, we then look for a recorded interaction on disk. If one is not found, we record the request and response. If one is found, we use that recorded interaction to construct a real response as the R client expects.

Read more about vcr in Section 3.

2.3 Why crul?

crul is just one of the HTTP clients in R. It’s the one that I maintain though, so was easiest to get started with adding mocking integration.

There is now integration with httr for both webmockr and vcr.

The major feature that httr has that crul does not have is OAuth support, but that’s not an important use case for me so is not a high priority for crul.

A major reason to use crul over httr is that it uses more of an object oriented interface. That is, you create objects that you can call methods on and retrieve variables/results from calls/etc. It’s a different approach than httr which focuses on passing things to functions.

2.4 Use cases

2.4.1 mocking use cases

  • one
  • two
  • three

2.4.2 caching use cases

  • one
  • two
  • three