class handling all request matchers
See also
pattern classes for HTTP method MethodPattern, headers HeadersPattern, body BodyPattern, and URI/URL UriPattern
Methods
Method new()
Create a new RequestPattern
object
Usage
RequestPattern$new(
method,
uri = NULL,
uri_regex = NULL,
query = NULL,
body = NULL,
headers = NULL,
basic_auth = NULL
)
Arguments
method
the HTTP method (any, head, options, get, post, put, patch, trace, or delete). "any" matches any HTTP method. required.
uri
(character) request URI. required or uri_regex
uri_regex
(character) request URI as regex. required or uri
query
(list) query parameters, optional
body
(list) body request, optional
headers
(list) headers, optional
basic_auth
(list) vector of length 2 (username, passwdord), optional
Method matches()
does a request signature match the selected matchers?
Arguments
request_signature
a RequestSignature object
Examples
if (FALSE) { # \dontrun{
(x <- RequestPattern$new(method = "get", uri = "httpbin.org/get"))
x$body_pattern
x$headers_pattern
x$method_pattern
x$uri_pattern
x$to_s()
# make a request signature
rs <- RequestSignature$new(method = "get", uri = "http://httpbin.org/get")
# check if it matches
x$matches(rs)
# regex uri
(x <- RequestPattern$new(method = "get", uri_regex = ".+ossref.org"))
x$uri_pattern
x$uri_pattern$to_s()
x$to_s()
# uri with query parameters
(x <- RequestPattern$new(
method = "get", uri = "https://httpbin.org/get",
query = list(foo = "bar")
))
x$to_s()
## query params included in url, not separately
(x <- RequestPattern$new(
method = "get", uri = "https://httpbin.org/get?stuff=things"
))
x$to_s()
x$query_params
# just headers (via setting method=any & uri_regex=.+)
headers <- list(
"User-Agent" = "Apple",
"Accept-Encoding" = "gzip, deflate",
"Accept" = "application/json, text/xml, application/xml, */*"
)
x <- RequestPattern$new(
method = "any",
uri_regex = ".+",
headers = headers
)
x$to_s()
rs <- RequestSignature$new(
method = "any", uri = "http://foo.bar",
options = list(headers = headers)
)
rs
x$matches(rs)
# body
x <- RequestPattern$new(
method = "post", uri = "httpbin.org/post",
body = list(y = crul::upload(system.file("CITATION")))
)
x$to_s()
rs <- RequestSignature$new(
method = "post", uri = "http://httpbin.org/post",
options = list(
body = list(y = crul::upload(system.file("CITATION")))
)
)
rs
x$matches(rs)
# basic auth
x <- RequestPattern$new(
method = "post",
uri = "httpbin.org/post",
basic_auth = c("user", "pass")
)
x
x$headers_pattern$to_s()
x$to_s()
rs <- RequestSignature$new(
method = "post", uri = "http://httpbin.org/post",
options = list(headers = prep_auth("user:pass"))
)
rs
x$matches(rs) # TRUE
rs <- RequestSignature$new(
method = "post", uri = "http://httpbin.org/post",
options = list(headers = prep_auth("user:longpassword"))
)
x$matches(rs) # FALSE
} # }