The mock contexts in httptest can raise errors or messages when requests
are made, and those (error) messages have three
elements, separated by space: (1) the request
method (e.g. "GET"); (2) the request URL; and
(3) the request body, if present.
These verb-expectation functions look for this message shape. expect_PUT,
for instance, looks for a request message that starts with "PUT".
This means that expect_verb functions won't work outside of mock context,
as no error would be raised while making a request. Thus, any expect_verb
function should be wrapped inside a mocking function like
without_internet(), as shown in the examples.
expect_GET(object, url = "", ...)
expect_POST(object, url = "", ...)
expect_PATCH(object, url = "", ...)
expect_PUT(object, url = "", ...)
expect_DELETE(object, url = "", ...)
expect_no_request(object, ...)Code to execute that may cause an HTTP request
character: the URL you expect a request to be made to. Default is an empty string, meaning that you can just assert that a request is made with a certain method without asserting anything further.
character segments of a request payload you expect to be included
in the request body, to be joined together by paste0. You may also pass
any of the following named logical arguments, which will be passed to
base::grepl():
fixed: Should matching take the pattern as is or treat it as a regular
expression. Default: TRUE, and note that this default is the opposite of
the default in grepl. (The rest of the arguments follow its defaults.)
ignore.case: Should matching be done case insensitively? Default:
FALSE, meaning matches are case sensitive.
perl: Should Perl-compatible regular expressions be used? Default: FALSE
useBytes: Should matching be done byte-by-byte rather than
character-by-character? Default: FALSE
A testthat 'expectation'.
library(httr)
# without_internet provides required mock context for expectations
without_internet({
expect_GET(
GET("http://httpbin.org/get"),
"http://httpbin.org/get"
)
expect_GET(GET("http://httpbin.org/get"),
"http://httpbin.org/[a-z]+",
fixed = FALSE
) # For regular expression matching
expect_PUT(
PUT("http://httpbin.org/put", body = '{"a":1}'),
"http://httpbin.org/put",
'{"a":1}'
)
expect_PUT(PUT("http://httpbin.org/put", body = '{"a":1}'))
expect_no_request(rnorm(5))
})
#> [1] -0.005571287 0.621552721 1.148411606 -1.821817661 -0.247325302