headers matcher
Details
webmockr
normalises headers and treats all forms of same headers as equal:
i.e the following two sets of headers are equal:
list(Header1 = "value1", content_length = 123, X_CuStOm_hEAder = "foo")
and
list(header1 = "value1", "Content-Length" = 123, "x-cuSTOM-HeAder" = "foo")
Methods
Method matches()
Match a list of headers against that stored
Examples
(x <- HeadersPattern$new(pattern = list(a = 5)))
#> <HeadersPattern>
#> Public:
#> clone: function (deep = FALSE)
#> empty_headers: function (headers)
#> initialize: function (pattern)
#> matches: function (headers)
#> pattern: list
#> to_s: function ()
#> Private:
#> normalize_headers: function (x)
x$pattern
#> $a
#> [1] 5
#>
x$matches(list(a = 5))
#> [1] TRUE
# different cases
(x <- HeadersPattern$new(pattern = list(Header1 = "value1")))
#> <HeadersPattern>
#> Public:
#> clone: function (deep = FALSE)
#> empty_headers: function (headers)
#> initialize: function (pattern)
#> matches: function (headers)
#> pattern: list
#> to_s: function ()
#> Private:
#> normalize_headers: function (x)
x$pattern
#> $header1
#> [1] "value1"
#>
x$matches(list(header1 = "value1"))
#> [1] TRUE
x$matches(list(header1 = "value2"))
#> [1] FALSE
# different symbols
(x <- HeadersPattern$new(pattern = list(`Hello_World` = "yep")))
#> <HeadersPattern>
#> Public:
#> clone: function (deep = FALSE)
#> empty_headers: function (headers)
#> initialize: function (pattern)
#> matches: function (headers)
#> pattern: list
#> to_s: function ()
#> Private:
#> normalize_headers: function (x)
x$pattern
#> $`hello-world`
#> [1] "yep"
#>
x$matches(list(`hello-world` = "yep"))
#> [1] TRUE
x$matches(list(`hello-worlds` = "yep"))
#> [1] FALSE
headers <- list(
"User-Agent" = "Apple",
"Accept-Encoding" = "gzip, deflate",
"Accept" = "application/json, text/xml, application/xml, */*"
)
(x <- HeadersPattern$new(pattern = headers))
#> <HeadersPattern>
#> Public:
#> clone: function (deep = FALSE)
#> empty_headers: function (headers)
#> initialize: function (pattern)
#> matches: function (headers)
#> pattern: list
#> to_s: function ()
#> Private:
#> normalize_headers: function (x)
x$to_s()
#> [1] "user-agent=\"Apple\", accept-encoding=\"gzip, deflate\", accept=\"application/json, text/xml, application/xml, */*\""
x$pattern
#> $`user-agent`
#> [1] "Apple"
#>
#> $`accept-encoding`
#> [1] "gzip, deflate"
#>
#> $accept
#> [1] "application/json, text/xml, application/xml, */*"
#>
x$matches(headers)
#> [1] TRUE