In this context, HTTP verb functions raise a 'message' so that test code can assert that the requests are made. As in without_internet(), the message raised has a well-defined shape, made of three elements, separated by space: (1) the request method (e.g. "GET", or for downloading, "DOWNLOAD"); (2) the request URL; and (3) the request body, if present. The verb-expectation functions, such as expect_GET and expect_POST, look for this shape.





Code to run inside the fake context


The result of expr


Unlike without_internet, the HTTP functions do not error and halt execution, instead returning a response-class object so that code calling the HTTP functions can proceed with its response handling logic and itself be tested. The response it returns echoes back most of the request itself, similar to how some endpoints on do.

In the interest of standardizing naming conventions, with_fake_http() is the preferred name for this context; with_fake_HTTP() is being deprecated.


with_fake_http({ expect_GET(req1 <- httr::GET(""), "") req1$url expect_POST(req2 <- httr::POST("", body='{"a":1}'), "") httr::content(req2) })
#> $a #> [1] 1 #>