Test that an HTTP request is made with a header
Source:R/expect-request-header.R
expect_request_header.RdThis expectation checks that HTTP headers (and potentially header values) are present in a request. It works both in the mock HTTP contexts and on "live" HTTP requests.
Usage
expect_request_header(
expr,
...,
fixed = FALSE,
ignore.case = FALSE,
perl = FALSE,
useBytes = FALSE
)Arguments
- expr
Code to evaluate
- ...
Named headers to match. Values should either be a string (length-1 character), which will be passed to
testthat::expect_match(), orNULLto assert that the named header is not present in the request. To assert that a header is merely present in the request, without asserting anything about its contents, provide an empty string (""). Header names are always case-insensitive; header values will be matched using the following parameters:- fixed
logical. If
TRUE,patternis a string to be matched as is. Overrides all conflicting arguments.- ignore.case
logical. if
FALSE, the pattern matching is case sensitive and ifTRUE, case is ignored during matching.- perl
logical. Should Perl-compatible regexps be used?
- useBytes
logical. If
TRUEthe matching is done byte-by-byte rather than character-by-character. See ‘Details’.
Examples
if (FALSE) {
library(httr2)
expect_request_header(
request("http://httpbin.org") %>%
req_headers(Accept = "image/png") %>%
req_perform(),
accept = "image/png",
`x-fake-header` = NULL
)
expect_request_header(
request("http://httpbin.org") %>%
req_headers(Accept = "image/png") %>%
req_perform(),
accept = ""
)
}