expect_request_header {httptest2}R Documentation

Test that an HTTP request is made with a header

Description

This 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(), or NULL to 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, pattern is a string to be matched as is. Overrides all conflicting arguments.

ignore.case

if FALSE, the pattern matching is case sensitive and if TRUE, case is ignored during matching.

perl

logical. Should Perl-compatible regexps be used?

useBytes

logical. If TRUE the matching is done byte-by-byte rather than character-by-character. See ‘Details’.

Value

The value of expr if there are no expectation failures

Examples



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 = ""
)


[Package httptest2 version 1.1.0 Index]