StubbedRequest | R Documentation |
stubbed request class underlying stub_request()
method
(xx) xx
uri
(xx) xx
uri_regex
(xx) xx
regex
a logical
uri_parts
(xx) xx
host
(xx) xx
query
(xx) xx
body
(xx) xx
basic_auth
(xx) xx
request_headers
(xx) xx
response_headers
(xx) xx
responses_sequences
(xx) xx
status_code
(xx) xx
counter
a StubCounter object
new()
Create a new StubbedRequest
object
StubbedRequest$new(method, uri = NULL, uri_regex = NULL)
method
the HTTP method (any, head, get, post, put, patch, or delete). "any" matches any HTTP method. required.
uri
(character) request URI. either this or uri_regex
required. webmockr can match uri's without the "http" scheme,
but does not match if the scheme is "https". required, unless
uri_regex
given. See UriPattern for more.
uri_regex
(character) request URI as regex. either this or uri
required
A new StubbedRequest
object
print()
print method for the StubbedRequest
class
StubbedRequest$print(x, ...)
x
self
...
ignored
with()
Set expectations for what's given in HTTP request
StubbedRequest$with( query = NULL, body = NULL, headers = NULL, basic_auth = NULL )
query
(list) request query params, as a named list. optional
body
(list) request body, as a named list. optional
headers
(list) request headers as a named list. optional.
basic_auth
(character) basic authentication. optional.
nothing returned; sets only
to_return()
Set expectations for what's returned in HTTP response
StubbedRequest$to_return(status, body, headers)
status
(numeric) an HTTP status code
body
(list) response body, one of: character
, json
,
list
, raw
, numeric
, NULL
, FALSE
, or a file connection
(other connection types not supported)
headers
(list) named list, response headers. optional.
nothing returned; sets whats to be returned
to_timeout()
Response should time out
StubbedRequest$to_timeout()
nothing returned
to_raise()
Response should raise an exception x
StubbedRequest$to_raise(x)
x
(character) an exception message
nothing returned
to_s()
Response as a character string
StubbedRequest$to_s()
(character) the response as a string
reset()
Reset the counter for the stub
StubbedRequest$reset()
nothing returned; resets stub counter to no requests
clone()
The objects of this class are cloneable with this method.
StubbedRequest$clone(deep = FALSE)
deep
Whether to make a deep clone.
stub_request()
## Not run:
x <- StubbedRequest$new(method = "get", uri = "api.crossref.org")
x$method
x$uri
x$with(headers = list("User-Agent" = "R", apple = "good"))
x$to_return(status = 200, body = "foobar", headers = list(a = 5))
x
x$to_s()
# query
x <- StubbedRequest$new(method = "get", uri = "httpbin.org")
x$with(query = list(a = 5))
x
x$to_s()
## including
x <- StubbedRequest$new(method = "get", uri = "httpbin.org")
x$with(query = including(list(a = 5)))
x
x$to_s()
x$with(query = including(list(a = 5, b = 7)))
x$to_s()
## excluding
x <- StubbedRequest$new(method = "get", uri = "httpbin.org")
x$with(query = excluding(list(a = 5)))
x
x$to_s()
# many to_return's
x <- StubbedRequest$new(method = "get", uri = "httpbin.org")
x$to_return(status = 200, body = "foobar", headers = list(a = 5))
x$to_return(status = 200, body = "bears", headers = list(b = 6))
x
x$to_s()
# raw body
x <- StubbedRequest$new(method = "get", uri = "api.crossref.org")
x$to_return(status = 200, body = raw(0), headers = list(a = 5))
x$to_s()
x
x <- StubbedRequest$new(method = "get", uri = "api.crossref.org")
x$to_return(
status = 200, body = charToRaw("foo bar"),
headers = list(a = 5)
)
x$to_s()
x
# basic auth
x <- StubbedRequest$new(method = "get", uri = "api.crossref.org")
x$with(basic_auth = c("foo", "bar"))
x$to_s()
x
# file path
x <- StubbedRequest$new(method = "get", uri = "api.crossref.org")
f <- tempfile()
x$to_return(status = 200, body = file(f), headers = list(a = 5))
x
x$to_s()
unlink(f)
# to_file(): file path and payload to go into the file
# payload written to file during mocked response creation
x <- StubbedRequest$new(method = "get", uri = "api.crossref.org")
f <- tempfile()
x$to_return(
status = 200, body = mock_file(f, "{\"foo\": \"bar\"}"),
headers = list(a = 5)
)
x
x$to_s()
unlink(f)
# uri_regex
(x <- StubbedRequest$new(method = "get", uri_regex = ".+ossref.org"))
x$method
x$uri_regex
x$to_s()
# to timeout
(x <- StubbedRequest$new(method = "get", uri_regex = ".+ossref.org"))
x$to_s()
x$to_timeout()
x$to_s()
x
# to raise
library(fauxpas)
(x <- StubbedRequest$new(method = "get", uri_regex = ".+ossref.org"))
x$to_s()
x$to_raise(HTTPBadGateway)
x$to_s()
x
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.