webmockr/ 0000755 0001762 0000144 00000000000 14377415642 012100 5 ustar ligges users webmockr/NAMESPACE 0000644 0001762 0000144 00000002542 14362302441 013304 0 ustar ligges users # Generated by roxygen2: do not edit by hand
S3method(print,mock_file)
S3method(print,webmockr_config)
export("%>%")
export(Adapter)
export(BodyPattern)
export(CrulAdapter)
export(HashCounter)
export(HeadersPattern)
export(HttpLibAdapaterRegistry)
export(HttrAdapter)
export(MethodPattern)
export(RequestPattern)
export(RequestRegistry)
export(RequestSignature)
export(Response)
export(StubCounter)
export(StubRegistry)
export(StubbedRequest)
export(UriPattern)
export(build_crul_request)
export(build_crul_response)
export(build_httr_request)
export(build_httr_response)
export(disable)
export(enable)
export(enabled)
export(httr_mock)
export(mock_file)
export(pluck_body)
export(remove_request_stub)
export(request_registry)
export(request_registry_clear)
export(stub_registry)
export(stub_registry_clear)
export(stub_request)
export(to_raise)
export(to_return)
export(to_return_)
export(to_timeout)
export(webmockr_allow_net_connect)
export(webmockr_configuration)
export(webmockr_configure)
export(webmockr_configure_reset)
export(webmockr_crul_fetch)
export(webmockr_disable)
export(webmockr_disable_net_connect)
export(webmockr_enable)
export(webmockr_net_connect_allowed)
export(webmockr_reset)
export(wi_th)
export(wi_th_)
importFrom(R6,R6Class)
importFrom(base64enc,base64encode)
importFrom(crul,mock)
importFrom(fauxpas,HTTPRequestTimeout)
importFrom(magrittr,"%>%")
webmockr/LICENSE 0000644 0001762 0000144 00000000057 14336066705 013104 0 ustar ligges users YEAR: 2022
COPYRIGHT HOLDER: Scott Chamberlain
webmockr/man/ 0000755 0001762 0000144 00000000000 14370732366 012651 5 ustar ligges users webmockr/man/enable.Rd 0000644 0001762 0000144 00000002100 14336067430 014352 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/flipswitch.R
\name{enable}
\alias{enable}
\alias{enabled}
\alias{disable}
\title{Enable or disable webmockr}
\usage{
enable(adapter = NULL, options = list(), quiet = FALSE)
enabled(adapter = "crul")
disable(adapter = NULL, options = list(), quiet = FALSE)
}
\arguments{
\item{adapter}{(character) the adapter name, 'crul' or 'httr'.
one or the other. if none given, we attempt to enable both
adapters}
\item{options}{list of options - ignored for now.}
\item{quiet}{(logical) suppress messages? default: \code{FALSE}}
}
\value{
\code{enable()} and \code{disable()} invisibly returns booleans for
each adapter, as a result of running enable or disable, respectively,
on each \link{HttpLibAdapaterRegistry} object. \code{enabled} returns a
single boolean
}
\description{
Enable or disable webmockr
}
\details{
\code{enable()} enables \pkg{webmockr} for all adapters.
\code{disable()} disables \pkg{webmockr} for all adapters. \code{enabled()}
answers whether \pkg{webmockr} is enabled for a given adapter
}
webmockr/man/HashCounter.Rd 0000644 0001762 0000144 00000005137 14336066705 015370 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/RequestRegistry.R
\name{HashCounter}
\alias{HashCounter}
\title{HashCounter}
\description{
hash with counter, to store requests, and count each time
it is used
}
\examples{
x <- HashCounter$new()
x$hash
z <- RequestSignature$new(method = "get", uri = "https:/httpbin.org/get")
x$put(z)
x$hash
x$get(z)
x$put(z)
x$get(z)
}
\seealso{
Other request-registry:
\code{\link{RequestRegistry}},
\code{\link{request_registry}()}
}
\concept{request-registry}
\section{Public fields}{
\if{html}{\out{
}}
\describe{
\item{\code{hash}}{(list) a list for internal use only, with elements
\code{key}, \code{sig}, and \code{count}}
}
\if{html}{\out{
}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-HashCounter-put}{\code{HashCounter$put()}}
\item \href{#method-HashCounter-get}{\code{HashCounter$get()}}
\item \href{#method-HashCounter-clone}{\code{HashCounter$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-HashCounter-put}{}}}
\subsection{Method \code{put()}}{
Register a request by it's key
\subsection{Usage}{
\if{html}{\out{}}\preformatted{HashCounter$put(req_sig)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{req_sig}}{an object of class \code{RequestSignature}}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; registers request and iterates
internal counter
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-HashCounter-get}{}}}
\subsection{Method \code{get()}}{
Get a request by key
\subsection{Usage}{
\if{html}{\out{}}\preformatted{HashCounter$get(req_sig)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{req_sig}}{an object of class \code{RequestSignature}}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
(integer) the count of how many times the request has been made
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-HashCounter-clone}{}}}
\subsection{Method \code{clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{}}\preformatted{HashCounter$clone(deep = FALSE)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{deep}}{Whether to make a deep clone.}
}
\if{html}{\out{
}}
}
}
}
webmockr/man/stub_request.Rd 0000644 0001762 0000144 00000013136 14300064462 015655 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/stub_request.R
\name{stub_request}
\alias{stub_request}
\title{Stub an http request}
\usage{
stub_request(method = "get", uri = NULL, uri_regex = NULL)
}
\arguments{
\item{method}{(character) HTTP method, one of "get", "post", "put", "patch",
"head", "delete", "options" - or the special "any" (for any method)}
\item{uri}{(character) The request uri. Can be a full or partial uri.
\pkg{webmockr} can match uri's without the "http" scheme, but does
not match if the scheme is "https". required, unless \code{uri_regex} given.
See \link{UriPattern} for more. See the "uri vs. uri_regex" section}
\item{uri_regex}{(character) A URI represented as regex. required, if \code{uri}
not given. See examples and the "uri vs. uri_regex" section}
}
\value{
an object of class \code{StubbedRequest}, with print method describing
the stub.
}
\description{
Stub an http request
}
\details{
Internally, this calls \link{StubbedRequest} which handles the logic
See \code{\link[=stub_registry]{stub_registry()}} for listing stubs, \code{\link[=stub_registry_clear]{stub_registry_clear()}}
for removing all stubs and \code{\link[=remove_request_stub]{remove_request_stub()}} for removing specific
stubs
If multiple stubs match the same request, we use the first stub. So if you
want to use a stub that was created after an earlier one that matches,
remove the earlier one(s).
Note on \code{wi_th()}: If you pass \code{query} values are coerced to character
class in the recorded stub. You can pass numeric, integer, etc., but
all will be coerced to character.
See \code{\link[=wi_th]{wi_th()}} for details on request body/query/headers and
\code{\link[=to_return]{to_return()}} for details on how response status/body/headers
are handled
}
\note{
Trailing slashes are dropped from stub URIs before matching
}
\section{uri vs. uri_regex}{
When you use \code{uri}, we compare the URIs without query params AND
also the query params themselves without the URIs.
When you use \code{uri_regex} we don't compare URIs and query params;
we just use your regex string defined in \code{uri_regex} as the pattern
for a call to \link{grepl}
}
\section{Mocking writing to disk}{
See \link{mocking-disk-writing}
}
\examples{
\dontrun{
# basic stubbing
stub_request("get", "https://httpbin.org/get")
stub_request("post", "https://httpbin.org/post")
# any method, use "any"
stub_request("any", "https://httpbin.org/get")
# list stubs
stub_registry()
# request headers
stub_request("get", "https://httpbin.org/get") \%>\%
wi_th(headers = list('User-Agent' = 'R'))
# request body
stub_request("post", "https://httpbin.org/post") \%>\%
wi_th(body = list(foo = 'bar'))
stub_registry()
library(crul)
x <- crul::HttpClient$new(url = "https://httpbin.org")
crul::mock()
x$post('post', body = list(foo = 'bar'))
# add expectation with to_return
stub_request("get", "https://httpbin.org/get") \%>\%
wi_th(
query = list(hello = "world"),
headers = list('User-Agent' = 'R')) \%>\%
to_return(status = 200, body = "stuff", headers = list(a = 5))
# list stubs again
stub_registry()
# regex
stub_request("get", uri_regex = ".+ample\\\\..")
# set stub an expectation to timeout
stub_request("get", "https://httpbin.org/get") \%>\% to_timeout()
x <- crul::HttpClient$new(url = "https://httpbin.org")
res <- x$get('get')
# raise exception
library(fauxpas)
stub_request("get", "https://httpbin.org/get") \%>\% to_raise(HTTPAccepted)
stub_request("get", "https://httpbin.org/get") \%>\% to_raise(HTTPAccepted, HTTPGone)
x <- crul::HttpClient$new(url = "https://httpbin.org")
stub_request("get", "https://httpbin.org/get") \%>\% to_raise(HTTPBadGateway)
crul::mock()
x$get('get')
# pass a list to .list
z <- stub_request("get", "https://httpbin.org/get")
wi_th(z, .list = list(query = list(foo = "bar")))
# just body
stub_request("any", uri_regex = ".+") \%>\%
wi_th(body = list(foo = 'bar'))
## with crul
library(crul)
x <- crul::HttpClient$new(url = "https://httpbin.org")
crul::mock()
x$post('post', body = list(foo = 'bar'))
x$put('put', body = list(foo = 'bar'))
## with httr
library(httr)
httr_mock()
POST('https://example.com', body = list(foo = 'bar'))
PUT('https://google.com', body = list(foo = 'bar'))
# just headers
headers <- list(
'Accept-Encoding' = 'gzip, deflate',
'Accept' = 'application/json, text/xml, application/xml, */*')
stub_request("any", uri_regex = ".+") \%>\% wi_th(headers = headers)
library(crul)
x <- crul::HttpClient$new(url = "https://httpbin.org", headers = headers)
crul::mock()
x$post('post')
x$put('put', body = list(foo = 'bar'))
x$get('put', query = list(stuff = 3423234L))
# many responses
## the first response matches the first to_return call, and so on
stub_request("get", "https://httpbin.org/get") \%>\%
to_return(status = 200, body = "foobar", headers = list(a = 5)) \%>\%
to_return(status = 200, body = "bears", headers = list(b = 6))
con <- crul::HttpClient$new(url = "https://httpbin.org")
con$get("get")$parse("UTF-8")
con$get("get")$parse("UTF-8")
## OR, use times with to_return() to repeat the same response many times
library(fauxpas)
stub_request("get", "https://httpbin.org/get") \%>\%
to_return(status = 200, body = "apple-pie", times = 2) \%>\%
to_raise(HTTPUnauthorized)
con <- crul::HttpClient$new(url = "https://httpbin.org")
con$get("get")$parse("UTF-8")
con$get("get")$parse("UTF-8")
con$get("get")$parse("UTF-8")
# clear all stubs
stub_registry()
stub_registry_clear()
}
}
\seealso{
\code{\link[=wi_th]{wi_th()}}, \code{\link[=to_return]{to_return()}}, \code{\link[=to_timeout]{to_timeout()}}, \code{\link[=to_raise]{to_raise()}},
\code{\link[=mock_file]{mock_file()}}
}
webmockr/man/build_crul_response.Rd 0000644 0001762 0000144 00000000530 14113773445 017176 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/adapter-crul.R
\name{build_crul_response}
\alias{build_crul_response}
\title{Build a crul response}
\usage{
build_crul_response(req, resp)
}
\arguments{
\item{req}{a request}
\item{resp}{a response}
}
\value{
a crul response
}
\description{
Build a crul response
}
webmockr/man/to_timeout.Rd 0000644 0001762 0000144 00000001025 14113773445 015324 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/to_timeout.R
\name{to_timeout}
\alias{to_timeout}
\title{Set timeout as an expected return on a match}
\usage{
to_timeout(.data)
}
\arguments{
\item{.data}{input. Anything that can be coerced to a \code{StubbedRequest} class
object}
}
\value{
an object of class \code{StubbedRequest}, with print method describing
the stub
}
\description{
Set timeout as an expected return on a match
}
\note{
see examples in \code{\link[=stub_request]{stub_request()}}
}
webmockr/man/to_raise.Rd 0000644 0001762 0000144 00000003056 14336067430 014744 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/to_raise.R
\name{to_raise}
\alias{to_raise}
\title{Set raise error condition}
\usage{
to_raise(.data, ...)
}
\arguments{
\item{.data}{input. Anything that can be coerced to a \code{StubbedRequest}
class object}
\item{...}{One or more HTTP exceptions from the \pkg{fauxpas} package. Run
\code{grep("HTTP*", getNamespaceExports("fauxpas"), value = TRUE)} for a list of
possible exceptions}
}
\value{
an object of class \code{StubbedRequest}, with print method describing
the stub
}
\description{
Set raise error condition
}
\details{
The behavior in the future will be:
When multiple exceptions are passed, the first is used on the first
mock, the second on the second mock, and so on. Subsequent mocks use the
last exception
But for now, only the first exception is used until we get that fixed
}
\note{
see examples in \code{\link[=stub_request]{stub_request()}}
}
\section{Raise vs. Return}{
\code{to_raise()} always raises a stop condition, while \code{to_return(status=xyz)} only
sets the status code on the returned HTTP response object. So if you want to
raise a stop condition then \code{to_raise()} is what you want. But if you
don't want to raise a stop condition use \code{to_return()}. Use cases for each
vary. For example, in a unit test you may have a test expecting a 503 error;
in this case \code{to_raise()} makes sense. In another case, if a unit test
expects to test some aspect of an HTTP response object that httr or crul
typically returns, then you'll want \code{to_return()}.
}
webmockr/man/pluck_body.Rd 0000644 0001762 0000144 00000001250 14113773445 015267 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/pluck_body.R
\name{pluck_body}
\alias{pluck_body}
\title{Extract the body from an HTTP request}
\usage{
pluck_body(x)
}
\arguments{
\item{x}{an unexecuted crul \emph{or} httr request object}
}
\value{
one of the following:
\itemize{
\item \code{NULL} if the request is not associated with a body
\item \code{NULL} if an upload is used not in a list
\item list containing the multipart-encoded body
\item character vector with the JSON- or raw-encoded body, or upload form file
}
}
\description{
Returns an appropriate representation of the data contained within a request
body based on its encoding.
}
webmockr/man/RequestPattern.Rd 0000644 0001762 0000144 00000011517 14336066705 016132 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/RequestPattern.R
\name{RequestPattern}
\alias{RequestPattern}
\title{RequestPattern class}
\description{
class handling all request matchers
}
\examples{
\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)
}
}
\seealso{
pattern classes for HTTP method \link{MethodPattern}, headers
\link{HeadersPattern}, body \link{BodyPattern}, and URI/URL \link{UriPattern}
}
\section{Public fields}{
\if{html}{\out{}}
\describe{
\item{\code{method_pattern}}{xxx}
\item{\code{uri_pattern}}{xxx}
\item{\code{body_pattern}}{xxx}
\item{\code{headers_pattern}}{xxx}
}
\if{html}{\out{
}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-RequestPattern-new}{\code{RequestPattern$new()}}
\item \href{#method-RequestPattern-matches}{\code{RequestPattern$matches()}}
\item \href{#method-RequestPattern-to_s}{\code{RequestPattern$to_s()}}
\item \href{#method-RequestPattern-clone}{\code{RequestPattern$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-RequestPattern-new}{}}}
\subsection{Method \code{new()}}{
Create a new \code{RequestPattern} object
\subsection{Usage}{
\if{html}{\out{}}\preformatted{RequestPattern$new(
method,
uri = NULL,
uri_regex = NULL,
query = NULL,
body = NULL,
headers = NULL
)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{method}}{the HTTP method (any, head, options, get, post, put,
patch, trace, or delete). "any" matches any HTTP method. required.}
\item{\code{uri}}{(character) request URI. required or uri_regex}
\item{\code{uri_regex}}{(character) request URI as regex. required or uri}
\item{\code{query}}{(list) query parameters, optional}
\item{\code{body}}{(list) body request, optional}
\item{\code{headers}}{(list) headers, optional}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
A new \code{RequestPattern} object
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-RequestPattern-matches}{}}}
\subsection{Method \code{matches()}}{
does a request signature match the selected matchers?
\subsection{Usage}{
\if{html}{\out{}}\preformatted{RequestPattern$matches(request_signature)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{request_signature}}{a \link{RequestSignature} object}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
a boolean
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-RequestPattern-to_s}{}}}
\subsection{Method \code{to_s()}}{
Print pattern for easy human consumption
\subsection{Usage}{
\if{html}{\out{}}\preformatted{RequestPattern$to_s()}\if{html}{\out{
}}
}
\subsection{Returns}{
a string
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-RequestPattern-clone}{}}}
\subsection{Method \code{clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{}}\preformatted{RequestPattern$clone(deep = FALSE)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{deep}}{Whether to make a deep clone.}
}
\if{html}{\out{
}}
}
}
}
webmockr/man/build_httr_request.Rd 0000644 0001762 0000144 00000000507 14113773445 017050 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/adapter-httr.R
\name{build_httr_request}
\alias{build_httr_request}
\title{Build a httr request}
\usage{
build_httr_request(x)
}
\arguments{
\item{x}{an unexecuted httr request object}
}
\value{
a httr request
}
\description{
Build a httr request
}
webmockr/man/UriPattern.Rd 0000644 0001762 0000144 00000017626 14336066705 015250 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/RequestPattern.R
\name{UriPattern}
\alias{UriPattern}
\title{UriPattern}
\description{
uri matcher
}
\examples{
# trailing slash
(z <- UriPattern$new(pattern = "http://foobar.com"))
z$matches("http://foobar.com") # TRUE
z$matches("http://foobar.com/") # TRUE
# without scheme
## matches http by default: does not match https by default
(z <- UriPattern$new(pattern = "foobar.com"))
z$matches("http://foobar.com") # TRUE
z$matches("http://foobar.com/") # TRUE
z$matches("https://foobar.com") # FALSE
z$matches("https://foobar.com/") # FALSE
## to match https, you'll have to give the complete url
(z <- UriPattern$new(pattern = "https://foobar.com"))
z$matches("https://foobar.com/") # TRUE
z$matches("http://foobar.com/") # FALSE
# default ports
(z <- UriPattern$new(pattern = "http://foobar.com"))
z$matches("http://foobar.com:80") # TRUE
z$matches("http://foobar.com:80/") # TRUE
z$matches("http://foobar.com:443") # TRUE
z$matches("http://foobar.com:443/") # TRUE
# user info - FIXME, not sure we support this yet
(z <- UriPattern$new(pattern = "http://foobar.com"))
z$matches("http://user:pass@foobar.com")
# regex
(z <- UriPattern$new(regex_pattern = ".+ample\\\\.."))
z$matches("http://sample.org") # TRUE
z$matches("http://example.com") # TRUE
z$matches("http://tramples.net") # FALSE
# add query parameters
(z <- UriPattern$new(pattern = "http://foobar.com"))
z$add_query_params(list(pizza = "cheese", cheese = "cheddar"))
z
z$pattern
z$matches("http://foobar.com?pizza=cheese&cheese=cheddar") # TRUE
z$matches("http://foobar.com?pizza=cheese&cheese=swiss") # FALSE
# query parameters in the uri
(z <- UriPattern$new(pattern = "https://httpbin.org/get?stuff=things"))
z$add_query_params() # have to run this method to gather query params
z$matches("https://httpbin.org/get?stuff=things") # TRUE
z$matches("https://httpbin.org/get?stuff2=things") # FALSE
# regex add query parameters
(z <- UriPattern$new(regex_pattern = "https://foobar.com/.+/order"))
z$add_query_params(list(pizza = "cheese"))
z
z$pattern
z$matches("https://foobar.com/pizzas/order?pizza=cheese") # TRUE
z$matches("https://foobar.com/pizzas?pizza=cheese") # FALSE
# query parameters in the regex uri
(z <- UriPattern$new(regex_pattern = "https://x.com/.+/order\\\\?fruit=apple"))
z$add_query_params() # have to run this method to gather query params
z$matches("https://x.com/a/order?fruit=apple") # TRUE
z$matches("https://x.com/a?fruit=apple") # FALSE
# any pattern
(z <- UriPattern$new(regex_pattern = "stuff\\\\.com.+"))
z$regex
z$pattern
z$matches("http://stuff.com") # FALSE
z$matches("https://stuff.com/stff") # TRUE
z$matches("https://stuff.com/apple?bears=brown&bats=grey") # TRUE
}
\keyword{internal}
\section{Public fields}{
\if{html}{\out{}}
\describe{
\item{\code{pattern}}{(character) pattern holder}
\item{\code{regex}}{a logical}
\item{\code{query_params}}{a list, or \code{NULL} if empty}
}
\if{html}{\out{
}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-UriPattern-new}{\code{UriPattern$new()}}
\item \href{#method-UriPattern-matches}{\code{UriPattern$matches()}}
\item \href{#method-UriPattern-pattern_matches}{\code{UriPattern$pattern_matches()}}
\item \href{#method-UriPattern-query_params_matches}{\code{UriPattern$query_params_matches()}}
\item \href{#method-UriPattern-extract_query}{\code{UriPattern$extract_query()}}
\item \href{#method-UriPattern-add_query_params}{\code{UriPattern$add_query_params()}}
\item \href{#method-UriPattern-to_s}{\code{UriPattern$to_s()}}
\item \href{#method-UriPattern-clone}{\code{UriPattern$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-UriPattern-new}{}}}
\subsection{Method \code{new()}}{
Create a new \code{UriPattern} object
\subsection{Usage}{
\if{html}{\out{}}\preformatted{UriPattern$new(pattern = NULL, regex_pattern = NULL)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{pattern}}{(character) a uri, as a character string. if scheme
is missing, it is added (we assume http)}
\item{\code{regex_pattern}}{(character) a uri as a regex character string,
see \link[base:regex]{base::regex}. if scheme is missing, it is added (we assume
http)}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
A new \code{UriPattern} object
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-UriPattern-matches}{}}}
\subsection{Method \code{matches()}}{
Match a uri against a pattern
\subsection{Usage}{
\if{html}{\out{}}\preformatted{UriPattern$matches(uri)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{uri}}{(character) a uri}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
a boolean
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-UriPattern-pattern_matches}{}}}
\subsection{Method \code{pattern_matches()}}{
Match a URI
\subsection{Usage}{
\if{html}{\out{}}\preformatted{UriPattern$pattern_matches(uri)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{uri}}{(character) a uri}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
a boolean
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-UriPattern-query_params_matches}{}}}
\subsection{Method \code{query_params_matches()}}{
Match query parameters of a URI
\subsection{Usage}{
\if{html}{\out{}}\preformatted{UriPattern$query_params_matches(uri)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{uri}}{(character) a uri}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
a boolean
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-UriPattern-extract_query}{}}}
\subsection{Method \code{extract_query()}}{
Extract query parameters as a named list
\subsection{Usage}{
\if{html}{\out{}}\preformatted{UriPattern$extract_query(uri)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{uri}}{(character) a uri}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
named list, or \code{NULL} if no query parameters
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-UriPattern-add_query_params}{}}}
\subsection{Method \code{add_query_params()}}{
Add query parameters to the URI
\subsection{Usage}{
\if{html}{\out{}}\preformatted{UriPattern$add_query_params(query_params)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{query_params}}{(list|character) list or character}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned, updates uri pattern
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-UriPattern-to_s}{}}}
\subsection{Method \code{to_s()}}{
Print pattern for easy human consumption
\subsection{Usage}{
\if{html}{\out{}}\preformatted{UriPattern$to_s()}\if{html}{\out{
}}
}
\subsection{Returns}{
a string
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-UriPattern-clone}{}}}
\subsection{Method \code{clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{}}\preformatted{UriPattern$clone(deep = FALSE)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{deep}}{Whether to make a deep clone.}
}
\if{html}{\out{
}}
}
}
}
webmockr/man/build_httr_response.Rd 0000644 0001762 0000144 00000000530 14113773445 017212 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/adapter-httr.R
\name{build_httr_response}
\alias{build_httr_response}
\title{Build a httr response}
\usage{
build_httr_response(req, resp)
}
\arguments{
\item{req}{a request}
\item{resp}{a response}
}
\value{
a httr response
}
\description{
Build a httr response
}
webmockr/man/webmockr_enable-defunct.Rd 0000644 0001762 0000144 00000000405 14113773445 017702 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/defunct.R
\name{webmockr_enable}
\alias{webmockr_enable}
\title{This function is defunct.}
\usage{
webmockr_enable(...)
}
\description{
This function is defunct.
}
\keyword{internal}
webmockr/man/stub_registry.Rd 0000644 0001762 0000144 00000001654 14113773445 016051 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/stub_registry.R
\name{stub_registry}
\alias{stub_registry}
\title{List stubs in the stub registry}
\usage{
stub_registry()
}
\value{
an object of class \code{StubRegistry}, print method gives the
stubs in the registry
}
\description{
List stubs in the stub registry
}
\examples{
# make a stub
stub_request("get", "https://httpbin.org/get") \%>\%
to_return(body = "success!", status = 200)
# check the stub registry, there should be one in there
stub_registry()
# make another stub
stub_request("get", "https://httpbin.org/get") \%>\%
to_return(body = "woopsy", status = 404)
# check the stub registry, now there are two there
stub_registry()
# to clear the stub registry
stub_registry_clear()
}
\seealso{
Other stub-registry:
\code{\link{StubRegistry}},
\code{\link{remove_request_stub}()},
\code{\link{stub_registry_clear}()}
}
\concept{stub-registry}
webmockr/man/wi_th.Rd 0000644 0001762 0000144 00000006413 14113773445 014254 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/wi_th.R
\name{wi_th}
\alias{wi_th}
\title{Set additional parts of a stubbed request}
\usage{
wi_th(.data, ..., .list = list())
}
\arguments{
\item{.data}{input. Anything that can be coerced to a \code{StubbedRequest} class
object}
\item{...}{Comma separated list of named variables. accepts the following:
\code{query}, \code{body}, \code{headers}, \code{basic_auth}. See Details.}
\item{.list}{named list, has to be one of \code{query}, \code{body},
\code{headers} and/or \code{basic_auth}. An alternative to passing in via \code{...}.
Don't pass the same thing to both, e.g. don't pass 'query' to \code{...}, and
also 'query' to this parameter}
}
\value{
an object of class \code{StubbedRequest}, with print method describing
the stub
}
\description{
Set query params, request body, request headers and/or basic_auth
}
\details{
\code{with} is a function in the \code{base} package, so we went with
\code{wi_th}
Values for query, body, headers, and basic_auth:
\itemize{
\item query: (list) a named list. values are coerced to character
class in the recorded stub. You can pass numeric, integer, etc., but
all will be coerced to character.
\item body: various, including character string, list, raw, numeric,
upload (\code{crul::upload} or \code{httr::upload_file}, they both create the
same object in the end)
\item headers: (list) a named list
\item basic_auth: (character) a length two vector, username and password.
authentication type (basic/digest/ntlm/etc.) is ignored. that is,
mocking authenciation right now does not take into account the
authentication type. We don't do any checking of the username/password
except to detect edge cases where for example, the username/password
were probably not set by the user on purpose (e.g., a URL is
picked up by an environment variable)
}
Note that there is no regex matching on query, body, or headers. They
are tested for matches in the following ways:
\itemize{
\item query: compare stubs and requests with \code{identical()}. this compares
named lists, so both list names and values are compared
\item body: varies depending on the body format (list vs. character, etc.)
\item headers: compare stub and request values with \code{==}. list names are
compared with \code{\%in\%}. \code{basic_auth} is included in headers (with the name
Authorization)
}
}
\note{
see more examples in \code{\link[=stub_request]{stub_request()}}
}
\examples{
# first, make a stub object
req <- stub_request("post", "https://httpbin.org/post")
# add body
# list
wi_th(req, body = list(foo = "bar"))
# string
wi_th(req, body = '{"foo": "bar"}')
# raw
wi_th(req, body = charToRaw('{"foo": "bar"}'))
# numeric
wi_th(req, body = 5)
# an upload
wi_th(req, body = crul::upload(system.file("CITATION")))
# wi_th(req, body = httr::upload_file(system.file("CITATION")))
# add query - has to be a named list
wi_th(req, query = list(foo = "bar"))
# add headers - has to be a named list
wi_th(req, headers = list(foo = "bar"))
wi_th(req, headers = list(`User-Agent` = "webmockr/v1", hello="world"))
# .list - pass in a named list instead
wi_th(req, .list = list(body = list(foo = "bar")))
# basic authentication
wi_th(req, basic_auth = c("user", "pass"))
wi_th(req, basic_auth = c("user", "pass"), headers = list(foo = "bar"))
}
webmockr/man/mocking-disk-writing.Rd 0000644 0001762 0000144 00000004046 14113773445 017202 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/mocking-disk-writing.R
\name{mocking-disk-writing}
\alias{mocking-disk-writing}
\title{Mocking writing to disk}
\description{
Mocking writing to disk
}
\examples{
\dontrun{
# enable mocking
enable()
# Write to a file before mocked request
# crul
library(crul)
## make a temp file
f <- tempfile(fileext = ".json")
## write something to the file
cat("{\"hello\":\"world\"}\n", file = f)
readLines(f)
## make the stub
stub_request("get", "https://httpbin.org/get") \%>\%
to_return(body = file(f))
## make a request
(out <- HttpClient$new("https://httpbin.org/get")$get(disk = f))
out$content
readLines(out$content)
# httr
library(httr)
## make a temp file
f <- tempfile(fileext = ".json")
## write something to the file
cat("{\"hello\":\"world\"}\n", file = f)
readLines(f)
## make the stub
stub_request("get", "https://httpbin.org/get") \%>\%
to_return(body = file(f),
headers = list('content-type' = "application/json"))
## make a request
## with httr, you must set overwrite=TRUE or you'll get an errror
out <- GET("https://httpbin.org/get", write_disk(f, overwrite=TRUE))
out
out$content
content(out, "text", encoding = "UTF-8")
# Use mock_file to have webmockr handle file and contents
# crul
library(crul)
f <- tempfile(fileext = ".json")
## make the stub
stub_request("get", "https://httpbin.org/get") \%>\%
to_return(body = mock_file(f, "{\"hello\":\"mars\"}\n"))
## make a request
(out <- crul::HttpClient$new("https://httpbin.org/get")$get(disk = f))
out$content
readLines(out$content)
# httr
library(httr)
## make a temp file
f <- tempfile(fileext = ".json")
## make the stub
stub_request("get", "https://httpbin.org/get") \%>\%
to_return(
body = mock_file(path = f, payload = "{\"foo\": \"bar\"}"),
headers = list('content-type' = "application/json")
)
## make a request
out <- GET("https://httpbin.org/get", write_disk(f))
out
## view stubbed file content
out$content
readLines(out$content)
content(out, "text", encoding = "UTF-8")
# disable mocking
disable()
}
}
webmockr/man/Adapter.Rd 0000644 0001762 0000144 00000022151 14336067430 014514 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/adapter-crul.R, R/adapter-httr.R, R/adapter.R
\name{CrulAdapter}
\alias{CrulAdapter}
\alias{HttrAdapter}
\alias{Adapter}
\title{Adapters for Modifying HTTP Requests}
\description{
\code{Adapter} is the base parent class used to implement
\pkg{webmockr} support for different HTTP clients. It should not be used
directly. Instead, use one of the client-specific adapters that webmockr
currently provides:
\itemize{
\item \code{CrulAdapter} for \pkg{crul}
\item \code{HttrAdapter} for \pkg{httr}
}
}
\details{
Note that the documented fields and methods are the same across all
client-specific adapters.
}
\examples{
\dontrun{
if (requireNamespace("httr", quietly = TRUE)) {
# library(httr)
# normal httr request, works fine
# real <- GET("https://httpbin.org/get")
# real
# with webmockr
# library(webmockr)
## turn on httr mocking
# httr_mock()
## now this request isn't allowed
# GET("https://httpbin.org/get")
## stub the request
# stub_request('get', uri = 'https://httpbin.org/get') \%>\%
# wi_th(
# headers = list('Accept' = 'application/json, text/xml, application/xml, */*')
# ) \%>\%
# to_return(status = 418, body = "I'm a teapot!", headers = list(a = 5))
## now the request succeeds and returns a mocked response
# (res <- GET("https://httpbin.org/get"))
# res$status_code
# rawToChar(res$content)
# allow real requests while webmockr is loaded
# webmockr_allow_net_connect()
# webmockr_net_connect_allowed()
# GET("https://httpbin.org/get?animal=chicken")
# webmockr_disable_net_connect()
# webmockr_net_connect_allowed()
# GET("https://httpbin.org/get?animal=chicken")
# httr_mock(FALSE)
}
}
}
\section{Super class}{
\code{\link[webmockr:Adapter]{webmockr::Adapter}} -> \code{CrulAdapter}
}
\section{Public fields}{
\if{html}{\out{}}
\describe{
\item{\code{client}}{HTTP client package name}
\item{\code{name}}{adapter name}
}
\if{html}{\out{
}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-CrulAdapter-clone}{\code{CrulAdapter$clone()}}
}
}
\if{html}{\out{
Inherited methods
}}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-CrulAdapter-clone}{}}}
\subsection{Method \code{clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{}}\preformatted{CrulAdapter$clone(deep = FALSE)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{deep}}{Whether to make a deep clone.}
}
\if{html}{\out{
}}
}
}
}
\section{Super class}{
\code{\link[webmockr:Adapter]{webmockr::Adapter}} -> \code{HttrAdapter}
}
\section{Public fields}{
\if{html}{\out{}}
\describe{
\item{\code{client}}{HTTP client package name}
\item{\code{name}}{adapter name}
}
\if{html}{\out{
}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-HttrAdapter-clone}{\code{HttrAdapter$clone()}}
}
}
\if{html}{\out{
Inherited methods
}}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-HttrAdapter-clone}{}}}
\subsection{Method \code{clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{}}\preformatted{HttrAdapter$clone(deep = FALSE)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{deep}}{Whether to make a deep clone.}
}
\if{html}{\out{
}}
}
}
}
\section{Public fields}{
\if{html}{\out{}}
\describe{
\item{\code{client}}{HTTP client package name}
\item{\code{name}}{adapter name}
}
\if{html}{\out{
}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-Adapter-new}{\code{Adapter$new()}}
\item \href{#method-Adapter-enable}{\code{Adapter$enable()}}
\item \href{#method-Adapter-disable}{\code{Adapter$disable()}}
\item \href{#method-Adapter-handle_request}{\code{Adapter$handle_request()}}
\item \href{#method-Adapter-remove_stubs}{\code{Adapter$remove_stubs()}}
\item \href{#method-Adapter-clone}{\code{Adapter$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-Adapter-new}{}}}
\subsection{Method \code{new()}}{
Create a new Adapter object
\subsection{Usage}{
\if{html}{\out{}}\preformatted{Adapter$new()}\if{html}{\out{
}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-Adapter-enable}{}}}
\subsection{Method \code{enable()}}{
Enable the adapter
\subsection{Usage}{
\if{html}{\out{}}\preformatted{Adapter$enable(quiet = FALSE)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{quiet}}{(logical) suppress messages? default: \code{FALSE}}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
\code{TRUE}, invisibly
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-Adapter-disable}{}}}
\subsection{Method \code{disable()}}{
Disable the adapter
\subsection{Usage}{
\if{html}{\out{}}\preformatted{Adapter$disable(quiet = FALSE)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{quiet}}{(logical) suppress messages? default: \code{FALSE}}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
\code{FALSE}, invisibly
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-Adapter-handle_request}{}}}
\subsection{Method \code{handle_request()}}{
All logic for handling a request
\subsection{Usage}{
\if{html}{\out{}}\preformatted{Adapter$handle_request(req)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{req}}{a request}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
various outcomes
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-Adapter-remove_stubs}{}}}
\subsection{Method \code{remove_stubs()}}{
Remove all stubs
\subsection{Usage}{
\if{html}{\out{}}\preformatted{Adapter$remove_stubs()}\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; removes all request stubs
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-Adapter-clone}{}}}
\subsection{Method \code{clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{}}\preformatted{Adapter$clone(deep = FALSE)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{deep}}{Whether to make a deep clone.}
}
\if{html}{\out{
}}
}
}
}
webmockr/man/to_return_-defunct.Rd 0000644 0001762 0000144 00000000366 14113773445 016751 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/defunct.R
\name{to_return_}
\alias{to_return_}
\title{This function is defunct.}
\usage{
to_return_(...)
}
\description{
This function is defunct.
}
\keyword{internal}
webmockr/man/wi_th_-defunct.Rd 0000644 0001762 0000144 00000000352 14113773445 016035 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/defunct.R
\name{wi_th_}
\alias{wi_th_}
\title{This function is defunct.}
\usage{
wi_th_(...)
}
\description{
This function is defunct.
}
\keyword{internal}
webmockr/man/webmockr-package.Rd 0000644 0001762 0000144 00000001514 14336067430 016336 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/webmockr.R
\docType{package}
\name{webmockr-package}
\alias{webmockr-package}
\alias{webmockr}
\title{webmockr}
\description{
Stubbing and setting expectations on HTTP requests
}
\section{Features}{
\itemize{
\item Stubbing HTTP requests at low http client lib level
\item Setting and verifying expectations on HTTP requests
\item Matching requests based on method, URI, headers and body
\item Supports multiple HTTP libraries, including \pkg{crul} and
\pkg{httr}
\item Integration with HTTP test caching library \pkg{vcr}
}
}
\examples{
library(webmockr)
stub_request("get", "https://httpbin.org/get")
stub_request("post", "https://httpbin.org/post")
stub_registry()
}
\author{
Scott Chamberlain \email{myrmecocystus+r@gmail.com}
Aaron Wolen
}
\keyword{package}
webmockr/man/to_return.Rd 0000644 0001762 0000144 00000010135 14336067430 015154 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/to_return.R
\name{to_return}
\alias{to_return}
\title{Expectation for what's returned from a stubbed request}
\usage{
to_return(.data, ..., .list = list(), times = 1)
}
\arguments{
\item{.data}{input. Anything that can be coerced to a \code{StubbedRequest} class
object}
\item{...}{Comma separated list of named variables. accepts the following:
\code{status}, \code{body}, \code{headers}. See Details for more.}
\item{.list}{named list, has to be one of 'status', 'body',
and/or 'headers'. An alternative to passing in via \code{...}. Don't pass the
same thing to both, e.g. don't pass 'status' to \code{...}, and also 'status' to
this parameter}
\item{times}{(integer) number of times the given response should be
returned; default: 1. value must be greater than or equal to 1. Very large
values probably don't make sense, but there's no maximum value. See
Details.}
}
\value{
an object of class \code{StubbedRequest}, with print method describing
the stub
}
\description{
Set response status code, response body, and/or response headers
}
\details{
Values for status, body, and headers:
\itemize{
\item status: (numeric/integer) three digit status code
\item body: various: \code{character}, \code{json}, \code{list}, \code{raw}, \code{numeric},
\code{NULL}, \code{FALSE}, a file connection (other connetion types
not supported), or a \code{mock_file} function call (see \code{\link[=mock_file]{mock_file()}})
\item headers: (list) a named list, must be named
}
response headers are returned with all lowercase names and the values
are all of type character. if numeric/integer values are given
(e.g., \code{to_return(headers = list(a = 10))}), we'll coerce any
numeric/integer values to character.
}
\note{
see more examples in \code{\link[=stub_request]{stub_request()}}
}
\section{multiple \code{to_return()}}{
You can add more than one \code{to_return()} to a webmockr stub (including
\code{\link[=to_raise]{to_raise()}}, \code{\link[=to_timeout]{to_timeout()}}). Each one is a HTTP response returned.
That is, you'll match to an HTTP request based on \code{stub_request()} and
\code{wi_th()}; the first time the request is made, the first response
is returned; the second time the reqeust is made, the second response
is returned; and so on.
Be aware that webmockr has to track number of requests
(see \code{\link[=request_registry]{request_registry()}}), and so if you use multiple \code{to_return()}
or the \code{times} parameter, you must clear the request registry
in order to go back to mocking responses from the start again.
\code{\link[=webmockr_reset]{webmockr_reset()}} clears the stub registry and the request registry,
after which you can use multiple responses again (after creating
your stub(s) again of course)
}
\section{Raise vs. Return}{
\code{to_raise()} always raises a stop condition, while \code{to_return(status=xyz)} only
sets the status code on the returned HTTP response object. So if you want to
raise a stop condition then \code{to_raise()} is what you want. But if you
don't want to raise a stop condition use \code{to_return()}. Use cases for each
vary. For example, in a unit test you may have a test expecting a 503 error;
in this case \code{to_raise()} makes sense. In another case, if a unit test
expects to test some aspect of an HTTP response object that httr or crul
typically returns, then you'll want \code{to_return()}.
}
\examples{
# first, make a stub object
foo <- function() {
stub_request("post", "https://httpbin.org/post")
}
# add status, body and/or headers
foo() \%>\% to_return(status = 200)
foo() \%>\% to_return(body = "stuff")
foo() \%>\% to_return(body = list(a = list(b = "world")))
foo() \%>\% to_return(headers = list(a = 5))
foo() \%>\%
to_return(status = 200, body = "stuff", headers = list(a = 5))
# .list - pass in a named list instead
foo() \%>\% to_return(.list = list(body = list(foo = "bar")))
# multiple responses using chained `to_return()`
foo() \%>\% to_return(body = "stuff") \%>\% to_return(body = "things")
# many of the same response using the times parameter
foo() \%>\% to_return(body = "stuff", times = 3)
}
webmockr/man/httr_mock.Rd 0000644 0001762 0000144 00000001036 14113773445 015130 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/adapter-httr.R
\name{httr_mock}
\alias{httr_mock}
\title{Turn on httr mocking
Sets a callback that routes httr request through webmockr}
\usage{
httr_mock(on = TRUE)
}
\arguments{
\item{on}{(logical) set to \code{TRUE} to turn on, and \code{FALSE}
to turn off. default: \code{TRUE}}
}
\value{
Silently returns \code{TRUE} when enabled and \code{FALSE} when disabled.
}
\description{
Turn on httr mocking
Sets a callback that routes httr request through webmockr
}
webmockr/man/webmockr_disable-defunct.Rd 0000644 0001762 0000144 00000000410 14113773445 020053 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/defunct.R
\name{webmockr_disable}
\alias{webmockr_disable}
\title{This function is defunct.}
\usage{
webmockr_disable(...)
}
\description{
This function is defunct.
}
\keyword{internal}
webmockr/man/Response.Rd 0000644 0001762 0000144 00000023360 14336066705 014741 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/Response.R
\name{Response}
\alias{Response}
\title{Response}
\description{
custom webmockr http response class
}
\examples{
\dontrun{
(x <- Response$new())
x$set_url("https://httpbin.org/get")
x
x$set_request_headers(list('Content-Type' = "application/json"))
x
x$request_headers
x$set_response_headers(list('Host' = "httpbin.org"))
x
x$response_headers
x$set_status(404)
x
x$get_status()
x$set_body("hello world")
x
x$get_body()
# raw body
x$set_body(charToRaw("hello world"))
x
x$get_body()
x$set_exception("exception")
x
x$get_exception()
}
}
\section{Public fields}{
\if{html}{\out{}}
\describe{
\item{\code{url}}{(character) a url}
\item{\code{body}}{(various) list, character, etc}
\item{\code{content}}{(various) response content/body}
\item{\code{request_headers}}{(list) a named list}
\item{\code{response_headers}}{(list) a named list}
\item{\code{options}}{(character) list}
\item{\code{status_code}}{(integer) an http status code}
\item{\code{exception}}{(character) an exception message}
\item{\code{should_timeout}}{(logical) should the response timeout?}
}
\if{html}{\out{
}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-Response-new}{\code{Response$new()}}
\item \href{#method-Response-print}{\code{Response$print()}}
\item \href{#method-Response-set_url}{\code{Response$set_url()}}
\item \href{#method-Response-get_url}{\code{Response$get_url()}}
\item \href{#method-Response-set_request_headers}{\code{Response$set_request_headers()}}
\item \href{#method-Response-get_request_headers}{\code{Response$get_request_headers()}}
\item \href{#method-Response-set_response_headers}{\code{Response$set_response_headers()}}
\item \href{#method-Response-get_respone_headers}{\code{Response$get_respone_headers()}}
\item \href{#method-Response-set_body}{\code{Response$set_body()}}
\item \href{#method-Response-get_body}{\code{Response$get_body()}}
\item \href{#method-Response-set_status}{\code{Response$set_status()}}
\item \href{#method-Response-get_status}{\code{Response$get_status()}}
\item \href{#method-Response-set_exception}{\code{Response$set_exception()}}
\item \href{#method-Response-get_exception}{\code{Response$get_exception()}}
\item \href{#method-Response-clone}{\code{Response$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-Response-new}{}}}
\subsection{Method \code{new()}}{
Create a new \code{Response} object
\subsection{Usage}{
\if{html}{\out{}}\preformatted{Response$new(options = list())}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{options}}{(list) a list of options}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
A new \code{Response} object
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-Response-print}{}}}
\subsection{Method \code{print()}}{
print method for the \code{Response} class
\subsection{Usage}{
\if{html}{\out{}}\preformatted{Response$print(x, ...)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{x}}{self}
\item{\code{...}}{ignored}
}
\if{html}{\out{
}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-Response-set_url}{}}}
\subsection{Method \code{set_url()}}{
set the url for the response
\subsection{Usage}{
\if{html}{\out{}}\preformatted{Response$set_url(url)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{url}}{(character) a url}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; sets url
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-Response-get_url}{}}}
\subsection{Method \code{get_url()}}{
get the url for the response
\subsection{Usage}{
\if{html}{\out{}}\preformatted{Response$get_url()}\if{html}{\out{
}}
}
\subsection{Returns}{
(character) a url
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-Response-set_request_headers}{}}}
\subsection{Method \code{set_request_headers()}}{
set the request headers for the response
\subsection{Usage}{
\if{html}{\out{}}\preformatted{Response$set_request_headers(headers, capitalize = TRUE)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{headers}}{(list) named list}
\item{\code{capitalize}}{(logical) whether to capitalize first letters of
each header; default: \code{TRUE}}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; sets request headers on the response
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-Response-get_request_headers}{}}}
\subsection{Method \code{get_request_headers()}}{
get the request headers for the response
\subsection{Usage}{
\if{html}{\out{}}\preformatted{Response$get_request_headers()}\if{html}{\out{
}}
}
\subsection{Returns}{
(list) request headers, a named list
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-Response-set_response_headers}{}}}
\subsection{Method \code{set_response_headers()}}{
set the response headers for the response
\subsection{Usage}{
\if{html}{\out{}}\preformatted{Response$set_response_headers(headers, capitalize = TRUE)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{headers}}{(list) named list}
\item{\code{capitalize}}{(logical) whether to capitalize first letters of
each header; default: \code{TRUE}}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; sets response headers on the response
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-Response-get_respone_headers}{}}}
\subsection{Method \code{get_respone_headers()}}{
get the response headers for the response
\subsection{Usage}{
\if{html}{\out{}}\preformatted{Response$get_respone_headers()}\if{html}{\out{
}}
}
\subsection{Returns}{
(list) response headers, a named list
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-Response-set_body}{}}}
\subsection{Method \code{set_body()}}{
set the body of the response
\subsection{Usage}{
\if{html}{\out{}}\preformatted{Response$set_body(body, disk = FALSE)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{body}}{(various types)}
\item{\code{disk}}{(logical) whether its on disk; default: \code{FALSE}}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; sets body on the response
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-Response-get_body}{}}}
\subsection{Method \code{get_body()}}{
get the body of the response
\subsection{Usage}{
\if{html}{\out{}}\preformatted{Response$get_body()}\if{html}{\out{
}}
}
\subsection{Returns}{
various
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-Response-set_status}{}}}
\subsection{Method \code{set_status()}}{
set the http status of the response
\subsection{Usage}{
\if{html}{\out{}}\preformatted{Response$set_status(status)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{status}}{(integer) the http status}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; sets the http status of the response
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-Response-get_status}{}}}
\subsection{Method \code{get_status()}}{
get the http status of the response
\subsection{Usage}{
\if{html}{\out{}}\preformatted{Response$get_status()}\if{html}{\out{
}}
}
\subsection{Returns}{
(integer) the http status
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-Response-set_exception}{}}}
\subsection{Method \code{set_exception()}}{
set an exception
\subsection{Usage}{
\if{html}{\out{}}\preformatted{Response$set_exception(exception)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{exception}}{(character) an exception string}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; sets an exception
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-Response-get_exception}{}}}
\subsection{Method \code{get_exception()}}{
get the exception, if set
\subsection{Usage}{
\if{html}{\out{}}\preformatted{Response$get_exception()}\if{html}{\out{
}}
}
\subsection{Returns}{
(character) an exception
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-Response-clone}{}}}
\subsection{Method \code{clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{}}\preformatted{Response$clone(deep = FALSE)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{deep}}{Whether to make a deep clone.}
}
\if{html}{\out{
}}
}
}
}
webmockr/man/StubCounter.Rd 0000644 0001762 0000144 00000004455 14362302636 015417 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/StubbedRequest.R
\name{StubCounter}
\alias{StubCounter}
\title{StubCounter}
\description{
hash with counter to store requests and count number
of requests made against the stub
}
\examples{
x <- StubCounter$new()
x
x$hash
x$count()
z <- RequestSignature$new(method = "get", uri = "https:/httpbin.org/get")
x$put(z)
x$count()
x$put(z)
x$count()
}
\section{Public fields}{
\if{html}{\out{}}
\describe{
\item{\code{hash}}{(list) a list for internal use only, with elements
\code{key}, \code{sig}, and \code{count}}
}
\if{html}{\out{
}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-StubCounter-put}{\code{StubCounter$put()}}
\item \href{#method-StubCounter-count}{\code{StubCounter$count()}}
\item \href{#method-StubCounter-clone}{\code{StubCounter$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-StubCounter-put}{}}}
\subsection{Method \code{put()}}{
Register a request by it's key
\subsection{Usage}{
\if{html}{\out{}}\preformatted{StubCounter$put(x)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{x}}{an object of class \code{RequestSignature}}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; registers request & iterates internal counter
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-StubCounter-count}{}}}
\subsection{Method \code{count()}}{
Get the count of number of times any matching request has
been made against this stub
\subsection{Usage}{
\if{html}{\out{}}\preformatted{StubCounter$count()}\if{html}{\out{
}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-StubCounter-clone}{}}}
\subsection{Method \code{clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{}}\preformatted{StubCounter$clone(deep = FALSE)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{deep}}{Whether to make a deep clone.}
}
\if{html}{\out{
}}
}
}
}
webmockr/man/build_crul_request.Rd 0000644 0001762 0000144 00000000507 14113773445 017034 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/adapter-crul.R
\name{build_crul_request}
\alias{build_crul_request}
\title{Build a crul request}
\usage{
build_crul_request(x)
}
\arguments{
\item{x}{an unexecuted crul request object}
}
\value{
a crul request
}
\description{
Build a crul request
}
webmockr/man/request_registry.Rd 0000644 0001762 0000144 00000002612 14113773445 016557 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/request_registry.R
\name{request_registry}
\alias{request_registry}
\alias{request_registry_clear}
\title{List or clear requests in the request registry}
\usage{
request_registry()
request_registry_clear()
}
\value{
an object of class \code{RequestRegistry}, print method gives the
requests in the registry and the number of times each one has been
performed
}
\description{
List or clear requests in the request registry
}
\details{
\code{request_registry()} lists the requests that have been made
that webmockr knows about; \code{request_registry_clear()} resets the
request registry (removes all recorded requests)
}
\examples{
webmockr::enable()
stub_request("get", "https://httpbin.org/get") \%>\%
to_return(body = "success!", status = 200)
# nothing in the request registry
request_registry()
# make the request
z <- crul::HttpClient$new(url = "https://httpbin.org")$get("get")
# check the request registry - the request was made 1 time
request_registry()
# do the request again
z <- crul::HttpClient$new(url = "https://httpbin.org")$get("get")
# check the request registry - now it's been made 2 times, yay!
request_registry()
# clear the request registry
request_registry_clear()
webmockr::disable()
}
\seealso{
Other request-registry:
\code{\link{HashCounter}},
\code{\link{RequestRegistry}}
}
\concept{request-registry}
webmockr/man/webmockr-defunct.Rd 0000644 0001762 0000144 00000001121 14113773445 016370 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/defunct.R
\name{webmockr-defunct}
\alias{webmockr-defunct}
\title{Defunct functions in \pkg{webmockr}}
\description{
\itemize{
\item \code{\link[=webmockr_enable]{webmockr_enable()}}: Function removed, see \code{\link[=enable]{enable()}}
\item \code{\link[=webmockr_disable]{webmockr_disable()}}: Function removed, see \code{\link[=disable]{disable()}}
\item \link{to_return_}: Only \code{\link[=to_return]{to_return()}} is available now
\item \link{wi_th_}: Only \code{\link[=wi_th]{wi_th()}} is available now
}
}
webmockr/man/stub_registry_clear.Rd 0000644 0001762 0000144 00000001100 14113773445 017201 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/stub_registry_clear.R
\name{stub_registry_clear}
\alias{stub_registry_clear}
\title{stub_registry_clear}
\usage{
stub_registry_clear()
}
\value{
an empty list invisibly
}
\description{
Clear all stubs in the stub registry
}
\examples{
(x <- stub_request("get", "https://httpbin.org/get"))
stub_registry()
stub_registry_clear()
stub_registry()
}
\seealso{
Other stub-registry:
\code{\link{StubRegistry}},
\code{\link{remove_request_stub}()},
\code{\link{stub_registry}()}
}
\concept{stub-registry}
webmockr/man/pipe.Rd 0000644 0001762 0000144 00000000317 14113773445 014074 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/pipe.R
\name{\%>\%}
\alias{\%>\%}
\title{Pipe operator}
\usage{
lhs \%>\% rhs
}
\description{
Pipe operator
}
\keyword{internal}
webmockr/man/mock_file.Rd 0000644 0001762 0000144 00000000752 14113773445 015072 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/mock_file.R
\name{mock_file}
\alias{mock_file}
\title{Mock file}
\usage{
mock_file(path, payload)
}
\arguments{
\item{path}{(character) a file path. required}
\item{payload}{(character) string to be written to the file given
at \code{path} parameter. required}
}
\value{
a list with S3 class \code{mock_file}
}
\description{
Mock file
}
\examples{
mock_file(path = tempfile(), payload = "{\"foo\": \"bar\"}")
}
webmockr/man/RequestRegistry.Rd 0000644 0001762 0000144 00000011715 14336066705 016325 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/RequestRegistry.R
\name{RequestRegistry}
\alias{RequestRegistry}
\title{RequestRegistry}
\description{
keeps track of HTTP requests
}
\examples{
x <- RequestRegistry$new()
z1 <- RequestSignature$new("get", "http://scottchamberlain.info")
z2 <- RequestSignature$new("post", "https://httpbin.org/post")
x$register_request(request = z1)
x$register_request(request = z1)
x$register_request(request = z2)
# print method to list requests
x
# more complex requests
w <- RequestSignature$new(
method = "get",
uri = "https:/httpbin.org/get",
options = list(headers = list(`User-Agent` = "foobar", stuff = "things"))
)
w$to_s()
x$register_request(request = w)
x
# hashes, and number of times each requested
x$request_signatures$hash
# times_executed method
pat <- RequestPattern$new(
method = "get",
uri = "https:/httpbin.org/get",
headers = list(`User-Agent` = "foobar", stuff = "things")
)
pat$to_s()
x$times_executed(pat)
z <- RequestPattern$new(method = "get", uri = "http://scottchamberlain.info")
x$times_executed(z)
w <- RequestPattern$new(method = "post", uri = "https://httpbin.org/post")
x$times_executed(w)
## pattern with no matches - returns 0 (zero)
pat <- RequestPattern$new(
method = "get",
uri = "http://recology.info/"
)
pat$to_s()
x$times_executed(pat)
# reset the request registry
x$reset()
}
\seealso{
\code{\link[=stub_registry]{stub_registry()}} and \link{StubRegistry}
Other request-registry:
\code{\link{HashCounter}},
\code{\link{request_registry}()}
}
\concept{request-registry}
\section{Public fields}{
\if{html}{\out{}}
\describe{
\item{\code{request_signatures}}{a HashCounter object}
}
\if{html}{\out{
}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-RequestRegistry-print}{\code{RequestRegistry$print()}}
\item \href{#method-RequestRegistry-reset}{\code{RequestRegistry$reset()}}
\item \href{#method-RequestRegistry-register_request}{\code{RequestRegistry$register_request()}}
\item \href{#method-RequestRegistry-times_executed}{\code{RequestRegistry$times_executed()}}
\item \href{#method-RequestRegistry-clone}{\code{RequestRegistry$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-RequestRegistry-print}{}}}
\subsection{Method \code{print()}}{
print method for the \code{RequestRegistry} class
\subsection{Usage}{
\if{html}{\out{}}\preformatted{RequestRegistry$print(x, ...)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{x}}{self}
\item{\code{...}}{ignored}
}
\if{html}{\out{
}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-RequestRegistry-reset}{}}}
\subsection{Method \code{reset()}}{
Reset the registry to no registered requests
\subsection{Usage}{
\if{html}{\out{}}\preformatted{RequestRegistry$reset()}\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; ressets registry to no requests
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-RequestRegistry-register_request}{}}}
\subsection{Method \code{register_request()}}{
Register a request
\subsection{Usage}{
\if{html}{\out{}}\preformatted{RequestRegistry$register_request(request)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{request}}{a character string of the request, serialized from
a \code{RequestSignature$new(...)$to_s()}}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; registers the request
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-RequestRegistry-times_executed}{}}}
\subsection{Method \code{times_executed()}}{
How many times has a request been made
\subsection{Usage}{
\if{html}{\out{}}\preformatted{RequestRegistry$times_executed(request_pattern)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{request_pattern}}{an object of class \code{RequestPattern}}
}
\if{html}{\out{
}}
}
\subsection{Details}{
if no match is found for the request pattern, 0 is returned
}
\subsection{Returns}{
integer, the number of times the request has been made
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-RequestRegistry-clone}{}}}
\subsection{Method \code{clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{}}\preformatted{RequestRegistry$clone(deep = FALSE)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{deep}}{Whether to make a deep clone.}
}
\if{html}{\out{
}}
}
}
}
webmockr/man/webmockr_configure.Rd 0000644 0001762 0000144 00000003645 14370732366 017022 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/webmockr-opts.R
\name{webmockr_configure}
\alias{webmockr_configure}
\alias{webmockr_configure_reset}
\alias{webmockr_configuration}
\alias{webmockr_allow_net_connect}
\alias{webmockr_disable_net_connect}
\alias{webmockr_net_connect_allowed}
\title{webmockr configuration}
\usage{
webmockr_configure(
allow_net_connect = FALSE,
allow_localhost = FALSE,
allow = NULL,
show_stubbing_instructions = TRUE
)
webmockr_configure_reset()
webmockr_configuration()
webmockr_allow_net_connect()
webmockr_disable_net_connect(allow = NULL)
webmockr_net_connect_allowed(uri = NULL)
}
\arguments{
\item{allow_net_connect}{(logical) Default: \code{FALSE}}
\item{allow_localhost}{(logical) Default: \code{FALSE}}
\item{allow}{(character) one or more URI/URL to allow (and by extension
all others are not allowed)}
\item{show_stubbing_instructions}{(logical) Default: \code{TRUE}. If \code{FALSE},
stubbing instructions are not shown}
\item{uri}{(character) a URI/URL as a character string - to determine
whether or not it is allowed}
}
\description{
webmockr configuration
}
\section{webmockr_allow_net_connect}{
If there are stubs found for a request, even if net connections are
allowed (by running \code{webmockr_allow_net_connect()}) the stubbed
response will be returned. If no stub is found, and net connections
are allowed, then a real HTTP request can be made.
}
\examples{
\dontrun{
webmockr_configure()
webmockr_configure(
allow_localhost = TRUE
)
webmockr_configuration()
webmockr_configure_reset()
webmockr_allow_net_connect()
webmockr_net_connect_allowed()
# disable net connect for any URIs
webmockr_disable_net_connect()
### gives NULL with no URI passed
webmockr_net_connect_allowed()
# disable net connect EXCEPT FOR given URIs
webmockr_disable_net_connect(allow = "google.com")
### is a specific URI allowed?
webmockr_net_connect_allowed("google.com")
}
}
webmockr/man/BodyPattern.Rd 0000644 0001762 0000144 00000007070 14361011740 015361 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/RequestPattern.R
\name{BodyPattern}
\alias{BodyPattern}
\title{BodyPattern}
\description{
body matcher
}
\examples{
# make a request signature
bb <- RequestSignature$new(
method = "get",
uri = "https:/httpbin.org/get",
options = list(
body = list(foo = "bar", a = 5)
)
)
# make body pattern object
## FALSE
z <- BodyPattern$new(pattern = list(foo = "bar"))
z$pattern
z$matches(bb$body)
## TRUE
z <- BodyPattern$new(pattern = list(foo = "bar", a = 5))
z$pattern
z$matches(bb$body)
# uploads in bodies
## upload NOT in a list
bb <- RequestSignature$new(
method = "post", uri = "https:/httpbin.org/post",
options = list(body = crul::upload(system.file("CITATION"))))
bb$body
z <- BodyPattern$new(pattern =
crul::upload(system.file("CITATION")))
z$pattern
z$matches(bb$body)
## upload in a list
bb <- RequestSignature$new(
method = "post", uri = "https:/httpbin.org/post",
options = list(body = list(y = crul::upload(system.file("CITATION")))))
bb$body
z <- BodyPattern$new(pattern =
list(y = crul::upload(system.file("CITATION"))))
z$pattern
z$matches(bb$body)
}
\keyword{internal}
\section{Public fields}{
\if{html}{\out{}}
\describe{
\item{\code{pattern}}{a list}
}
\if{html}{\out{
}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-BodyPattern-new}{\code{BodyPattern$new()}}
\item \href{#method-BodyPattern-matches}{\code{BodyPattern$matches()}}
\item \href{#method-BodyPattern-to_s}{\code{BodyPattern$to_s()}}
\item \href{#method-BodyPattern-clone}{\code{BodyPattern$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-BodyPattern-new}{}}}
\subsection{Method \code{new()}}{
Create a new \code{BodyPattern} object
\subsection{Usage}{
\if{html}{\out{}}\preformatted{BodyPattern$new(pattern)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{pattern}}{(list) a body object}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
A new \code{BodyPattern} object
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-BodyPattern-matches}{}}}
\subsection{Method \code{matches()}}{
Match a request body pattern against a pattern
\subsection{Usage}{
\if{html}{\out{}}\preformatted{BodyPattern$matches(body, content_type = "")}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{body}}{(list) the body}
\item{\code{content_type}}{(character) content type}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
a boolean
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-BodyPattern-to_s}{}}}
\subsection{Method \code{to_s()}}{
Print pattern for easy human consumption
\subsection{Usage}{
\if{html}{\out{}}\preformatted{BodyPattern$to_s()}\if{html}{\out{
}}
}
\subsection{Returns}{
a string
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-BodyPattern-clone}{}}}
\subsection{Method \code{clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{}}\preformatted{BodyPattern$clone(deep = FALSE)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{deep}}{Whether to make a deep clone.}
}
\if{html}{\out{
}}
}
}
}
webmockr/man/webmockr_crul_fetch.Rd 0000644 0001762 0000144 00000000501 14113773445 017141 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/zzz.R
\name{webmockr_crul_fetch}
\alias{webmockr_crul_fetch}
\title{execute a curl request}
\usage{
webmockr_crul_fetch(x)
}
\arguments{
\item{x}{an object}
}
\value{
a curl response
}
\description{
execute a curl request
}
\keyword{internal}
webmockr/man/StubRegistry.Rd 0000644 0001762 0000144 00000014412 14362302636 015602 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/StubRegistry.R
\name{StubRegistry}
\alias{StubRegistry}
\title{StubRegistry}
\description{
stub registry to keep track of \link{StubbedRequest} stubs
}
\examples{
\dontrun{
# Make a stub
stub1 <- StubbedRequest$new(method = "get", uri = "api.crossref.org")
stub1$with(headers = list('User-Agent' = 'R'))
stub1$to_return(status = 200, body = "foobar", headers = list())
stub1
# Make another stub
stub2 <- StubbedRequest$new(method = "get", uri = "api.crossref.org")
stub2
# Put both stubs in the stub registry
reg <- StubRegistry$new()
reg$register_stub(stub = stub1)
reg$register_stub(stub = stub2)
reg
reg$request_stubs
}
}
\seealso{
Other stub-registry:
\code{\link{remove_request_stub}()},
\code{\link{stub_registry_clear}()},
\code{\link{stub_registry}()}
}
\concept{stub-registry}
\section{Public fields}{
\if{html}{\out{}}
\describe{
\item{\code{request_stubs}}{(list) list of request stubs}
\item{\code{global_stubs}}{(list) list of global stubs}
}
\if{html}{\out{
}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-StubRegistry-print}{\code{StubRegistry$print()}}
\item \href{#method-StubRegistry-register_stub}{\code{StubRegistry$register_stub()}}
\item \href{#method-StubRegistry-find_stubbed_request}{\code{StubRegistry$find_stubbed_request()}}
\item \href{#method-StubRegistry-request_stub_for}{\code{StubRegistry$request_stub_for()}}
\item \href{#method-StubRegistry-remove_request_stub}{\code{StubRegistry$remove_request_stub()}}
\item \href{#method-StubRegistry-remove_all_request_stubs}{\code{StubRegistry$remove_all_request_stubs()}}
\item \href{#method-StubRegistry-is_registered}{\code{StubRegistry$is_registered()}}
\item \href{#method-StubRegistry-clone}{\code{StubRegistry$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-StubRegistry-print}{}}}
\subsection{Method \code{print()}}{
print method for the \code{StubRegistry} class
\subsection{Usage}{
\if{html}{\out{}}\preformatted{StubRegistry$print(x, ...)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{x}}{self}
\item{\code{...}}{ignored}
}
\if{html}{\out{
}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-StubRegistry-register_stub}{}}}
\subsection{Method \code{register_stub()}}{
Register a stub
\subsection{Usage}{
\if{html}{\out{}}\preformatted{StubRegistry$register_stub(stub)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{stub}}{an object of type \link{StubbedRequest}}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; registers the stub
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-StubRegistry-find_stubbed_request}{}}}
\subsection{Method \code{find_stubbed_request()}}{
Find a stubbed request
\subsection{Usage}{
\if{html}{\out{}}\preformatted{StubRegistry$find_stubbed_request(req)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{req}}{an object of class \link{RequestSignature}}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
an object of type \link{StubbedRequest}, if matched
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-StubRegistry-request_stub_for}{}}}
\subsection{Method \code{request_stub_for()}}{
Find a stubbed request
\subsection{Usage}{
\if{html}{\out{}}\preformatted{StubRegistry$request_stub_for(request_signature, count = TRUE)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{request_signature}}{an object of class \link{RequestSignature}}
\item{\code{count}}{(bool) iterate counter or not. default: \code{TRUE}}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
logical, 1 or more
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-StubRegistry-remove_request_stub}{}}}
\subsection{Method \code{remove_request_stub()}}{
Remove a stubbed request by matching request signature
\subsection{Usage}{
\if{html}{\out{}}\preformatted{StubRegistry$remove_request_stub(stub)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{stub}}{an object of type \link{StubbedRequest}}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; removes the stub from the registry
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-StubRegistry-remove_all_request_stubs}{}}}
\subsection{Method \code{remove_all_request_stubs()}}{
Remove all request stubs
\subsection{Usage}{
\if{html}{\out{}}\preformatted{StubRegistry$remove_all_request_stubs()}\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; removes all request stubs
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-StubRegistry-is_registered}{}}}
\subsection{Method \code{is_registered()}}{
Find a stubbed request
\subsection{Usage}{
\if{html}{\out{}}\preformatted{StubRegistry$is_registered(x)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{x}}{an object of class \link{RequestSignature}}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; registers the stub
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-StubRegistry-clone}{}}}
\subsection{Method \code{clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{}}\preformatted{StubRegistry$clone(deep = FALSE)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{deep}}{Whether to make a deep clone.}
}
\if{html}{\out{
}}
}
}
}
webmockr/man/webmockr_reset.Rd 0000644 0001762 0000144 00000001211 14113773445 016144 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/webmockr_reset.R
\name{webmockr_reset}
\alias{webmockr_reset}
\title{webmockr_reset}
\usage{
webmockr_reset()
}
\value{
nothing
}
\description{
Clear all stubs and the request counter
}
\details{
this function runs \code{\link[=stub_registry_clear]{stub_registry_clear()}} and
\code{\link[=request_registry_clear]{request_registry_clear()}} - so you can run those two yourself
to achieve the same thing
}
\examples{
# webmockr_reset()
}
\seealso{
\code{\link[=stub_registry_clear]{stub_registry_clear()}} \code{\link[=request_registry_clear]{request_registry_clear()}}
}
webmockr/man/HttpLibAdapaterRegistry.Rd 0000644 0001762 0000144 00000004724 14336066705 017707 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/HttpLibAdapterRegistry.R
\name{HttpLibAdapaterRegistry}
\alias{HttpLibAdapaterRegistry}
\title{HttpLibAdapaterRegistry}
\description{
http lib adapter registry
}
\examples{
x <- HttpLibAdapaterRegistry$new()
x$register(CrulAdapter$new())
x
x$adapters
x$adapters[[1]]$name
}
\section{Public fields}{
\if{html}{\out{}}
\describe{
\item{\code{adapters}}{list}
}
\if{html}{\out{
}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-HttpLibAdapaterRegistry-print}{\code{HttpLibAdapaterRegistry$print()}}
\item \href{#method-HttpLibAdapaterRegistry-register}{\code{HttpLibAdapaterRegistry$register()}}
\item \href{#method-HttpLibAdapaterRegistry-clone}{\code{HttpLibAdapaterRegistry$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-HttpLibAdapaterRegistry-print}{}}}
\subsection{Method \code{print()}}{
print method for the \code{HttpLibAdapaterRegistry} class
\subsection{Usage}{
\if{html}{\out{}}\preformatted{HttpLibAdapaterRegistry$print(x, ...)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{x}}{self}
\item{\code{...}}{ignored}
}
\if{html}{\out{
}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-HttpLibAdapaterRegistry-register}{}}}
\subsection{Method \code{register()}}{
Register an http library adapter
\subsection{Usage}{
\if{html}{\out{}}\preformatted{HttpLibAdapaterRegistry$register(x)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{x}}{an http lib adapter, e.g., \link{CrulAdapter}}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
nothing, registers the library adapter
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-HttpLibAdapaterRegistry-clone}{}}}
\subsection{Method \code{clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{}}\preformatted{HttpLibAdapaterRegistry$clone(deep = FALSE)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{deep}}{Whether to make a deep clone.}
}
\if{html}{\out{
}}
}
}
}
webmockr/man/StubbedRequest.Rd 0000644 0001762 0000144 00000021271 14362302441 016070 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/StubbedRequest.R
\name{StubbedRequest}
\alias{StubbedRequest}
\title{StubbedRequest}
\description{
stubbed request class underlying \code{\link[=stub_request]{stub_request()}}
}
\examples{
\dontrun{
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()
# 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
}
}
\seealso{
\code{\link[=stub_request]{stub_request()}}
}
\section{Public fields}{
\if{html}{\out{}}
\describe{
\item{\code{method}}{(xx) xx}
\item{\code{uri}}{(xx) xx}
\item{\code{uri_regex}}{(xx) xx}
\item{\code{uri_parts}}{(xx) xx}
\item{\code{host}}{(xx) xx}
\item{\code{query}}{(xx) xx}
\item{\code{body}}{(xx) xx}
\item{\code{basic_auth}}{(xx) xx}
\item{\code{request_headers}}{(xx) xx}
\item{\code{response_headers}}{(xx) xx}
\item{\code{responses_sequences}}{(xx) xx}
\item{\code{status_code}}{(xx) xx}
\item{\code{counter}}{a StubCounter object}
}
\if{html}{\out{
}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-StubbedRequest-new}{\code{StubbedRequest$new()}}
\item \href{#method-StubbedRequest-print}{\code{StubbedRequest$print()}}
\item \href{#method-StubbedRequest-with}{\code{StubbedRequest$with()}}
\item \href{#method-StubbedRequest-to_return}{\code{StubbedRequest$to_return()}}
\item \href{#method-StubbedRequest-to_timeout}{\code{StubbedRequest$to_timeout()}}
\item \href{#method-StubbedRequest-to_raise}{\code{StubbedRequest$to_raise()}}
\item \href{#method-StubbedRequest-to_s}{\code{StubbedRequest$to_s()}}
\item \href{#method-StubbedRequest-reset}{\code{StubbedRequest$reset()}}
\item \href{#method-StubbedRequest-clone}{\code{StubbedRequest$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-StubbedRequest-new}{}}}
\subsection{Method \code{new()}}{
Create a new \code{StubbedRequest} object
\subsection{Usage}{
\if{html}{\out{}}\preformatted{StubbedRequest$new(method, uri = NULL, uri_regex = NULL)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{method}}{the HTTP method (any, head, get, post, put,
patch, or delete). "any" matches any HTTP method. required.}
\item{\code{uri}}{(character) request URI. either this or \code{uri_regex}
required. \pkg{webmockr} can match uri's without the "http" scheme,
but does not match if the scheme is "https". required, unless
\code{uri_regex} given. See \link{UriPattern} for more.}
\item{\code{uri_regex}}{(character) request URI as regex. either this or \code{uri}
required}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
A new \code{StubbedRequest} object
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-StubbedRequest-print}{}}}
\subsection{Method \code{print()}}{
print method for the \code{StubbedRequest} class
\subsection{Usage}{
\if{html}{\out{}}\preformatted{StubbedRequest$print(x, ...)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{x}}{self}
\item{\code{...}}{ignored}
}
\if{html}{\out{
}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-StubbedRequest-with}{}}}
\subsection{Method \code{with()}}{
Set expectations for what's given in HTTP request
\subsection{Usage}{
\if{html}{\out{}}\preformatted{StubbedRequest$with(
query = NULL,
body = NULL,
headers = NULL,
basic_auth = NULL
)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{query}}{(list) request query params, as a named list. optional}
\item{\code{body}}{(list) request body, as a named list. optional}
\item{\code{headers}}{(list) request headers as a named list. optional.}
\item{\code{basic_auth}}{(character) basic authentication. optional.}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; sets only
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-StubbedRequest-to_return}{}}}
\subsection{Method \code{to_return()}}{
Set expectations for what's returned in HTTP response
\subsection{Usage}{
\if{html}{\out{}}\preformatted{StubbedRequest$to_return(status, body, headers)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{status}}{(numeric) an HTTP status code}
\item{\code{body}}{(list) response body, one of: \code{character}, \code{json},
\code{list}, \code{raw}, \code{numeric}, \code{NULL}, \code{FALSE}, or a file connection
(other connetion types not supported)}
\item{\code{headers}}{(list) named list, response headers. optional.}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; sets whats to be returned
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-StubbedRequest-to_timeout}{}}}
\subsection{Method \code{to_timeout()}}{
Response should time out
\subsection{Usage}{
\if{html}{\out{}}\preformatted{StubbedRequest$to_timeout()}\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-StubbedRequest-to_raise}{}}}
\subsection{Method \code{to_raise()}}{
Response should raise an exception \code{x}
\subsection{Usage}{
\if{html}{\out{}}\preformatted{StubbedRequest$to_raise(x)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{x}}{(character) an exception message}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-StubbedRequest-to_s}{}}}
\subsection{Method \code{to_s()}}{
Response as a character string
\subsection{Usage}{
\if{html}{\out{}}\preformatted{StubbedRequest$to_s()}\if{html}{\out{
}}
}
\subsection{Returns}{
(character) the response as a string
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-StubbedRequest-reset}{}}}
\subsection{Method \code{reset()}}{
Reset the counter for the stub
\subsection{Usage}{
\if{html}{\out{}}\preformatted{StubbedRequest$reset()}\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; resets stub counter to no requests
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-StubbedRequest-clone}{}}}
\subsection{Method \code{clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{}}\preformatted{StubbedRequest$clone(deep = FALSE)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{deep}}{Whether to make a deep clone.}
}
\if{html}{\out{
}}
}
}
}
webmockr/man/MethodPattern.Rd 0000644 0001762 0000144 00000005762 14336066705 015727 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/RequestPattern.R
\name{MethodPattern}
\alias{MethodPattern}
\title{MethodPattern}
\description{
method matcher
}
\details{
Matches regardless of case. e.g., POST will match to post
}
\examples{
(x <- MethodPattern$new(pattern = "post"))
x$pattern
x$matches(method = "post")
x$matches(method = "POST")
# all matches() calls should be TRUE
(x <- MethodPattern$new(pattern = "any"))
x$pattern
x$matches(method = "post")
x$matches(method = "GET")
x$matches(method = "HEAD")
}
\keyword{internal}
\section{Public fields}{
\if{html}{\out{}}
\describe{
\item{\code{pattern}}{(character) an http method}
}
\if{html}{\out{
}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-MethodPattern-new}{\code{MethodPattern$new()}}
\item \href{#method-MethodPattern-matches}{\code{MethodPattern$matches()}}
\item \href{#method-MethodPattern-to_s}{\code{MethodPattern$to_s()}}
\item \href{#method-MethodPattern-clone}{\code{MethodPattern$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-MethodPattern-new}{}}}
\subsection{Method \code{new()}}{
Create a new \code{MethodPattern} object
\subsection{Usage}{
\if{html}{\out{}}\preformatted{MethodPattern$new(pattern)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{pattern}}{(character) a HTTP method, lowercase}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
A new \code{MethodPattern} object
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-MethodPattern-matches}{}}}
\subsection{Method \code{matches()}}{
test if the pattern matches a given http method
\subsection{Usage}{
\if{html}{\out{}}\preformatted{MethodPattern$matches(method)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{method}}{(character) a HTTP method, lowercase}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
a boolean
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-MethodPattern-to_s}{}}}
\subsection{Method \code{to_s()}}{
Print pattern for easy human consumption
\subsection{Usage}{
\if{html}{\out{}}\preformatted{MethodPattern$to_s()}\if{html}{\out{
}}
}
\subsection{Returns}{
a string
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-MethodPattern-clone}{}}}
\subsection{Method \code{clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{}}\preformatted{MethodPattern$clone(deep = FALSE)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{deep}}{Whether to make a deep clone.}
}
\if{html}{\out{
}}
}
}
}
webmockr/man/HeadersPattern.Rd 0000644 0001762 0000144 00000010457 14336066705 016057 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/RequestPattern.R
\name{HeadersPattern}
\alias{HeadersPattern}
\title{HeadersPattern}
\description{
headers matcher
}
\details{
\code{webmockr} normalises headers and treats all forms of same headers as equal:
i.e the following two sets of headers are equal:
\code{list(Header1 = "value1", content_length = 123, X_CuStOm_hEAder = "foo")}
and
\code{list(header1 = "value1", "Content-Length" = 123, "x-cuSTOM-HeAder" = "foo")}
}
\examples{
(x <- HeadersPattern$new(pattern = list(a = 5)))
x$pattern
x$matches(list(a = 5))
# different cases
(x <- HeadersPattern$new(pattern = list(Header1 = "value1")))
x$pattern
x$matches(list(header1 = "value1"))
x$matches(list(header1 = "value2"))
# different symbols
(x <- HeadersPattern$new(pattern = list(`Hello_World` = "yep")))
x$pattern
x$matches(list(`hello-world` = "yep"))
x$matches(list(`hello-worlds` = "yep"))
headers <- list(
'User-Agent' = 'Apple',
'Accept-Encoding' = 'gzip, deflate',
'Accept' = 'application/json, text/xml, application/xml, */*')
(x <- HeadersPattern$new(pattern = headers))
x$to_s()
x$pattern
x$matches(headers)
}
\keyword{internal}
\section{Public fields}{
\if{html}{\out{}}
\describe{
\item{\code{pattern}}{a list}
}
\if{html}{\out{
}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-HeadersPattern-new}{\code{HeadersPattern$new()}}
\item \href{#method-HeadersPattern-matches}{\code{HeadersPattern$matches()}}
\item \href{#method-HeadersPattern-empty_headers}{\code{HeadersPattern$empty_headers()}}
\item \href{#method-HeadersPattern-to_s}{\code{HeadersPattern$to_s()}}
\item \href{#method-HeadersPattern-clone}{\code{HeadersPattern$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-HeadersPattern-new}{}}}
\subsection{Method \code{new()}}{
Create a new \code{HeadersPattern} object
\subsection{Usage}{
\if{html}{\out{}}\preformatted{HeadersPattern$new(pattern)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{pattern}}{(list) a pattern, as a named list, must be named,
e.g,. \code{list(a = 5, b = 6)}}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
A new \code{HeadersPattern} object
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-HeadersPattern-matches}{}}}
\subsection{Method \code{matches()}}{
Match a list of headers against that stored
\subsection{Usage}{
\if{html}{\out{}}\preformatted{HeadersPattern$matches(headers)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{headers}}{(list) named list of headers, e.g,. \code{list(a = 5, b = 6)}}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
a boolean
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-HeadersPattern-empty_headers}{}}}
\subsection{Method \code{empty_headers()}}{
Are headers empty? tests if null or length==0
\subsection{Usage}{
\if{html}{\out{}}\preformatted{HeadersPattern$empty_headers(headers)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{headers}}{named list of headers}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
a boolean
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-HeadersPattern-to_s}{}}}
\subsection{Method \code{to_s()}}{
Print pattern for easy human consumption
\subsection{Usage}{
\if{html}{\out{}}\preformatted{HeadersPattern$to_s()}\if{html}{\out{
}}
}
\subsection{Returns}{
a string
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-HeadersPattern-clone}{}}}
\subsection{Method \code{clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{}}\preformatted{HeadersPattern$clone(deep = FALSE)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{deep}}{Whether to make a deep clone.}
}
\if{html}{\out{
}}
}
}
}
webmockr/man/RequestSignature.Rd 0000644 0001762 0000144 00000010232 14336066705 016447 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/RequestSignature.R
\name{RequestSignature}
\alias{RequestSignature}
\title{RequestSignature}
\description{
General purpose request signature builder
}
\examples{
# make request signature
x <- RequestSignature$new(method = "get", uri = "https:/httpbin.org/get")
# method
x$method
# uri
x$uri
# request signature to string
x$to_s()
# headers
w <- RequestSignature$new(
method = "get",
uri = "https:/httpbin.org/get",
options = list(headers = list(`User-Agent` = "foobar", stuff = "things"))
)
w
w$headers
w$to_s()
# headers and body
bb <- RequestSignature$new(
method = "get",
uri = "https:/httpbin.org/get",
options = list(
headers = list(`User-Agent` = "foobar", stuff = "things"),
body = list(a = "tables")
)
)
bb
bb$headers
bb$body
bb$to_s()
# with disk path
f <- tempfile()
bb <- RequestSignature$new(
method = "get",
uri = "https:/httpbin.org/get",
options = list(disk = f)
)
bb
bb$disk
bb$to_s()
}
\section{Public fields}{
\if{html}{\out{}}
\describe{
\item{\code{method}}{(character) an http method}
\item{\code{uri}}{(character) a uri}
\item{\code{body}}{(various) request body}
\item{\code{headers}}{(list) named list of headers}
\item{\code{proxies}}{(list) proxies as a named list}
\item{\code{auth}}{(list) authentication details, as a named list}
\item{\code{url}}{internal use}
\item{\code{disk}}{(character) if writing to disk, the path}
\item{\code{fields}}{(various) request body details}
\item{\code{output}}{(various) request output details, disk, memory, etc}
}
\if{html}{\out{
}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-RequestSignature-new}{\code{RequestSignature$new()}}
\item \href{#method-RequestSignature-print}{\code{RequestSignature$print()}}
\item \href{#method-RequestSignature-to_s}{\code{RequestSignature$to_s()}}
\item \href{#method-RequestSignature-clone}{\code{RequestSignature$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-RequestSignature-new}{}}}
\subsection{Method \code{new()}}{
Create a new \code{RequestSignature} object
\subsection{Usage}{
\if{html}{\out{}}\preformatted{RequestSignature$new(method, uri, options = list())}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{method}}{the HTTP method (any, head, options, get, post, put,
patch, trace, or delete). "any" matches any HTTP method. required.}
\item{\code{uri}}{(character) request URI. required.}
\item{\code{options}}{(list) options. optional. See Details.}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
A new \code{RequestSignature} object
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-RequestSignature-print}{}}}
\subsection{Method \code{print()}}{
print method for the \code{RequestSignature} class
\subsection{Usage}{
\if{html}{\out{}}\preformatted{RequestSignature$print()}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{x}}{self}
\item{\code{...}}{ignored}
}
\if{html}{\out{
}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-RequestSignature-to_s}{}}}
\subsection{Method \code{to_s()}}{
Request signature to a string
\subsection{Usage}{
\if{html}{\out{}}\preformatted{RequestSignature$to_s()}\if{html}{\out{
}}
}
\subsection{Returns}{
a character string representation of the request signature
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-RequestSignature-clone}{}}}
\subsection{Method \code{clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{}}\preformatted{RequestSignature$clone(deep = FALSE)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{deep}}{Whether to make a deep clone.}
}
\if{html}{\out{
}}
}
}
}
webmockr/man/remove_request_stub.Rd 0000644 0001762 0000144 00000001247 14113773445 017244 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/remove_request_stub.R
\name{remove_request_stub}
\alias{remove_request_stub}
\title{Remove a request stub}
\usage{
remove_request_stub(stub)
}
\arguments{
\item{stub}{a request stub, of class \code{StubbedRequest}}
}
\value{
logical, \code{TRUE} if removed, \code{FALSE} if not removed
}
\description{
Remove a request stub
}
\examples{
(x <- stub_request("get", "https://httpbin.org/get"))
stub_registry()
remove_request_stub(x)
stub_registry()
}
\seealso{
Other stub-registry:
\code{\link{StubRegistry}},
\code{\link{stub_registry_clear}()},
\code{\link{stub_registry}()}
}
\concept{stub-registry}
webmockr/DESCRIPTION 0000644 0001762 0000144 00000003403 14377415642 013606 0 ustar ligges users Package: webmockr
Title: Stubbing and Setting Expectations on 'HTTP' Requests
Description: Stubbing and setting expectations on 'HTTP' requests.
Includes tools for stubbing 'HTTP' requests, including expected
request conditions and response conditions. Match on
'HTTP' method, query parameters, request body, headers and
more. Can be used for unit tests or outside of a testing
context.
Version: 0.9.0
Authors@R: c(
person("Scott", "Chamberlain", role = c("aut", "cre"), email =
"myrmecocystus+r@gmail.com", comment = c(ORCID="0000-0003-1444-9135")),
person("Aaron", "Wolen", role = "ctb",
comment = c(ORCID="0000-0003-2542-2202")),
person("rOpenSci", role = "fnd", comment = "https://ropensci.org")
)
License: MIT + file LICENSE
URL: https://github.com/ropensci/webmockr (devel)
https://books.ropensci.org/http-testing/ (user manual)
https://docs.ropensci.org/webmockr/ (documentation)
BugReports: https://github.com/ropensci/webmockr/issues
Encoding: UTF-8
Language: en-US
Imports: curl, jsonlite, magrittr (>= 1.5), R6 (>= 2.1.3), urltools (>=
1.6.0), fauxpas, crul (>= 0.7.0), base64enc
Suggests: testthat, xml2, vcr, httr
RoxygenNote: 7.2.3
X-schema.org-applicationCategory: Web
X-schema.org-keywords: http, https, API, web-services, curl, mock,
mocking, fakeweb, http-mocking, testing, testing-tools, tdd
X-schema.org-isPartOf: https://ropensci.org
NeedsCompilation: no
Packaged: 2023-02-28 14:32:51 UTC; sckott
Author: Scott Chamberlain [aut, cre] (),
Aaron Wolen [ctb] (),
rOpenSci [fnd] (https://ropensci.org)
Maintainer: Scott Chamberlain
Repository: CRAN
Date/Publication: 2023-02-28 15:20:02 UTC
webmockr/tests/ 0000755 0001762 0000144 00000000000 14113773445 013236 5 ustar ligges users webmockr/tests/test-all.R 0000644 0001762 0000144 00000000053 14113773445 015104 0 ustar ligges users library("testthat")
test_check("webmockr")
webmockr/tests/testthat/ 0000755 0001762 0000144 00000000000 14377303202 015066 5 ustar ligges users webmockr/tests/testthat/test-StubbedRequest.R 0000644 0001762 0000144 00000014005 14113773445 021137 0 ustar ligges users context("StubbedRequest")
test_that("StubbedRequest: works", {
expect_is(StubbedRequest, "R6ClassGenerator")
aa <- StubbedRequest$new(method = "get", uri = "https:/httpbin.org/get")
expect_is(aa, "StubbedRequest")
expect_null(aa$host)
expect_null(aa$query)
expect_null(aa$body)
expect_null(aa$request_headers)
expect_null(aa$response_headers)
expect_null(aa$response)
expect_null(aa$response_sequences)
expect_is(aa$method, "character")
expect_equal(aa$method, "get")
expect_is(aa$uri, "character")
expect_equal(aa$uri, "https:/httpbin.org/get")
expect_is(aa$uri_parts, "list")
expect_equal(aa$uri_parts$domain, "https")
expect_equal(aa$uri_parts$path, "httpbin.org/get")
expect_is(aa$to_s, "function")
expect_equal(aa$to_s(), "GET: https:/httpbin.org/get")
# with
expect_is(aa$with, "function")
expect_null(aa$query)
aa$with(query = list(foo = "bar"))
expect_is(aa$query, "list")
expect_named(aa$query, "foo")
expect_equal(aa$to_s(), "GET: https:/httpbin.org/get?foo=bar")
## >1 query param gets combined with "&" and not ","
aa$with(query = list(foo = "bar", stuff = 567))
expect_equal(sort(names(aa$query)), c("foo", "stuff"))
expect_equal(aa$to_s(), "GET: https:/httpbin.org/get?foo=bar&stuff=567")
# to_return
expect_is(aa$to_return, "function")
expect_null(aa$body)
aa$to_return(
status = 404,
body = list(hello = "world"),
headers = list(a = 5)
)
expect_is(aa$responses_sequences, "list")
expect_is(aa$responses_sequences[[1]]$body, "list")
expect_named(aa$responses_sequences[[1]]$body, "hello")
})
test_that("StubbedRequest: to_timeout", {
x <- StubbedRequest$new(method = "get", uri = "https:/httpbin.org/get")
expect_false(grepl("should_timeout: TRUE", x$to_s()))
x$to_timeout()
expect_true(grepl("should_timeout: TRUE", x$to_s()))
})
library("fauxpas")
test_that("StubbedRequest: to_raise", {
x <- StubbedRequest$new(method = "get", uri = "https:/httpbin.org/get")
expect_false(grepl("to_raise: HTTPBadGateway", x$to_s()))
x$to_raise(HTTPBadGateway)
expect_true(grepl("to_raise: HTTPBadGateway", x$to_s()))
## many exceptions
x$to_raise(list(HTTPBadGateway, HTTPForbidden, HTTPInsufficientStorage))
expect_true(
grepl("to_raise: HTTPBadGateway, HTTPForbidden, HTTPInsufficientStorage",
x$to_s()))
})
test_that("StubbedRequest: different methods work", {
expect_equal(
StubbedRequest$new(method = "any",
uri = "https:/httpbin.org/get")$method,
"any"
)
expect_equal(
StubbedRequest$new(method = "get",
uri = "https:/httpbin.org/get")$method,
"get"
)
expect_equal(
StubbedRequest$new(method = "head",
uri = "https:/httpbin.org/get")$method,
"head"
)
expect_equal(
StubbedRequest$new(method = "post",
uri = "https:/httpbin.org/get")$method,
"post"
)
expect_equal(
StubbedRequest$new(method = "put",
uri = "https:/httpbin.org/get")$method,
"put"
)
expect_equal(
StubbedRequest$new(method = "patch",
uri = "https:/httpbin.org/get")$method,
"patch"
)
expect_equal(
StubbedRequest$new(method = "delete",
uri = "https:/httpbin.org/get")$method,
"delete"
)
})
test_that("StubbedRequest fails well", {
# requires uri or uri_regex
expect_error(StubbedRequest$new(), "one of uri or uri_regex is required")
# method not in acceptable set
expect_error(StubbedRequest$new(method = "adf"),
"'arg' should be one of")
})
test_that("StubbedRequest long string handling", {
x <- StubbedRequest$new(method = "get", uri = "api.crossref.org")
# with
x$with(
query = list(foo = "Bar", a = 5, b = 8,
user = paste0("asdfa asldfj asdfljas dflajsd fasldjf",
" asldfja sdfljas dflajs fdlasjf aslfa fdfdsf")),
body = list(a = 5, b = 8, user = "asdfa asldfj asdfljas dflajsdfdfdsf",
foo = "Bar"),
headers = list(farm = "animal",
`User-Agent` =
"stuff things whasdlfj adsfla jsdflja sdflasj dflasj dfasljf asdf")
)
# with: long query
expect_output(x$print(), "foo=Bar, a=5, b=8, user=asdfa asldfj asdflja...")
# with: long body
expect_output(x$print(), "a=5, b=8, user=asdfa asldfj asdflja..., foo=Bar")
# with: long request headers
expect_output(x$print(), "farm=animal, User-Agent=stuff things whasdlf...")
# to_return
x$to_return(
status = 200,
body = list(name = "julia", title = "advanced user",
location = "somewhere in the middle of the earth",
foo = "Bar"),
headers = list(farm = "animal",
`User-Agent` =
"stuff things whasdlfj adsfla jsdflja sdflasj dflasj dfasljf asdf")
)
# to_return: status code
expect_output(x$print(), "200")
# to_return: long body
expect_output(x$print(),
"name=julia, title=advanced user, location=somewhere in the mid..., foo=Bar")
# to_return: long response headers
expect_output(x$print(), "farm=animal, User-Agent=stuff things whasdlf...")
})
test_that("StubbedRequest nested lists in body", {
x <- StubbedRequest$new(method = "get", uri = "api.crossref.org")
x$with(
query = list(foo = "Bar"),
headers = list(farm = "animal"),
body = list(a = list(b = list(c = "foo", d = "bar")))
)
expect_output(x$print(),
"a = list\\(b = list\\(c = \"foo\", d = \"bar\"\\)\\)")
# longer
x$with(
query = list(foo = "Bar"),
headers = list(farm = "animal"),
body = list(
apple = list(
bears = list(
cheesecake =
list(foo_do_the_thing = "bar asdjlfas dfaljsdf asljdf slf"))))
)
expect_output(x$print(),
"apple = list\\(bears = list\\(cheesecake = list\\(foo_do_the_thing = \"bar asdjlfas dfa...")
})
test_that("StubbedRequest w/ >1 to_return()", {
stub_registry_clear()
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$to_s()
expect_equal(length(x$responses_sequences), 2)
expect_match(x$to_s(), "foobar")
expect_match(x$to_s(), "bears")
})
webmockr/tests/testthat/test-uri_regex.R 0000644 0001762 0000144 00000004730 14113773445 020173 0 ustar ligges users context("uri_regex")
test_that("uri_regex with crul", {
stub_request("get", uri_regex = "httpbin.org/.+") %>%
to_return(body = list(foo = "bar"))
library(crul)
enable(adapter = "crul")
invisible(
lapply(c('elephants', 'bears', 'leaves', 'foo', 'bar'), function(z) {
expect_true(HttpClient$new("https://httpbin.org")$get(z)$success())
})
)
# more complicated regex
stub_request("get", uri_regex = "[Aa].+\\.io/apple/")
invisible(
lapply(c('Anounce', 'apple', 'Afar', 'after'), function(z) {
expect_true(HttpClient$new(sprintf("https://%s.io", z))$get("apple")$success())
expect_error(HttpClient$new(sprintf("https://%s.io", z))$get("fruit"),
"Real HTTP connections are disabled")
})
)
# regex to match any URL
## https://github.com/ropensci/webmockr/issues/113
## when matching any url with `.+`, it would lead to an empty url in response
## object, at least with crul
stub_request("get", uri_regex = ".+")
invisible(
lapply(c('Anounce', 'apple', 'Afar', 'after'), function(z) {
url <- sprintf("https://%s.io", z)
res <- HttpClient$new(url)$get(z)
expect_is(res, "HttpResponse")
expect_true(grepl(res$url, file.path(url, z), ignore.case = TRUE))
})
)
})
stub_registry_clear()
test_that("uri_regex with httr", {
stub_request("get", uri_regex = "httpbin.org/.+") %>%
to_return(body = list(foo = "bar"))
library(httr)
enable(adapter = "httr")
invisible(
lapply(c('elephants', 'bears', 'leaves', 'foo', 'bar'), function(z) {
expect_false(http_error(GET(file.path("https://httpbin.org", z))))
})
)
# more complicated regex
stub_request("get", uri_regex = "[Aa].+\\.io/apple/")
invisible(
lapply(c('Anounce', 'apple', 'Afar', 'after'), function(z) {
expect_false(http_error(GET(sprintf("https://%s.io/apple", z))))
expect_error(GET(sprintf("https://%s.io/fruit", z)),
"Real HTTP connections are disabled")
})
)
# regex to match any URL
## https://github.com/ropensci/webmockr/issues/113
## when matching any url with `.+`, it would lead to an empty url in response
## object, at least with crul
stub_request("get", uri_regex = ".+")
invisible(
lapply(c('Anounce', 'apple', 'Afar', 'after'), function(z) {
url <- sprintf("https://%s.io", z)
res <- GET(url, path = z)
expect_is(res, "response")
expect_true(grepl(res$url, file.path(url, z), ignore.case = TRUE))
})
)
})
stub_registry_clear()
webmockr/tests/testthat/test-to_return.R 0000644 0001762 0000144 00000012464 14364046756 020234 0 ustar ligges users context("to_return: works as expected")
stub_registry()$remove_all_request_stubs()
test_that("no stubs exist before stub_request called", {
expect_equal(length(stub_registry()$request_stubs), 0)
})
aa <- stub_request("get", "https://httpbin.org/get") %>%
to_return(status = 200, body = "stuff", headers = list(a = 5))
test_that("stub_request bits are correct", {
expect_is(aa, "StubbedRequest")
expect_null(aa$body)
expect_null(aa$host)
expect_null(aa$response)
expect_null(aa$query)
expect_null(aa$request_headers)
expect_is(aa$method, "character")
expect_equal(aa$method, "get")
expect_is(aa$uri, "character")
expect_equal(aa$uri, "https://httpbin.org/get")
# to_return expected stuff
expect_is(aa$response_headers, "list")
expect_named(aa$response_headers, "a")
expect_equal(aa$response_headers$a, 5)
expect_is(aa$responses_sequences, "list")
expect_identical(
sort(names(aa$responses_sequences[[1]])),
sort(c("status", "body", "headers", "body_raw",
"timeout", "raise", "exceptions"))
)
expect_equal(aa$responses_sequences[[1]]$status, 200)
expect_equal(aa$responses_sequences[[1]]$body, "stuff")
})
test_that("stubs exist after stub_request called", {
expect_equal(length(stub_registry()$request_stubs), 1)
})
test_that("stub_request fails well", {
expect_error(to_return(), "argument \".data\" is missing")
expect_error(to_return(5), ".data must be of class StubbedRequest")
zzz <- stub_request("get", "https://httpbin.org/get")
# status
expect_error(to_return(zzz, status = "foo"), "must be of class numeric")
# headers
expect_error(to_return(zzz, headers = list(5, 6)), "'headers' must be a named list")
expect_error(to_return(zzz, headers = list(a = 5, 6)), "'headers' must be a named list")
expect_error(to_return(zzz, .list = 4), ".list must be of class list")
})
stub_registry_clear()
enable()
context("to_return: response headers returned all lowercase")
test_that("to_return (response) headers are all lowercase, crul", {
stub <- stub_request(uri = "http://httpbin.org/get") %>%
to_return(headers = list("Foo-Bar" = "baz"))
cli <- crul::HttpClient$new(url = "http://httpbin.org/")
x <- cli$get("get")
expect_is(x$response_headers, "list")
expect_named(x$response_headers, "foo-bar")
})
stub_registry_clear()
test_that("to_return (response) headers are all lowercase, httr", {
loadNamespace("httr")
stub <- stub_request(uri = "http://httpbin.org/get") %>%
to_return(headers = list("Foo-Bar" = "baz"))
x <- httr::GET("http://httpbin.org/get")
expect_is(x$headers, "list")
expect_named(x$headers, "foo-bar")
})
disable()
stub_registry_clear()
enable()
context("to_return: response header values are all character")
test_that("to_return response header values are all character, crul", {
cli <- crul::HttpClient$new(url = "http://httpbin.org/")
stub_request(uri = "http://httpbin.org/get") %>%
to_return(headers = list("Foo-Bar" = 10))
x <- cli$get("get")
expect_is(x$response_headers, "list")
expect_named(x$response_headers, "foo-bar")
expect_is(x$response_headers$`foo-bar`, "character")
expect_equal(x$response_headers$`foo-bar`, "10")
stub_registry_clear()
stub_request(uri = "http://httpbin.org/get") %>%
to_return(headers = list(
a = 10, b = 234233434, c = 2344.342342,
d = "brown", e = as.factor("blue")
))
z <- cli$get("get")
expect_is(z$response_headers, "list")
expect_named(z$response_headers, letters[1:5])
invisible(
vapply(z$response_headers, function(z) expect_is(z, "character"), "")
)
expect_equal(z$response_headers$c, "2344.342342")
expect_equal(z$response_headers$e, "blue")
})
stub_registry_clear()
test_that("to_return response header values are all character, httr", {
loadNamespace("httr")
stub_request(uri = "http://httpbin.org/get") %>%
to_return(headers = list("Foo-Bar" = 10))
x <- httr::GET("http://httpbin.org/get")
expect_is(x$headers, "list")
expect_named(x$headers, "foo-bar")
expect_is(x$headers$`foo-bar`, "character")
expect_equal(x$headers$`foo-bar`, "10")
stub_registry_clear()
stub_request(uri = "http://httpbin.org/get") %>%
to_return(headers = list(
a = 10, b = 234233434, c = 2344.342342,
d = "brown", e = as.factor("blue")
))
z <- httr::GET("http://httpbin.org/get")
expect_is(z$headers, "list")
expect_named(z$headers, letters[1:5])
invisible(
vapply(z$headers, function(z) expect_is(z, "character"), "")
)
expect_equal(z$headers$c, "2344.342342")
expect_equal(z$headers$e, "blue")
})
disable()
context("to_return_: defunct")
test_that("to_return_: defunct", {
expect_error(to_return_(), "to_return", class = "error")
})
stub_to_return_status_code <- function() {
stub_registry()$request_stubs[[1]]$responses_sequences[[1]]$status
}
stub_registry_clear()
enable()
test_that("stub_request status accepts numeric or integer values", {
stub_status_type_a <- stub_request("get", "https://httpbin.org/get")
expect_s3_class(to_return(stub_status_type_a, status = 200), "StubbedRequest")
expect_type(stub_to_return_status_code(), "double") # numeric = double
stub_registry_clear()
stub_status_type_b <- stub_request("get", "https://httpbin.org/get")
expect_s3_class(to_return(stub_status_type_b, status = 200L), "StubbedRequest")
expect_type(stub_to_return_status_code(), "integer")
})
disable()
webmockr/tests/testthat/httr_obj_auth.rda 0000644 0001762 0000144 00000000557 14113773445 020431 0 ustar ligges users ‹ ]Q]OÂ0-Û`²(1ñìÁø@`‚1áŘh|7>ð¶tãÅmýø¯ýj»dnÉ]{OϽ=÷ôíy½ÖBÈAî ‡Wo=GÿzÈCC½^í¥ä1K1Qr{£A_Çu‹ä¾¾¼w ;]WŠÆfMh1¾Ã 4šFx˘N¹¾¸]5#e™Ñ”HÊ
|¬˜„NŸòl¶Ï*`ŒÇµý‚ä :ROi
¥Ô»Ÿ:3ƒ×aÝf4IÏðCt¿Œf!ŸVÙ2Z„Æ<–Ñ\óF¦æñÙ¯ÝÃ×Sª©PPÁU…ÎoCõêP àd…l€cTåqÓËÊãÆ¦æx)ª—è>W?͈°8
xyäTBœCÎøWƒjl«ŠÔZ·ü§Ô·öå ÷Ìêpµ)gI@6À…åm)d›ù¬4}m˜ÉbÉ> °t¦d©d}©k/=;ÉáS8û€_€@© webmockr/tests/testthat/test-zutils.R 0000644 0001762 0000144 00000015147 14370732366 017542 0 ustar ligges users context("util fxns: normalize_uri")
test_that("normalize_uri", {
# prunes trailing slash
expect_is(normalize_uri("example.com/"), "character")
expect_match(normalize_uri("example.com/"), "example.com")
# prunes ports 80 and 443
expect_match(normalize_uri("example.com:80"), "example.com")
expect_match(normalize_uri("example.com:443"), "example.com")
# escapes special characters
expect_match(normalize_uri("example.com/foo/bar"),
"example.com/foo%2Fbar")
expect_match(normalize_uri("example.com/foo+bar"),
"example.com/foo%2Bbar")
expect_match(normalize_uri("example.com/foo*bar"),
"example.com/foo%2Abar")
})
context("util fxns: net_connect_explicit_allowed")
test_that("net_connect_explicit_allowed", {
aa <- net_connect_explicit_allowed(
allowed = "example.com",
uri = "http://example.com")
expect_is(aa, "logical")
expect_equal(length(aa), 1)
# works with lists
expect_true(
net_connect_explicit_allowed(
list("example.com", "foobar.org"),
"example.com"
)
)
expect_false(
net_connect_explicit_allowed(
list("example.com", "foobar.org"),
"stuff.io"
)
)
# no uri passed, returns FALSE
expect_false(net_connect_explicit_allowed("google.com"))
# empty character string uri passed, returns FALSE
expect_false(net_connect_explicit_allowed("google.com", ""))
# no allowed passed, errors
expect_error(net_connect_explicit_allowed(),
"argument \"allowed\" is missing")
})
context("util fxns: webmockr_net_connect_allowed")
test_that("webmockr_net_connect_allowed", {
# works with character strings
expect_false(webmockr_net_connect_allowed("example.com"))
expect_false(webmockr_net_connect_allowed("http://example.com"))
expect_false(webmockr_net_connect_allowed("https://example.com"))
# no uri passed, returns FALSE
expect_false(webmockr_net_connect_allowed())
# nonense passed, returns FALSE
expect_false(webmockr_net_connect_allowed(""))
expect_false(webmockr_net_connect_allowed("asdfadfafsd"))
# errors when of wrong class
expect_error(webmockr_net_connect_allowed(mtcars),
"uri must be of class character, list")
})
context("util fxns: webmockr_disable_net_connect")
test_that("webmockr_disable_net_connect", {
# nothing passed
expect_null(sm(webmockr_disable_net_connect()))
expect_message(webmockr_disable_net_connect(), "net connect disabled")
# single uri passed
expect_message(webmockr_disable_net_connect("google.com"), "net connect disabled")
expect_is(sm(webmockr_disable_net_connect("google.com")), "character")
expect_equal(sm(webmockr_disable_net_connect("google.com")), "google.com")
# many uri's passed
expect_message(webmockr_disable_net_connect(c("google.com", "nytimes.com")),
"net connect disabled")
expect_is(sm(webmockr_disable_net_connect(c("google.com", "nytimes.com"))),
"character")
expect_equal(sm(webmockr_disable_net_connect(c("google.com", "nytimes.com"))),
c("google.com", "nytimes.com"))
# errors when of wrong class
expect_error(webmockr_disable_net_connect(5),
"allow must be of class character")
expect_error(webmockr_disable_net_connect(mtcars),
"allow must be of class character")
})
context("util fxns: webmockr_allow_net_connect")
test_that("webmockr_allow_net_connect", {
# first call, sets to TRUE, and returns message
# nothing passed
expect_message(z <- webmockr_allow_net_connect(), "net connect allowed")
expect_true(z)
# check if net collect allowed afterwards, should be TRUE
expect_true(webmockr_net_connect_allowed())
# errors when an argument passed
expect_error(webmockr_allow_net_connect(5), "unused argument")
})
context("config options: show_stubbing_instructions")
test_that("show_stubbing_instructions", {
x = crul::HttpClient$new("https://httpbin.org/get")
# DO show stubbing instructions
webmockr_configure(show_stubbing_instructions = TRUE)
err_mssg <- as.character(tryCatch(x$get(), error = function(e) e))
expect_true(grepl("snippet", err_mssg, perl = TRUE))
# DO NOT show stubbing instructions
webmockr_configure(show_stubbing_instructions = FALSE)
err_mssg <- as.character(tryCatch(x$get(), error = function(e) e))
expect_false(grepl("^((?!snippet).)*$", err_mssg, perl = TRUE))
# reset to default
webmockr_configure(show_stubbing_instructions = TRUE)
})
context("util fxns: webmockr_configuration")
test_that("webmockr_configuration", {
expect_is(webmockr_configuration(), "webmockr_config")
expect_named(
webmockr_configuration(),
c('show_stubbing_instructions', 'allow', 'allow_net_connect',
'allow_localhost')
)
# errors when an argument passed
expect_error(webmockr_configuration(5), "unused argument")
})
context("util fxns: webmockr_configure_reset")
test_that("webmockr_configure_reset", {
# webmockr_configure_reset does the same thing as webmockr_configure
expect_identical(webmockr_configure(), webmockr_configure_reset())
# errors when an argument passed
expect_error(webmockr_configure_reset(5), "unused argument")
})
context("util fxns: defunct")
test_that("webmockr_disable", {
expect_error(webmockr_disable(), "disable", class = "error")
})
test_that("webmockr_enable", {
expect_error(webmockr_enable(), "enable", class = "error")
})
context("util fxns: hdl_lst")
test_that("hdl_lst works", {
expect_equal(hdl_lst(NULL), "")
expect_equal(hdl_lst(character(0)), "")
expect_equal(hdl_lst(raw(0)), "")
expect_equal(hdl_lst(raw(5)), "raw bytes, length: 5")
expect_error(hdl_lst(), "argument \"x\" is missing")
expect_equal(hdl_lst(list(foo = "bar")), "foo=bar")
expect_equal(hdl_lst(list(foo = "5")), "foo=5")
expect_equal(hdl_lst(list(foo = "5", bar = "a")), "foo=5, bar=a")
expect_equal(hdl_lst(1.5), 1.5)
})
context("util fxns: hdl_lst2")
test_that("hdl_lst2 works", {
expect_equal(hdl_lst2(NULL), "")
expect_equal(hdl_lst2(character(0)), "")
expect_equal(hdl_lst2(raw(5)), "")
expect_equal(hdl_lst2(charToRaw("hello")), "hello")
expect_error(hdl_lst2(), "argument \"x\" is missing")
expect_equal(hdl_lst2(list(foo = "bar")), "foo=\"bar\"")
expect_equal(hdl_lst2(list(foo = 5)), "foo=5")
expect_equal(hdl_lst2(list(foo = 5, bar = "a")), "foo=5, bar=\"a\"")
expect_equal(hdl_lst2(list(foo = "bar", stuff = FALSE)), "foo=\"bar\", stuff=FALSE")
expect_equal(hdl_lst2(1.5), 1.5)
})
context("query_mapper")
test_that("query_mapper", {
expect_is(query_mapper, "function")
expect_null(query_mapper(NULL))
expect_equal(query_mapper(5), 5)
expect_equal(query_mapper('aaa'), 'aaa')
expect_equal(query_mapper(mtcars), mtcars)
})
webmockr/tests/testthat/httr_obj.rda 0000644 0001762 0000144 00000000503 14113773445 017377 0 ustar ligges users ‹ ]Q±NÃ05NKh„RÖLU‰!±!ØC·ÊM®iJbû,ÊÏŽWm†³}ÏÏ÷îß_wÑ""„P)¡=¨]NÈ€Œì~¶ATK¹ÚŒmÚ¸<¸Þ^?zЕ}Rë'Æš}UˆDªœå€Vä6ãu])ÇB
¶ÕRLc„²]UNãÃ;Lؤ×ÙPð
tOûô9M¡¶Rä·
:væŽY×e±J*ÙcòpŸÌbuã²y2»ì6±¨åEî‹FšþuuhWgd4(žƒÀËΪ×wCëu˜–\ë^±óoU ,+¨¤úé°‹[‘6£hKµzãàFfþW¬}KÀ3PÚóÖ”™ÏBY7u}qƒ›%ÊOž.
Ö[ÑÀ‹z¡‚/zo÷ÞŸsHO webmockr/tests/testthat/test-flipswitch.R 0000644 0001762 0000144 00000003171 14113773445 020354 0 ustar ligges users context("flipswitch (enable/disable)")
test_that("flipswitch in default state", {
expect_is(webmockr_lightswitch, "environment")
expect_is(webmockr_lightswitch$crul, "logical")
expect_false(webmockr_lightswitch$crul)
})
test_that("flipswitch - turn on with 'enable'", {
aa <- enable()
expect_is(aa, "logical")
expect_equal(length(aa), 2)
expect_true(all(aa))
expect_true(webmockr_lightswitch$crul)
skip_if_not_installed("httr")
expect_true(webmockr_lightswitch$httr)
})
test_that("flipswitch - turn on with 'enable' - one pkg", {
# disable all
disable()
# enable one pkg
aa <- enable('crul')
expect_is(aa, "logical")
expect_equal(length(aa), 1)
expect_true(aa)
expect_true(webmockr_lightswitch$crul)
skip_if_not_installed("httr")
expect_false(webmockr_lightswitch$httr)
})
test_that("flipswitch - turn off with 'disable'", {
aa <- disable()
# all are FALSE
expect_true(!all(aa))
expect_false(webmockr_lightswitch$crul)
skip_if_not_installed("httr")
expect_false(webmockr_lightswitch$httr)
})
test_that("enable and disable fail well", {
expect_error(enable(wasp = 5), "unused argument")
expect_error(disable(bee = 5), "unused argument")
expect_error(enable(adapter = 'stuff'),
"adapter must be one of")
expect_error(disable(adapter = 'stuff'),
"adapter must be one of")
# FIXME: not sure how to test when pkg not installed
# inside of test suite
})
test_that("enabled works", {
# disable all
disable()
expect_false(enabled())
expect_false(enabled('crul'))
expect_false(enabled('httr'))
expect_error(enabled('foobar'), "'adapter' must be in the set")
})
webmockr/tests/testthat/test-remove_request_stub.R 0000644 0001762 0000144 00000001654 14113773445 022306 0 ustar ligges users context("remove_request_stub")
# clear stubs before starting
stub_registry_clear()
test_that("remove_request_stub", {
# no stubs at beginning
expect_equal(length(stub_registry()$request_stubs), 0)
# make a stub
x <- stub_request("get", "https://httpbin.org/get")
# no there's a stub
expect_equal(length(stub_registry()$request_stubs), 1)
# remove the stub
w <- remove_request_stub(x)
expect_is(w, "list")
expect_equal(length(w), 0)
# no there's no stubs
expect_equal(length(stub_registry()$request_stubs), 0)
})
test_that("remove_request_stub: removes the stub upon an error", {
# no stubs at beginning
stub_registry_clear()
expect_equal(length(stub_registry()$request_stubs), 0)
expect_error(
stub_request("post", uri = "https://httpbin.org/post") %>%
to_return(body = 5)
)
expect_equal(length(stub_registry()$request_stubs), 0)
stub_registry_clear()
})
request_registry_clear()
webmockr/tests/testthat/test-webmockr_reset.R 0000644 0001762 0000144 00000001701 14113773445 021210 0 ustar ligges users context("webmockr_reset")
stub_registry_clear()
request_registry_clear()
enable()
test_that("webmockr_reset works", {
# before any stubs creatd
expect_equal(length(stub_registry()$request_stubs), 0)
expect_equal(length(request_registry()$request_signatures$hash), 0)
expect_null(webmockr_reset())
expect_equal(length(stub_registry()$request_stubs), 0)
expect_equal(length(request_registry()$request_signatures$hash), 0)
# after a stub creatd
stub_request("get", "https://scottchamberlain.info")
crul::HttpClient$new("https://scottchamberlain.info")$get()
expect_equal(length(stub_registry()$request_stubs), 1)
expect_equal(length(request_registry()$request_signatures$hash), 1)
webmockr_reset()
expect_equal(length(stub_registry()$request_stubs), 0)
expect_equal(length(request_registry()$request_signatures$hash), 0)
})
test_that("webmockr_reset fails well", {
expect_error(webmockr_reset(4), "unused argument")
})
disable()
webmockr/tests/testthat/httr_body_upload_list.rda 0000644 0001762 0000144 00000000657 14113773445 022173 0 ustar ligges users ‹ mRMOÜ05ÉB´«ªBB•8æÐ‚˜…m+qCT•PAÛây“ÉÆàÄÆ‚üù¶v—Åaâ™ñóÌ{3Y~8›=ÌBŠ÷"ÅÖDö³ƒ&hjÏOµÖ2_ó²Ë`œ”9£J#ØËÄÚþxrwûkäm¡.0vçš¶—,¸«mãN‰ŒDSÞâGÅÛãTëƯ
;N·ïúÄ>
ˆî¶¤4ß»,
¶úmÛô7õ(@ÞຖDvø‡´…^¸|Rx™U>À÷ •m¯ðyö/Aq#P˜¯ÖD¾º^]®®oµg½ÁmÒÑŸùÁ ¢kïëN@(®`D…⦗M^Qƒ¾ÿ*{ÐN·%>ýÙ²/Œdø[öe‘¦ò¤ÙyêÖŽçÙ"›;Ãàœ½KjH²VÿÓÓ/Ø5vÖO|ü³âðñ‡I5ä
4\vcîã«L[¸¿£/ñn@×¼£ØÒݤRÚy\E•>J¸pu}8#F×¹æOÐz87ZPösK$<xSøÎ0‡G webmockr/tests/testthat/test-pluck_body.R 0000644 0001762 0000144 00000004315 14113773445 020334 0 ustar ligges users context("pluck_body")
test_that("pluck_body: crul", {
# prep objects
# con <- crul::HttpClient$new("https://httpbin.org")
# upload_list <- list(y = crul::upload(system.file("CITATION")))
# b <- con$post("post", body = upload_list)
# crul_body_upload_list <- b$request
# crul_body_upload_list$url$handle <- NULL
# save(crul_body_upload_list,
# file = "tests/testthat/crul_body_upload_list.rda", version = 2)
# upload_no_list <- crul::upload(system.file("CITATION"))
# d <- con$post("post", body = upload_no_list)
# crul_body_upload_no_list <- d$request
# crul_body_upload_no_list$url$handle <- NULL
# save(crul_body_upload_no_list,
# file = "tests/testthat/crul_body_upload_no_list.rda", version = 2)
# upload in a list
load("crul_body_upload_list.rda")
expect_is(pluck_body(crul_body_upload_list), "list")
# upload not in a list
load("crul_body_upload_no_list.rda")
expect_is(pluck_body(crul_body_upload_no_list), "character")
expect_match(pluck_body(crul_body_upload_no_list), "file size")
})
test_that("pluck_body: httr", {
# prep objects
# upload_list <- list(y = httr::upload_file(system.file("CITATION")))
# b <- httr::POST("https://httpbin.org/post", body = upload_list)
# httr_body_upload_list <- b$request
# save(httr_body_upload_list,
# file = "tests/testthat/httr_body_upload_list.rda", version = 2)
# upload_no_list <- httr::upload_file(system.file("CITATION"))
# d <- httr::POST("https://httpbin.org/post", body = upload_no_list)
# httr_body_upload_no_list <- d$request
# save(httr_body_upload_no_list,
# file = "tests/testthat/httr_body_upload_no_list.rda", version = 2)
# upload in a list
load("httr_body_upload_list.rda")
expect_is(pluck_body(httr_body_upload_list), "list")
# upload not in a list
load("httr_body_upload_no_list.rda")
expect_is(pluck_body(httr_body_upload_no_list), "character")
expect_match(pluck_body(httr_body_upload_no_list), "file size")
})
test_that("pluck_body fails well", {
expect_error(pluck_body(5), "not a valid")
expect_error(pluck_body(mtcars), "not a valid")
expect_error(pluck_body(FALSE), "not a valid")
expect_error(pluck_body(list(url="adf", method=3, options=5)),
"not a valid")
})
webmockr/tests/testthat/test-StubRegistry.R 0000644 0001762 0000144 00000006225 14113773445 020651 0 ustar ligges users context("StubRegistry")
aa <- StubRegistry$new()
test_that("StubRegistry: bits are correct prior to having data", {
expect_is(StubRegistry, "R6ClassGenerator")
expect_is(aa, "StubRegistry")
expect_is(aa$global_stubs, "list")
expect_equal(length(aa$global_stubs), 0)
expect_is(aa$request_stubs, "list")
expect_equal(length(aa$request_stubs), 0)
expect_null(aa$stub)
expect_is(aa$find_stubbed_request, "function")
expect_is(aa$is_registered, "function")
expect_is(aa$print, "function")
expect_is(aa$register_stub, "function")
expect_is(aa$remove_all_request_stubs, "function")
expect_is(aa$remove_request_stub, "function")
expect_is(aa$request_stub_for, "function")
# expect_is(aa$response_for_request, "function")
})
test_that("StubRegistry: bits are correct after having data", {
stub1 <- StubbedRequest$new(method = "get", uri = "http://api.crossref.org")
stub1$with(headers = list('User-Agent' = 'R'))
stub1$to_return(status = 200, body = "foobar", headers = list())
stub2 <- StubbedRequest$new(method = "get", uri = "https://httpbin.org")
aa <- StubRegistry$new()
expect_is(aa$register_stub(stub = stub1), "list")
expect_is(aa$register_stub(stub = stub2), "list")
expect_is(aa, "StubRegistry")
# global stubs are still empty
expect_is(aa$global_stubs, "list")
expect_equal(length(aa$global_stubs), 0)
# request stubs now length 2
expect_is(aa$request_stubs, "list")
expect_equal(length(aa$request_stubs), 2)
expect_null(aa$stub)
# find_stubbed_request
req1 <- RequestSignature$new(
method = "get",
uri = "http://api.crossref.org",
options = list(
headers = list('User-Agent' = 'R')
)
)
res <- aa$find_stubbed_request(req = req1)
expect_is(res, "list")
expect_is(res[[1]], "StubbedRequest")
expect_equal(res[[1]]$uri, "http://api.crossref.org")
# is_registered
expect_true(aa$is_registered(x = req1))
# request_stub_for
matches <- aa$request_stub_for(request_signature = req1)
expect_is(matches, "logical")
expect_equal(matches, c(TRUE, FALSE))
# response_for_request
## FIXME - internal function not made yet
# expect_error(aa$response_for_request(request_signature = req1),
# "could not find function")
# remove_request_stub
res <- aa$remove_request_stub(stub = stub1)
expect_is(res, "list")
expect_equal(length(res), 1)
# remove_all_request_stubs
## add another first
aa$register_stub(stub = stub1)
res <- aa$remove_all_request_stubs()
expect_is(res, "list")
expect_equal(length(res), 0)
})
test_that("StubRegistry fails well", {
# fill ins ome data first
stub1 <- StubbedRequest$new(method = "get", uri = "api.crossref.org")
aa <- StubRegistry$new()
aa$register_stub(stub = stub1)
expect_error(aa$find_stubbed_request(), "argument \"req\" is missing")
expect_error(aa$is_registered(), "argument \"x\" is missing")
expect_error(aa$register_stub(), "argument \"stub\" is missing")
expect_error(aa$remove_request_stub(), "argument \"stub\" is missing")
expect_error(aa$request_stub_for(), "argument \"request_signature\" is missing")
# expect_error(aa$response_for_request(), "argument \"request_signature\" is missing")
})
webmockr/tests/testthat/httr_body_upload_no_list.rda 0000644 0001762 0000144 00000002574 14113773445 022667 0 ustar ligges users ‹ íXmoÛ6¦%YŽí4ov’&C€l(: h¤vM×aȇu+Ö!†~3d›ŽµÊ’ª$Ù¾ìì?î?t_†
ÉxÒQ¦)ÉqÚÝ0¸Ð"wÇçŽä}õò“ÆË!D!ª®Ee?5…ý«ÔY{{EA§ëõÏ;±ïxV¿ãzÇ#BÔ¯1Yôµï^|,õ
?üÜ4¡íÚ®á'¦ï
e%ñê=°|ß±{Vd{®ùcè¹÷w#z™g#çþ®8–tÜ3ïá¼F¢å;–íJÑW]kDC‡»ÑŸözÔðkñ™çFÔöŽÏ}ʾ/RQ`qš´;ŽÝíÅc>1ïvƒ½äkßx´0™}ã!”ÌIDSÑüý-DƒY·»œ›_¤S/xšGÆ€˜?Ð d`‡æ#ãSóˆ†^ôhh:8«k…Ô|vxüôøðÅ·’í\B”K)šoECþ;JŸL\ϱÂP2[xÁ¨3°ž§uÄùbÜdÒFŒ _MÛx×q6“ºfø
ì„U’)ü»hj}dE½¡aÒÄLA§nÏëSì½ZšDüË8R^-ÌÒÇŠ[Z¡g~@Ã$‡nŸž³ÝdºZu\aÚ¥\}}ƒ;õêWfØVi‹¨ràºÛ=’û§¼$Ã0Ò„ ãß 3ØB›ˆÔ]’ì‹DL/3YűM„j’Ìä#vmÃÜ&èæl£ý6ö}Èäc´ÛF`£ˆeÉôÕBÛëh»6A·#Äø™#÷c[¤¸@+?“qaþŽƒEŠŠ=4+%š5;4ÜØqÈDÅë—¨vA
·ÌÊ¥d^)1¯4ŠWÒVK´ÕÀ:—µ¢)ƒ=R¼‰Õ®-ÇS-' VÿË uŽhy%¿axIIJh ú¤÷UÛZIt•Iq¡DQw¨{Ž扰6ŠR,‡;Kêë%^Ùqï…´ º"%FÔ99ôg©y½´ó&ý´©[Òæý*¶é9E6y?lì+.(]Ô¿¡ë!Ñõ°Úfg韧‚°¾Aßì{‹/H~kØêiÿ6ï¯_Îú
ÆÏËì6»’]…Œï1E—ÆU\ÿ,rKå)ÂS¿Š²&H{ŠÀ¶‰²&ÉâX®…[uÍÈ‚š±A HàÄn˜>öµ`|`S§|¸ã0Ö%p
N†2³5~–h4ôú¼þUçgõù`ô–륦ù çƒkþÙ°âh؉¼W”G£{qäÇQ ìxS`L9J®cd@ʘ”« z‹C\‘ÀŠ’õRwSÞZfv™£Ø]Ý׬zw&^g²73ðÔÂzÕ
-}#ærÖ¿“·µÿÔ³d.«¹—H>°$?’óWºÉ飨‰lß
r¸fÕ"±ÁLþ"陳`_Â>mâ‚ó`ÇîbN ή6Ô®«¦t
%ã3œM~þrsç·³€v¦ü]ÇîÚ<ÃÕ”i'ØJxÏÙ7™³ï9ûΖ'û•ãš³ï9û&söýeßp•=öiœüÄ!
¬êFéz—×?µ5ö•ÅÉùÜâi`G´3¢#/8ç-íËœd÷ü¿ñý\ü
´Í!£! webmockr/tests/testthat/test-HashCounter.R 0000644 0001762 0000144 00000002013 14113773445 020415 0 ustar ligges users context("HashCounter")
test_that("HashCounter: structure", {
expect_is(HashCounter, "R6ClassGenerator")
x <- HashCounter$new()
expect_is(x, "HashCounter")
expect_is(x$clone, "function")
expect_is(x$get, "function")
expect_is(x$put, "function")
expect_is(x$hash, "list")
})
test_that("HashCounter: works as expected", {
x <- HashCounter$new()
a <- RequestSignature$new(method = "get", uri = "https:/httpbin.org/get")
b <- RequestSignature$new(method = "post", uri = "https://www.wikipedia.org/")
x$put(a)
expect_length(x$hash, 1)
expect_equal(x$hash[[a$to_s()]]$count, 1)
x$put(a)
expect_length(x$hash, 1)
expect_equal(x$hash[[a$to_s()]]$count, 2)
x$put(b)
expect_length(x$hash, 2)
expect_equal(x$hash[[b$to_s()]]$count, 1)
x$put(b)
x$put(b)
expect_length(x$hash, 2)
expect_equal(x$hash[[b$to_s()]]$count, 3)
})
test_that("HashCounter fails well", {
x <- HashCounter$new()
expect_error(x$get(), '\"req_sig\" is missing')
expect_error(x$put(), '\"req_sig\" is missing')
})
webmockr/tests/testthat/test-auth_handling.R 0000644 0001762 0000144 00000002477 14113773445 021015 0 ustar ligges users # from https://github.com/ropensci/webmockr/issues/108
# httr
stub_registry()$remove_all_request_stubs()
skip_if_not_installed("httr")
library("httr")
enable("httr")
test_that("auth handling: httr", {
stub_request("get", "http://stuff.com")
# auth well-formed
expect_is(
GET("http://stuff.com", authenticate("adf", "adf")),
"response"
)
# user name invalid according to RFC, but we can't know that
expect_is(
GET("http://stuff.com", authenticate("foo:bar", "adf")),
"response"
)
# malformed: url as username
expect_error(
GET("http://stuff.com", authenticate("http://", "foo.com"))
)
})
# crul
disable()
stub_registry()$remove_all_request_stubs()
skip_if_not_installed("crul")
library("crul")
enable("crul")
test_that("auth handling: httr", {
stub_request("get", "http://stuff.com")
# auth well-formed
x <- HttpClient$new("http://stuff.com")
x$auth <- auth("adf", "adf")
expect_is(x$get(), "HttpResponse")
# user name invalid according to RFC, but we can't know that
y <- HttpClient$new("http://stuff.com")
y$auth <- auth("foo:bar", "adf")
expect_is(y$get(), "HttpResponse")
# malformed: url as username
z <- HttpClient$new("http://stuff.com")
z$auth <- auth("http://", "foo.com")
expect_error(z$get())
})
stub_registry()$remove_all_request_stubs()
disable()
webmockr/tests/testthat/test-Response.R 0000644 0001762 0000144 00000007524 14113773445 020004 0 ustar ligges users context("Response")
aa <- Response$new()
test_that("Response: bits are correct prior to having data", {
expect_is(Response, "R6ClassGenerator")
expect_is(aa, "Response")
expect_null(aa$body, "function")
expect_null(aa$content, "function")
expect_null(aa$exception, "function")
expect_is(aa$get_body, "function")
expect_is(aa$get_exception, "function")
expect_is(aa$get_request_headers, "function")
expect_is(aa$get_respone_headers, "function")
expect_is(aa$get_status, "function")
expect_is(aa$get_url, "function")
expect_is(aa$print, "function")
expect_is(aa$set_body, "function")
expect_is(aa$set_exception, "function")
expect_is(aa$set_request_headers, "function")
expect_is(aa$set_response_headers, "function")
expect_is(aa$set_status, "function")
expect_is(aa$set_url, "function")
expect_null(aa$should_timeout, "function")
expect_null(aa$request_headers)
expect_null(aa$response_headers)
expect_null(aa$response_headers_all)
expect_equal(aa$status_code, 200)
expect_null(aa$url)
expect_null(aa$name)
})
test_that("Response: bits are correct after having data", {
aa <- Response$new()
aa$set_url("https://httpbin.org/get")
aa$set_request_headers(list('Content-Type' = "application/json"))
aa$set_response_headers(list('Host' = "httpbin.org"))
aa$set_status(404)
aa$set_body("hello world")
aa$set_exception("exception")
expect_is(aa, "Response")
expect_null(aa$should_timeout)
expect_is(aa$request_headers, "list")
expect_named(aa$request_headers, "Content-Type")
expect_is(aa$response_headers, "list")
expect_named(aa$response_headers, "Host")
# response_headers_all doesn't exist in Response, it's specific to crul
expect_null(aa$response_headers_all)
expect_equal(aa$status_code, 404)
expect_equal(aa$url, "https://httpbin.org/get")
expect_null(aa$name)
expect_equal(aa$body, "hello world")
expect_is(aa$content, "raw")
expect_equal(aa$exception, "exception")
expect_equal(aa$get_body(), "hello world")
expect_equal(aa$get_exception(), "exception")
expect_equal(aa$get_request_headers()[[1]], "application/json")
expect_equal(aa$get_respone_headers()[[1]], "httpbin.org")
expect_equal(aa$get_status(), 404)
expect_equal(aa$get_url(), "https://httpbin.org/get")
expect_output(aa$print(), "")
expect_output(aa$print(), "headers")
expect_output(aa$print(), "request headers")
# set_body: char gets converted to raw in $content
aa$set_body(body = "stuff")
expect_is(aa$body, "character")
expect_is(aa$content, "raw")
expect_length(aa$content, 5)
# set_body: raw remains as raw in $content
aa$set_body(body = charToRaw("stuff"))
expect_is(aa$body, "raw")
expect_is(aa$content, "raw")
expect_length(aa$content, 5)
# set_body: other types return raw(0) in $content
aa$set_body(body = NULL)
expect_null(aa$body)
expect_is(aa$content, "raw")
expect_length(aa$content, 0)
aa$set_exception(exception = "stop, wait, listen")
expect_equal(aa$exception, "stop, wait, listen")
aa$set_request_headers(headers = list(a = "howdy"))
expect_equal(aa$request_headers[[1]], "howdy")
aa$set_response_headers(headers = list(b = 6))
expect_equal(aa$get_respone_headers()[[1]], "6")
aa$set_status(status = 410)
expect_equal(aa$status_code, 410)
aa$set_url(url = "foobar.com")
expect_equal(aa$url, "foobar.com")
})
test_that("Response fails well", {
expect_error(aa$set_body(), "argument \"body\" is missing")
# body must be length 1
expect_error(aa$set_body(letters), "is not TRUE")
expect_error(aa$set_exception(), "argument \"exception\" is missing")
expect_error(aa$set_request_headers(), "argument \"headers\" is missing")
expect_error(aa$set_response_headers(), "argument \"headers\" is missing")
expect_error(aa$set_status(), "argument \"status\" is missing")
expect_error(aa$set_url(), "argument \"url\" is missing")
})
webmockr/tests/testthat/test-writing-to-disk.R 0000644 0001762 0000144 00000005706 14113773445 021241 0 ustar ligges users context("mock writing to disk")
enable()
test_that("Write to a file before mocked request: crul", {
skip_on_cran()
library(crul)
## make a temp file
f <- tempfile(fileext = ".json")
## write something to the file
cat("{\"hello\":\"world\"}\n", file = f)
expect_is(readLines(f), "character")
expect_match(readLines(f), "world")
## make the stub
stub_request("get", "https://httpbin.org/get") %>%
to_return(body = file(f))
## make a request
out <- HttpClient$new("https://httpbin.org/get")$get(disk = f)
expect_is(out$content, "character")
expect_equal(attr(out$content, "type"), "file")
expect_is(readLines(out$content), "character")
expect_match(readLines(out$content), "hello")
# cleanup
unlink(f)
stub_registry_clear()
})
test_that("Write to a file before mocked request: httr", {
skip_on_cran()
library(httr)
## make a temp file
f <- tempfile(fileext = ".json")
## write something to the file
cat("{\"hello\":\"world\"}\n", file = f)
expect_is(readLines(f), "character")
expect_match(readLines(f), "world")
## make the stub
stub_request("get", "https://httpbin.org/get") %>%
to_return(body = file(f),
headers = list('content-type' = "application/json"))
## make a request
## with httr, you must set overwrite=TRUE or you'll get an errror
out <- GET("https://httpbin.org/get", write_disk(f, overwrite=TRUE))
content(out)
expect_is(out$content, "path")
expect_equal(attr(out$content, "class"), "path")
expect_is(readLines(out$content), "character")
expect_match(readLines(out$content), "hello")
# cleanup
unlink(f)
stub_registry_clear()
})
test_that("Use mock_file to have webmockr handle file and contents: crul", {
skip_on_cran()
library(crul)
## make a temp file
f <- tempfile(fileext = ".json")
## make the stub
stub_request("get", "https://httpbin.org/get") %>%
to_return(body = mock_file(f, "{\"hello\":\"mars\"}\n"))
## make a request
out <- crul::HttpClient$new("https://httpbin.org/get")$get(disk = f)
out$content
expect_is(out$content, "character")
expect_match(out$content, "json")
expect_is(readLines(out$content), "character")
expect_true(any(grepl("hello", readLines(out$content))))
# cleanup
unlink(f)
stub_registry_clear()
})
test_that("Use mock_file to have webmockr handle file and contents: httr", {
skip_on_cran()
library(httr)
## make a temp file
f <- tempfile(fileext = ".json")
## make the stub
stub_request("get", "https://httpbin.org/get") %>%
to_return(
body = mock_file(path = f, payload = "{\"foo\": \"bar\"}"),
headers = list('content-type' = "application/json")
)
## make a request
out <- GET("https://httpbin.org/get", write_disk(f))
## view stubbed file content
expect_is(out$content, "path")
expect_match(out$content, "json")
expect_is(readLines(out$content), "character")
expect_true(any(grepl("foo", readLines(out$content))))
# cleanup
unlink(f)
stub_registry_clear()
})
webmockr/tests/testthat/test-stub_registry.R 0000644 0001762 0000144 00000002752 14113773445 021111 0 ustar ligges users context("stub_registry")
test_that("stub_registry: works", {
# before any stubs creatd
expect_output(print(stub_registry()), "Registered Stubs")
expect_equal(length(stub_registry()$request_stubs), 0)
# after a stub creatd
stub_request("get", "https://scottchamberlain.info")
expect_equal(length(stub_registry()$request_stubs), 1)
expect_match(stub_registry()$request_stubs[[1]]$to_s(),
"GET: https://scottchamberlain.info")
# stub with body
stub_request('post', uri = 'https://httpbin.org/post') %>%
wi_th(
body = list(y=crul::upload(system.file("CITATION")))
)
expect_equal(length(stub_registry()$request_stubs), 2)
expect_match(stub_registry()$request_stubs[[2]]$to_s(),
"POST: https://httpbin.org/post")
expect_match(stub_registry()$request_stubs[[2]]$to_s(),
"CITATION")
expect_match(stub_registry()$request_stubs[[2]]$to_s(),
"text/plain")
stub_registry_clear()
# stub with > 1 to_return()
s <- stub_request("get", "https://httpbin.org/get")
to_return(s, status = 200, body = "foobar", headers = list(a = 5))
to_return(s, status = 200, body = "bears", headers = list(b = 6))
expect_equal(length(stub_registry()$request_stubs), 1)
expect_equal(length(stub_registry()$request_stubs[[1]]$to_s()), 1)
expect_match(stub_registry()$request_stubs[[1]]$to_s(), "foobar")
expect_match(stub_registry()$request_stubs[[1]]$to_s(), "bears")
})
test_that("stub_registry fails well", {
expect_error(stub_registry(4), "unused argument")
})
webmockr/tests/testthat/test-b-no-cassette-in-use.R 0000644 0001762 0000144 00000001066 14113773445 022043 0 ustar ligges users context("no_cassette_in_use")
test_that("no cassette in use behaves as expected", {
skip_if_not_installed("vcr")
library("vcr")
dir <- tempdir()
invisible(vcr_configure(dir = dir))
crul::mock()
x <- crul::HttpClient$new(url = "https://httpbin.org")
# when no cassette in use, we get expected vcr error
expect_error(
x$get("get"),
"There is currently no cassette in use"
)
# cleanup
unlink(file.path(vcr_configuration()$dir, "turtle.yml"))
# reset configuration
vcr_configure_reset()
# unload vcr
unloadNamespace("vcr")
})
webmockr/tests/testthat/test-to_timeout.R 0000644 0001762 0000144 00000002123 14113773445 020364 0 ustar ligges users context("to_timeout")
stub_registry()$remove_all_request_stubs()
test_that("no stubs exist before stub_request called", {
expect_equal(length(stub_registry()$request_stubs), 0)
})
aa <- stub_request("get", "https://httpbin.org/get") %>% to_timeout()
test_that("stub_request bits are correct", {
expect_is(aa, "StubbedRequest")
expect_null(aa$body)
expect_null(aa$host)
expect_null(aa$response)
expect_null(aa$query)
expect_null(aa$request_headers)
expect_null(aa$response_headers)
expect_is(aa$responses_sequences, "list")
expect_is(aa$method, "character")
expect_equal(aa$method, "get")
expect_is(aa$uri, "character")
expect_equal(aa$uri, "https://httpbin.org/get")
# to_timeout expected stuff
expect_true(aa$responses_sequences[[1]]$timeout)
})
test_that("stubs exist after stub_request called", {
expect_equal(length(stub_registry()$request_stubs), 1)
})
test_that("stub_request fails well", {
expect_error(to_timeout(), "argument \".data\" is missing")
expect_error(to_timeout(5), ".data must be of class StubbedRequest")
})
# cleanup
stub_registry_clear()
webmockr/tests/testthat/helper-webmockr.R 0000644 0001762 0000144 00000001025 14113773445 020305 0 ustar ligges users sm <- function(x) suppressMessages(x)
get_err_mssg <- function(x) {
tmp <- tryCatch(x, error = function(e) e)
if (inherits(tmp, "error")) unclass(tmp)$message else tmp
}
# from https://stackoverflow.com/a/14838321/1091766
re_escape <- function(strings){
vals <- c("\\\\", "\\[", "\\]", "\\(", "\\)",
"\\{", "\\}", "\\^", "\\$","\\*",
"\\+", "\\?", "\\.", "\\|")
replace.vals <- paste0("\\\\", vals)
for(i in seq_along(vals)){
strings <- gsub(vals[i], replace.vals[i], strings)
}
strings
}
webmockr/tests/testthat/test-stub_request.R 0000644 0001762 0000144 00000002535 14113773445 020730 0 ustar ligges users context("stub_request")
stub_registry()$remove_all_request_stubs()
test_that("no stubs exist before stub_request called", {
expect_equal(length(stub_registry()$request_stubs), 0)
})
aa <- stub_request("get", "https://httpbin.org/get")
test_that("stub_request bits are correct", {
expect_is(aa, "StubbedRequest")
expect_null(aa$body)
expect_null(aa$host)
expect_null(aa$query)
expect_null(aa$request_headers)
expect_null(aa$response)
expect_null(aa$response_headers)
expect_null(aa$responses_sequences)
expect_is(aa$method, "character")
expect_equal(aa$method, "get")
expect_is(aa$uri, "character")
expect_equal(aa$uri, "https://httpbin.org/get")
expect_is(aa$print, "function")
expect_output(aa$print(), "")
expect_is(aa$to_return, "function")
expect_error(aa$to_return(), "argument \"body\" is missing")
expect_is(aa$to_s, "function")
expect_equal(aa$to_s(), "GET: https://httpbin.org/get")
expect_is(aa$with, "function")
expect_null(aa$with())
expect_is(aa$uri_parts, "list")
})
test_that("stubs exist after stub_request called", {
expect_equal(length(stub_registry()$request_stubs), 1)
})
test_that("stub_request fails well", {
expect_error(stub_request(), "one of uri or uri_regex is required")
expect_error(stub_request(method = "stuff", "adf"),
"'arg' should be one of")
})
webmockr/tests/testthat/test-within_test_that_blocks.R 0000644 0001762 0000144 00000003176 14113773445 023123 0 ustar ligges users context("within test_that blocks: httr")
library("httr")
test_that("httr: without pipe", {
httr_mock()
enable()
dat_json <- '{"foo":"bar"}'
stub <- stub_request("get", uri = "https://httpbin.org/get")
to_return(stub,
body = dat_json,
headers = list("Content-Type" = "application/json; charset=utf-8")
)
res <- GET("https://httpbin.org/get")
expect_true(inherits(res, "response"))
expect_is(content(res), "list")
expect_named(content(res), "foo")
expect_equal(content(res)$foo, "bar")
disable()
httr_mock(FALSE)
})
test_that("httr: with pipe", {
enable()
dat_json <- '{"foo":"bar"}'
stub <- stub_request("get", uri = "https://httpbin.org/get") %>%
to_return(body = dat_json,
headers = list("Content-Type" = "application/json; charset=utf-8"))
res <- GET("https://httpbin.org/get")
expect_true(inherits(res, "response"))
expect_is(content(res), "list")
expect_named(content(res), "foo")
expect_equal(content(res)$foo, "bar")
disable()
})
unloadNamespace("httr")
context("within test_that blocks: crul")
test_that("crul works", {
enable()
dat_json <- '{"foo":"bar"}'
stub <- stub_request("get", uri = "https://httpbin.org/get")
to_return(stub,
body = dat_json,
headers = list("Content-Type" = "application/json; howdy")
)
res <- crul::HttpClient$new("https://httpbin.org")$get("get")
expect_true(inherits(res, "HttpResponse"))
expect_is(res$parse("UTF-8"), "character")
expect_is(jsonlite::fromJSON(res$parse("UTF-8")), "list")
expect_named(jsonlite::fromJSON(res$parse("UTF-8")), "foo")
expect_equal(jsonlite::fromJSON(res$parse("UTF-8"))$foo, "bar")
disable()
})
webmockr/tests/testthat/test-CrulAdapter.R 0000644 0001762 0000144 00000013642 14113773445 020412 0 ustar ligges users context("CrulAdapter")
aa <- CrulAdapter$new()
test_that("CrulAdapter bits are correct", {
skip_on_cran()
expect_is(CrulAdapter, "R6ClassGenerator")
expect_is(aa, "CrulAdapter")
expect_null(aa$build_crul_request) # pulled out of object, so should be NULL
expect_null(aa$build_crul_response) # pulled out of object, so should be NULL
expect_is(aa$disable, "function")
expect_is(aa$enable, "function")
expect_is(aa$handle_request, "function")
expect_is(aa$remove_stubs, "function")
expect_is(aa$name, "character")
expect_equal(aa$name, "CrulAdapter")
})
test_that("CrulAdapter behaves correctly", {
skip_on_cran()
expect_message(aa$enable(), "CrulAdapter enabled!")
expect_message(aa$disable(), "CrulAdapter disabled!")
})
test_that("build_crul_request/response fail well", {
skip_on_cran()
expect_error(build_crul_request(), "argument \"x\" is missing")
expect_error(build_crul_response(), "argument \"resp\" is missing")
})
test_that("CrulAdapter: works when vcr is loaded but no cassette is inserted", {
skip_on_cran()
skip_if_not_installed("vcr")
webmockr::enable(adapter = "crul")
on.exit({
webmockr::disable(adapter = "crul")
unloadNamespace("vcr")
})
stub_request("get", "https://httpbin.org/get")
library("vcr")
# works when no cassette is loaded
cli <- crul::HttpClient$new("https://httpbin.org")
expect_silent(x <- cli$get("get"))
expect_is(x, "HttpResponse")
# works when empty cassette is loaded
vcr::vcr_configure(dir = tempdir())
vcr::insert_cassette("empty")
expect_silent(x <- cli$get("get"))
vcr::eject_cassette("empty")
expect_is(x, "HttpResponse")
})
context("CrulAdapter - with real data")
test_that("CrulAdapter works", {
skip_on_cran()
skip_if_not_installed('vcr')
load("crul_obj.rda")
crul_obj$url$handle <- curl::new_handle()
res <- CrulAdapter$new()
# with vcr message
library(vcr)
expect_error(
res$handle_request(crul_obj),
"There is currently no cassette in use"
)
# with webmockr message
# unload vcr
unloadNamespace("vcr")
expect_error(
res$handle_request(crul_obj),
"Real HTTP connections are disabled.\nUnregistered request:\n GET: http://localhost:9000/get\n\nYou can stub this request with the following snippet:\n\n stub_request\\('get', uri = 'http://localhost:9000/get'\\)\n============================================================"
)
invisible(stub_request("get", "http://localhost:9000/get"))
aa <- res$handle_request(crul_obj)
expect_is(res, "CrulAdapter")
expect_is(aa, "HttpResponse")
expect_equal(aa$method, "get")
expect_equal(aa$url, "http://localhost:9000/get")
# no response headers
expect_equal(length(aa$response_headers), 0)
expect_equal(length(aa$response_headers_all), 0)
# with headers
# clear registry
stub_registry_clear()
# stub with headers
x <- stub_request("get", "http://localhost:9000/get")
x <- to_return(x, headers = list('User-Agent' = 'foo-bar'))
aa <- res$handle_request(crul_obj)
expect_is(res, "CrulAdapter")
expect_is(aa, "HttpResponse")
expect_equal(aa$method, "get")
expect_equal(aa$url, "http://localhost:9000/get")
# has response_headers and response_headers_all
expect_equal(length(aa$response_headers), 1)
expect_is(aa$response_headers, "list")
expect_named(aa$response_headers, "user-agent")
expect_equal(length(aa$response_headers_all), 1)
expect_is(aa$response_headers_all, "list")
expect_named(aa$response_headers_all, NULL)
expect_named(aa$response_headers_all[[1]], "user-agent")
# stub with redirect headers
my_url <- "https://doi.org/10.1007/978-3-642-40455-9_52-1"
x <- stub_request("get", my_url)
x <- to_return(x, status = 302, headers =
list(
status = 302,
location = "http://link.springer.com/10.1007/978-3-642-40455-9_52-1"
)
)
crul_obj$url$url <- my_url
res <- CrulAdapter$new()
aa <- res$handle_request(crul_obj)
expect_equal(aa$method, "get")
expect_equal(aa$url, my_url)
expect_equal(aa$status_code, 302)
# has response_headers and response_headers_all
expect_equal(length(aa$response_headers), 2)
expect_is(aa$response_headers, "list")
expect_equal(sort(names(aa$response_headers)), c('location', 'status'))
expect_equal(length(aa$response_headers_all), 1)
expect_equal(length(aa$response_headers_all[[1]]), 2)
expect_is(aa$response_headers_all, "list")
expect_is(aa$response_headers_all[[1]], "list")
expect_named(aa$response_headers_all, NULL)
expect_equal(sort(names(aa$response_headers_all[[1]])),
c('location', 'status'))
## FIXME: ideally can test multiple redirect headers, e.g. like this:
# x <- stub_request("get", "https://doi.org/10.1007/978-3-642-40455-9_52-1")
# x <- to_return(x, headers = list(
# list(
# status = 'HTTP/1.1 302 ',
# location = "http://link.springer.com/10.1007/978-3-642-40455-9_52-1"
# ),
# list(
# status = 'HTTP/1.1 301 Moved Permanently',
# location = "https://link.springer.com/10.1007/978-3-642-40455-9_52-1"
# ),
# list(
# status = 'HTTP/1.1 302 Found',
# location = "https://link.springer.com/referenceworkentry/10.1007%2F978-3-642-40455-9_52-1"
# ),
# list(
# status = 'HTTP/1.1 200 OK'
# )
# ))
})
test_that("crul requests with JSON-encoded bodies work", {
skip_on_cran()
on.exit(disable(adapter = "crul"))
enable(adapter = "crul")
body <- list(foo = "bar")
url <- "https://httpbin.org"
cli <- crul::HttpClient$new(url)
z <- stub_request("post", uri = file.path(url, "post")) %>%
wi_th(body = jsonlite::toJSON(body, auto_unbox = TRUE))
# encoded body works
res <- cli$post("post", body = body, encode = "json")
expect_is(res, "HttpResponse")
# encoded but modified body fails
expect_error(
cli$post("post", body = list(foo = "bar1"), encode = "json"),
"Unregistered request"
)
# unencoded body fails
expect_error(
cli$post("post", body = body),
"Unregistered request"
)
})
webmockr/tests/testthat/test-to_return_body.R 0000644 0001762 0000144 00000003673 14113773445 021245 0 ustar ligges users context("to_return: response body types behave correctly for crul pkg")
test_that("to_return: setting body behaves correctly", {
enable()
stub_registry_clear()
# character
aa <- stub_request("get", "https://google.com") %>%
to_return(body = '{"foo":"bar"}')
z <- crul::HttpClient$new(url = "https://google.com")$get()
expect_is(z$content, "raw")
expect_is(z$parse("UTF-8"), "character")
expect_equal(z$parse("UTF-8"), '{"foo":"bar"}')
stub_registry_clear() # cleanup
# list
bb <- stub_request("get", "https://google.com") %>%
to_return(body = list(foo = "bar"))
z <- crul::HttpClient$new(url = "https://google.com")$get()
expect_is(z$content, "raw")
expect_is(z$parse("UTF-8"), "character")
expect_equal(z$parse("UTF-8"), '{"foo":"bar"}')
stub_registry_clear() # cleanup
# NULL
cc <- stub_request("get", "https://google.com") %>%
to_return(body = NULL)
z <- crul::HttpClient$new(url = "https://google.com")$get()
expect_is(z$content, "raw")
expect_is(z$parse("UTF-8"), "character")
expect_equal(z$parse("UTF-8"), "")
stub_registry_clear() # cleanup
# FALSE
dd <- stub_request("get", "https://google.com") %>%
to_return(body = FALSE)
z <- crul::HttpClient$new(url = "https://google.com")$get()
expect_is(z$content, "raw")
expect_is(z$parse("UTF-8"), "character")
expect_equal(z$parse("UTF-8"), "")
stub_registry_clear() # cleanup
# raw
ee <- stub_request("get", "https://google.com") %>%
to_return(body = charToRaw('{"foo":"bar"}'))
z <- crul::HttpClient$new(url = "https://google.com")$get()
expect_is(z$content, "raw")
expect_is(z$parse("UTF-8"), "character")
expect_equal(z$parse("UTF-8"), '{"foo":"bar"}')
stub_registry_clear() # cleanup
})
test_that("to_return: setting body with wrong type errors well", {
## ERRORS when not of right type
expect_error(
stub_request("get", "https://google.com") %>%
to_return(body = TRUE),
"Unknown type of `body`"
)
})
webmockr/tests/testthat/test-to_return_then.R 0000644 0001762 0000144 00000004104 14113773445 021234 0 ustar ligges users context("to_return: then")
enable()
webmockr_reset()
test_that("to_return: then", {
stub <- stub_request("get", "https://httpbin.org/get?stuff=things")
to_return(stub, status = 200, body = "foobar", headers = list(a = 5))
to_return(stub, status = 200, body = "bears", headers = list(b = 6))
cli <- crul::HttpClient$new(url = "https://httpbin.org/")
x1 <- cli$get("get", query = list(stuff="things"))
x2 <- cli$get("get", query = list(stuff="things"))
x3 <- cli$get("get", query = list(stuff="things"))
# first should have foobar
expect_equal(x1$parse("UTF-8"), "foobar")
# second should have bears
expect_equal(x2$parse("UTF-8"), "bears")
# third should have bears again, and so on
expect_equal(x3$parse("UTF-8"), "bears")
})
webmockr_reset()
test_that("to_return: webmockr_reset allows multiple requests to start from beginning", {
stub <- stub_request("get", "https://httpbin.org/get?stuff=things")
to_return(stub, status = 200, body = "foobar", headers = list(a = 5))
to_return(stub, status = 200, body = "bears", headers = list(b = 6))
cli <- crul::HttpClient$new(url = "https://httpbin.org/")
x1 <- cli$get("get", query = list(stuff="things"))
x2 <- cli$get("get", query = list(stuff="things"))
expect_equal(x1$parse("UTF-8"), "foobar")
expect_equal(x2$parse("UTF-8"), "bears")
# no reset - both requests give 2nd to_return body
z1 <- cli$get("get", query = list(stuff="things"))
z2 <- cli$get("get", query = list(stuff="things"))
expect_equal(z1$parse("UTF-8"), "bears")
expect_equal(z2$parse("UTF-8"), "bears")
# RESET - requests give back expected body (have to make stub again)
webmockr_reset()
stub <- stub_request("get", "https://httpbin.org/get?stuff=things")
to_return(stub, status = 200, body = "foobar", headers = list(a = 5))
to_return(stub, status = 200, body = "bears", headers = list(b = 6))
w1 <- cli$get("get", query = list(stuff="things"))
w2 <- cli$get("get", query = list(stuff="things"))
expect_equal(w1$parse("UTF-8"), "foobar")
expect_equal(w2$parse("UTF-8"), "bears")
})
webmockr_reset()
disable()
webmockr/tests/testthat/test-HttrAdapter.R 0000644 0001762 0000144 00000026057 14360715661 020432 0 ustar ligges users context("HttrAdapter")
skip_if_not_installed("httr")
library("httr")
aa <- HttrAdapter$new()
test_that("HttrAdapter bits are correct", {
skip_on_cran()
expect_is(HttrAdapter, "R6ClassGenerator")
expect_is(aa, "HttrAdapter")
expect_null(aa$build_httr_request) # pulled out of object, so should be NULL
expect_null(aa$build_httr_response) # pulled out of object, so should be NULL
expect_is(aa$disable, "function")
expect_is(aa$enable, "function")
expect_is(aa$handle_request, "function")
expect_is(aa$remove_stubs, "function")
expect_is(aa$name, "character")
expect_equal(aa$name, "HttrAdapter")
})
test_that("HttrAdapter behaves correctly", {
skip_on_cran()
expect_message(aa$enable(), "HttrAdapter enabled!")
expect_message(aa$disable(), "HttrAdapter disabled!")
})
test_that("build_httr_request/response fail well", {
skip_on_cran()
expect_error(build_httr_request(), "argument \"x\" is missing")
expect_error(build_httr_response(), "argument \"req\" is missing")
})
test_that("HttrAdapter: works when vcr is loaded but no cassette is inserted", {
skip_on_cran()
skip_if_not_installed("vcr")
webmockr::enable(adapter = "httr")
on.exit({
webmockr::disable(adapter = "httr")
unloadNamespace("vcr")
})
stub_request("get", "https://httpbin.org/get")
library("vcr")
# works when no cassette is loaded
expect_silent(x <- httr::GET("https://httpbin.org/get"))
expect_is(x, "response")
# # works when empty cassette is loaded
vcr::vcr_configure(dir = tempdir())
vcr::insert_cassette("empty")
expect_silent(x <- httr::GET("https://httpbin.org/get"))
vcr::eject_cassette("empty")
expect_is(x, "response")
})
# library(httr)
# z <- GET("https://httpbin.org/get")
# httr_obj <- z$request
# save(httr_obj, file = "tests/testthat/httr_obj.rda")
context("HttrAdapter: date slot")
test_that("HttrAdapter date slot works", {
skip_on_cran()
skip_if_not_installed("vcr")
library("vcr")
path <- file.path(tempdir(), "foobar")
vcr::vcr_configure(dir = path)
vcr::use_cassette("test-date", httr::GET("https://httpbin.org/get"))
# list.files(path)
# readLines(file.path(path, "test-date.yml"))
vcr::insert_cassette("test-date")
x <- httr::GET("https://httpbin.org/get")
# $date is of correct format
expect_output(print(x), "Date")
expect_is(x$date, "POSIXct")
expect_is(format(x$date, "%Y-%m-%d %H:%M"), "character")
# $headers$date is a different format
expect_is(x$headers$date, "character")
expect_error(format(x$headers$date, "%Y-%m-%d %H:%M"), "invalid 'trim'")
vcr::eject_cassette("test-date")
# cleanup
unlink(path, recursive = TRUE)
})
context("HttrAdapter: insensitive headers, webmockr flow")
test_that("HttrAdapter insensitive headers work, webmockr flow", {
skip_on_cran()
unloadNamespace("vcr")
httr_mock()
stub_registry_clear()
invisible(stub_request("get", uri = "https://httpbin.org/get") %>%
to_return(
body = list(foo = "bar"),
headers = list("Content-Type" = "application/json")
))
x <- httr::GET("https://httpbin.org/get")
expect_equal(x$headers[["content-type"]], "application/json")
expect_is(httr::content(x), "list")
expect_is(httr::content(x, "text", encoding = "UTF-8"), "character")
stub_registry_clear()
httr_mock(FALSE)
})
context("HttrAdapter: insensitive headers, vcr flow")
test_that("HttrAdapter insensitive headers work, vcr flow", {
skip_on_cran()
skip_if_not_installed("vcr")
library("vcr")
path <- file.path(tempdir(), "helloworld")
vcr::vcr_configure(dir = path)
vcr::use_cassette("test-date", GET("https://httpbin.org/get"))
vcr::insert_cassette("test-date")
x <- httr::GET("https://httpbin.org/get")
expect_equal(x$headers[["content-type"]], "application/json")
expect_is(httr::content(x), "list")
expect_is(httr::content(x, "text", encoding = "UTF-8"), "character")
vcr::eject_cassette("test-date")
# cleanup
unlink(path, recursive = TRUE)
})
context("HttrAdapter: works with real data")
test_that("HttrAdapter works", {
skip_on_cran()
skip_if_not_installed("vcr")
load("httr_obj.rda")
# load("tests/testthat/httr_obj.rda")
res <- HttrAdapter$new()
# with vcr message
library("vcr")
expect_error(
res$handle_request(httr_obj),
"There is currently no cassette in use"
)
# with webmockr message
# unload vcr
unloadNamespace("vcr")
expect_error(
res$handle_request(httr_obj),
"Real HTTP connections are disabled.\nUnregistered request:\n GET: https://httpbin.org/get"
)
invisible(stub_request("get", "https://httpbin.org/get"))
aa <- res$handle_request(httr_obj)
expect_is(res, "HttrAdapter")
expect_is(aa, "response")
expect_equal(aa$request$method, "GET")
expect_equal(aa$url, "https://httpbin.org/get")
# no response headers
expect_equal(length(aa$headers), 0)
expect_equal(length(aa$all_headers), 1)
# with headers
# clear registry
stub_registry_clear()
# stub with headers
x <- stub_request("get", "https://httpbin.org/get")
x <- to_return(x, headers = list("User-Agent" = "foo-bar"))
aa <- res$handle_request(httr_obj)
expect_is(res, "HttrAdapter")
expect_is(aa, "response")
expect_equal(aa$request$method, "GET")
expect_equal(aa$url, "https://httpbin.org/get")
# has headers and all_headers
expect_equal(length(aa$headers), 1)
expect_is(aa$headers, "list")
expect_named(aa$headers, "user-agent")
expect_equal(length(aa$all_headers), 1)
expect_is(aa$all_headers, "list")
expect_named(aa$all_headers, NULL)
expect_named(aa$all_headers[[1]], c("status", "version", "headers"))
# stub with redirect headers
my_url <- "https://doi.org/10.1007/978-3-642-40455-9_52-1"
x <- stub_request("get", my_url)
x <- to_return(x, status = 302, headers =
list(
status = 302,
location = "http://link.springer.com/10.1007/978-3-642-40455-9_52-1"
)
)
httr_obj$url <- my_url
res <- HttrAdapter$new()
aa <- res$handle_request(httr_obj)
expect_equal(aa$request$method, "GET")
expect_equal(aa$url, my_url)
expect_equal(aa$status_code, 302)
# has headers and all_headers
expect_equal(length(aa$headers), 2)
expect_is(aa$headers, "list")
expect_equal(sort(names(aa$headers)), c("location", "status"))
expect_equal(length(aa$all_headers), 1)
expect_equal(length(aa$all_headers[[1]]), 3)
expect_is(aa$all_headers, "list")
expect_is(aa$all_headers[[1]], "list")
expect_named(aa$all_headers, NULL)
expect_equal(sort(names(aa$all_headers[[1]])),
c("headers", "status", "version"))
})
test_that("HttrAdapter works with httr::authenticate", {
skip_on_cran()
unloadNamespace("vcr")
httr_mock()
# httr_mock(FALSE)
# webmockr_allow_net_connect()
stub_registry_clear()
# stub_registry()
# request_registry()
z <- stub_request("get", uri = "https://httpbin.org/basic-auth/foo/bar") %>%
to_return(
body = list(foo = "bar"),
headers = list("Content-Type" = "application/json")
)
# x <- httr::GET("https://httpbin.org/basic-auth/foo/bar", httr::authenticate("foo", "bar"))
# httr_obj_auth <- x$request
# save(httr_obj_auth, file = "tests/testthat/httr_obj_auth.rda", version = 2)
# load("tests/testthat/httr_obj_auth.rda")
# httr::content(x)
# mocked httr requests with auth work
# before the fixes in HttrAdapter: a real request through webmockr would
# not work with authenticate
x <- httr::GET("https://httpbin.org/basic-auth/foo/bar", httr::authenticate("foo", "bar"))
expect_is(x, "response")
expect_equal(httr::content(x), list(foo = "bar"))
expect_equal(x$headers, structure(list(`content-type` = "application/json"),
class = c("insensitive", "list")))
expect_equal(x$status_code, 200)
# HttrAdapter works on requests with auth
load("httr_obj_auth.rda")
zz <- HttrAdapter$new()
z <- zz$handle_request(httr_obj_auth)
expect_is(z, "response")
expect_equal(httr::content(z), list(foo = "bar"))
expect_equal(z$headers, structure(list(`content-type` = "application/json"),
class = c("insensitive", "list")))
expect_equal(z$status_code, 200)
})
test_that("httr works with webmockr_allow_net_connect", {
skip_on_cran()
httr_mock()
stub_registry_clear()
z <- stub_request("get", uri = "https://httpbin.org/get?stuff=things") %>%
to_return(body = "yum=cheese")
x <- httr::GET("https://httpbin.org/get?stuff=things")
expect_true(httr::content(x, "text", encoding="UTF-8") == "yum=cheese")
# allow net connect - stub still exists though - so not a real request
webmockr_allow_net_connect()
z <- httr::GET("https://httpbin.org/get?stuff=things")
expect_true(httr::content(z, "text", encoding="UTF-8") == "yum=cheese")
# allow net connect - stub now gone - so real request should happen
stub_registry_clear()
w <- httr::GET("https://httpbin.org/get?stuff=things")
expect_false(httr::content(w, "text", encoding="UTF-8") == "yum=cheese")
# disable net connect - now real requests can't be made
webmockr_disable_net_connect()
expect_error(httr::GET("https://httpbin.org/get?stuff=things"),
"Real HTTP connections are disabled")
})
test_that("httr requests with bodies work", {
skip_on_cran()
httr_mock()
stub_registry_clear()
z <- stub_request("post", uri = "https://httpbin.org/post") %>%
to_return(body = "asdffsdsdf")
x <- httr::POST("https://httpbin.org/post", body = list(stuff = "things"))
expect_true(httr::content(x, "text", encoding="UTF-8") == "asdffsdsdf")
# now with allow net connect
stub_registry_clear()
webmockr_allow_net_connect()
x <- httr::POST("https://httpbin.org/post", body = list(stuff = "things"))
expect_identical(httr::content(x)$form, list(stuff = "things"))
webmockr_disable_net_connect()
})
test_that("httr requests with nested list bodies work", {
skip_on_cran()
httr_mock()
stub_registry_clear()
body = list(id = ' ', method = 'x', params = list(pwd = 'p', user = 'a'))
z <- stub_request("post", uri = "https://httpbin.org/post") %>%
wi_th(body = body) %>%
to_return(body = "asdffsdsdf")
x <- httr::POST("https://httpbin.org/post", body = body)
expect_true(httr::content(x, "text", encoding="UTF-8") == "asdffsdsdf")
# now with allow net connect
stub_registry_clear()
webmockr_allow_net_connect()
x <- httr::POST("https://httpbin.org/post",
body = jsonlite::toJSON(body), httr::content_type_json())
expect_equal(
jsonlite::fromJSON(rawToChar(x$content))$json,
body)
webmockr_disable_net_connect()
})
test_that("httr requests with JSON-encoded bodies work", {
skip_on_cran()
on.exit(disable(adapter = "httr"))
enable(adapter = "httr")
stub_registry_clear()
body <- list(foo = "bar")
z <- stub_request("post", uri = "https://httpbin.org/post") %>%
wi_th(body = jsonlite::toJSON(body, auto_unbox = TRUE))
# encoded body works
res <- httr::POST("https://httpbin.org/post", body = body, encode = "json")
expect_is(res, "response")
# encoded but modified body fails
expect_error(
httr::POST("https://httpbin.org/post", body = list(foo = "bar1"), encode = "json"),
"Unregistered request"
)
# unencoded body fails
expect_error(
httr::POST("https://httpbin.org/post", body = body),
"Unregistered request"
)
})
webmockr/tests/testthat/test-writing-to-disk-write_disk_path.R 0000644 0001762 0000144 00000006644 14113773445 024421 0 ustar ligges users context("write_disk_path behavior")
# crul
test_that("with crul", {
skip_on_cran()
skip_if_not_installed("vcr")
library("vcr")
dir <- tempdir()
invisible(vcr_configure(dir = dir))
library(crul)
f <- tempfile(fileext = ".json")
webmockr_net_connect_allowed()
# path not set
expect_error(
suppressWarnings(use_cassette("write_disk_path_not_set_crul_error", {
out <- HttpClient$new("https://httpbin.org/get")$get(disk = f)
})),
"write_disk_path must be given"
)
# now set path
wdp <- file.path(dir, "files")
invisible(vcr_configure(dir = dir, write_disk_path = wdp))
expect_error(
use_cassette("write_disk_path_not_set_crul_noerror", {
out <- HttpClient$new("https://httpbin.org/get")$get(disk = f)
}),
NA
)
# cleanup
unlink(f)
unlink(wdp, TRUE)
unlink(file.path(dir, "write_disk_path_not_set_crul_error.yml"))
unlink(file.path(dir, "write_disk_path_not_set_crul_noerror.yml"))
webmockr_disable_net_connect()
unloadNamespace("vcr")
})
test_that("if relative path set its not expanded to full path anymore", {
skip_on_cran()
skip_if_not_installed("vcr")
library("vcr")
dir <- tempdir()
f <- "stuff.json"
wdp <- "../files"
invisible(vcr_configure(dir = dir, write_disk_path = wdp))
og <- getwd()
setwd(dir)
on.exit(setwd(og))
expect_error(
use_cassette("write_disk_path_is_relative", {
out <- HttpClient$new("https://httpbin.org/get?foo=foo")$get(disk = f)
}),
NA
)
txt <- readLines(file.path(dir, "write_disk_path_is_relative.yml"))
expect_true(any(grepl("../files/stuff.json", txt)))
# cleanup
# unlink("files", recursive = TRUE)
unlink("stuff.json")
webmockr_disable_net_connect()
unloadNamespace("vcr")
})
# httr
test_that("with httr", {
skip_on_cran()
skip_if_not_installed("vcr")
library("vcr")
enable()
dir <- tempdir()
invisible(vcr_configure(dir = dir))
library(httr)
f <- tempfile(fileext = ".json")
webmockr_net_connect_allowed()
# path not set
expect_error(
suppressWarnings(use_cassette("write_disk_path_not_set_crul_error", {
out <- GET("https://httpbin.org/get", write_disk(f))
})),
"write_disk_path must be given"
)
# now set path
f <- tempfile(fileext = ".json")
wdp <- file.path(dir, "files")
invisible(vcr_configure(dir = dir, write_disk_path = wdp))
expect_error(
use_cassette("write_disk_path_not_set_crul_noerror", {
out <- GET("https://httpbin.org/get", write_disk(f))
}),
NA
)
# cleanup
unlink(f)
unlink(wdp, TRUE)
unlink(file.path(dir, "write_disk_path_not_set_crul_error.yml"))
unlink(file.path(dir, "write_disk_path_not_set_crul_noerror.yml"))
webmockr_disable_net_connect()
unloadNamespace("vcr")
})
test_that("if relative path set its not expanded to full path anymore: httr", {
skip_on_cran()
skip_if_not_installed("vcr")
library("vcr")
dir <- tempdir()
f <- "stuff.json"
wdp <- "../files"
invisible(vcr_configure(dir = dir, write_disk_path = wdp))
og <- getwd()
setwd(dir)
on.exit(setwd(og))
expect_error(
use_cassette("write_disk_path_is_relative", {
out <- GET("https://httpbin.org/get?foo=foo", write_disk(f))
}),
NA
)
txt <- readLines(file.path(dir, "write_disk_path_is_relative.yml"))
expect_true(any(grepl("../files/stuff.json", txt)))
# cleanup
# unlink("files", recursive = TRUE)
unlink("stuff.json")
webmockr_disable_net_connect()
unloadNamespace("vcr")
})
webmockr/tests/testthat/crul_body_upload_list.rda 0000644 0001762 0000144 00000000622 14113773445 022147 0 ustar ligges users ‹ uRKKÃ@N“¶Ò€(ˆà1O¥Í[½P(
¥Hoa“l’ÕmvÙÝ`ã¯n^’†öÌÎã›ï›a–Ïësmjš¦kF_×tC=»ºúu´®6PöÒç)q=dnÊ…K°ªþB%{•í¨ï¼ºŠ¥dâ€Üz8±) £9n¯y/$Z`#åD™]+Ü-ñ_ÞìxÛÜ×{¾jîíÛ©íX|\xS{bå£ Ç~°R‡þ[aõ=Žò=Hâ0B‰,Åü7ÉN£ÌFV€B%j%ÈÁ>”˜&àSÐddI´•`»!#«™+C0<¢êlæûˆÉñKâÓ 'Qî—á†:µ
ãÊX`CžW®þMù— K;¬ð¸PB˜Øw`‰M¹ ʃ§ùj¶š¿¿µz›ÅHŒ@œ]*”qý–Cí#ð í#„”oÜTη׹.êd’½æU›Ú Ó òN(Ë·.j7F0Pó×µ!F$Õîþ à%³! webmockr/tests/testthat/test-to_raise.R 0000644 0001762 0000144 00000002755 14113773445 020014 0 ustar ligges users context("to_raise")
stub_registry()$remove_all_request_stubs()
test_that("no stubs exist before stub_request called", {
expect_equal(length(stub_registry()$request_stubs), 0)
})
library(fauxpas)
aa <- stub_request("get", "https://httpbin.org/get") %>% to_raise(HTTPAccepted)
test_that("stub_request bits are correct", {
expect_is(aa, "StubbedRequest")
expect_null(aa$body)
expect_null(aa$host)
expect_null(aa$response)
expect_null(aa$query)
expect_null(aa$request_headers)
expect_null(aa$response_headers)
# expect_false(aa$timeout) # timeout will be removed in StubbedRequest
expect_is(aa$method, "character")
expect_equal(aa$method, "get")
expect_is(aa$uri, "character")
expect_equal(aa$uri, "https://httpbin.org/get")
# to_raise expected stuff
rr <- aa$responses_sequences[[1]]
expect_true(rr$raise)
expect_is(rr$exceptions, "list")
expect_is(rr$exceptions[[1]], "R6ClassGenerator")
expect_equal(rr$exceptions[[1]]$classname, "HTTPAccepted")
expect_equal(rr$exceptions[[1]]$new()$status_code, 202)
})
test_that("stubs exist after stub_request called", {
expect_equal(length(stub_registry()$request_stubs), 1)
})
test_that("stub_request fails well", {
expect_error(to_raise(), "argument \".data\" is missing")
expect_error(to_raise(5), ".data must be of class StubbedRequest")
# exception clases
zzz <- stub_request("get", "https://httpbin.org/get")
expect_error(to_raise(zzz, "foo"),
"all objects must be error classes from fauxpas")
})
webmockr/tests/testthat/test-stub_requests_crul.R 0000644 0001762 0000144 00000007252 14113773445 022141 0 ustar ligges users context("stub_request and crul: get")
library(crul)
crul::mock()
# clear any stubs
stub_registry_clear()
test_that("stub_request works well: get requests", {
skip_on_cran()
# before any stubs made
## 0 stubs
expect_equal(length(stub_registry()$request_stubs), 0)
x <- crul::HttpClient$new(url = "https://httpbin.org")
ms1 <- get_err_mssg(x$get('get', query = list(foo = "bar", a = 5)))
expect_error(
x$get('get', query = list(foo = "bar", a = 5)),
re_escape(ms1)
)
ms2 <- get_err_mssg(x$get('get', query = list(foo = "bar", stuff = FALSE)))
expect_error(
x$get('get', query = list(foo = "bar", stuff = FALSE)),
re_escape(ms2)
)
ms3 <- get_err_mssg(x$get('get', query = list(foo = "bar")))
expect_error(
x$get('get', query = list(foo = "bar")),
re_escape(ms3)
)
# after a stub made
stub_request("get", "https://httpbin.org/get?foo=bar&a=5") %>%
wi_th(headers = list(
'Accept-Encoding' = 'gzip, deflate',
'Accept' = 'application/json, text/xml, application/xml, */*')
)
## 1 stub
expect_equal(length(stub_registry()$request_stubs), 1)
# the matching request works
z <- x$get('get', query = list(foo = "bar", a = 5))
expect_is(z, "HttpResponse")
expect_equal(z$url, "https://httpbin.org/get?foo=bar&a=5")
# but the others still do not work cause they dont match the stub
ms2 <- get_err_mssg(x$get('get', query = list(foo = "bar", stuff = FALSE)))
expect_error(x$get('get', query = list(foo = "bar", stuff = FALSE)), re_escape(ms2))
ms3 <- get_err_mssg(x$get('get', query = list(foo = "bar")))
expect_error(x$get('get', query = list(foo = "bar")), re_escape(ms3))
# a stub for the second request
stub_request("get", "https://httpbin.org/get?foo=bar&stuff=FALSE") %>%
wi_th(headers = list(
'Accept-Encoding' = 'gzip, deflate',
'Accept' = 'application/json, text/xml, application/xml, */*')
)
## 2 stubs now
expect_equal(length(stub_registry()$request_stubs), 2)
# the other request now works
w <- x$get('get', query = list(foo = "bar", stuff = FALSE))
expect_is(w, "HttpResponse")
expect_equal(w$url, "https://httpbin.org/get?foo=bar&stuff=FALSE")
# but the others still do not work cause they dont match the stub
ms4 <- get_err_mssg(x$get('get', query = list(foo = "bar")))
expect_error(x$get('get', query = list(foo = "bar")), re_escape(ms4))
})
# clear any stubs again
stub_registry_clear()
context("stub_request and crul: post")
test_that("stub_request works well: post requests", {
skip_on_cran()
# before any stubs made
## 0 stubs
expect_equal(length(stub_registry()$request_stubs), 0)
x <- crul::HttpClient$new(url = "https://httpbin.org")
ms1 <- get_err_mssg(x$post('post', body = list(foo = "bar", a = 5)))
expect_error(
x$post('post', body = list(foo = "bar", a = 5)),
re_escape(ms1)
)
# after a stub made
stub_request("post", "https://httpbin.org/post") %>%
wi_th(headers = list(
'Accept-Encoding' = 'gzip, deflate',
'Accept' = 'application/json, text/xml, application/xml, */*'),
body = list(foo = "bar", a = 5)
)
## 1 stub
expect_equal(length(stub_registry()$request_stubs), 1)
# the matching request works
z <- x$post('post', body = list(foo = "bar", a = 5))
expect_is(z, "HttpResponse")
expect_equal(z$url, "https://httpbin.org/post")
# but the others still do not work cause they dont match the stub
ms2 <- get_err_mssg(x$post('post', query = list(foo = "bar", stuff = FALSE)))
expect_error(x$post('post', query = list(foo = "bar", stuff = FALSE)), re_escape(ms2))
ms3 <- get_err_mssg(x$post('post', query = list(foo = "bar")))
expect_error(x$post('post', query = list(foo = "bar")), re_escape(ms3))
})
webmockr/tests/testthat/test-Adapter.R 0000644 0001762 0000144 00000000332 14113773445 017554 0 ustar ligges users context("Adapter class")
test_that("Adapter class can't be instantiated", {
expect_is(Adapter, "R6ClassGenerator")
expect_error(
Adapter$new(),
"Adapter parent class should not be called directly"
)
})
webmockr/tests/testthat/test-request_registry.R 0000644 0001762 0000144 00000001515 14113773445 021620 0 ustar ligges users context("request_registry")
test_that("request_registry: structure", {
request_registry_clear()
expect_is(request_registry, "function")
expect_is(request_registry(), "RequestRegistry")
enable()
stub_request("get", "https://httpbin.org/get") %>%
to_return(body = "success!", status = 200)
invisible(
crul::HttpClient$new(url = "https://httpbin.org")$get("get")
)
disable()
x <- request_registry()
expect_is(x, "RequestRegistry")
expect_is(x$clone, "function")
expect_is(x$print, "function")
expect_is(x$register_request, "function")
expect_null(x$request)
expect_is(x$request_signatures, "HashCounter")
expect_is(x$reset, "function")
expect_is(x$request_signatures$hash, "list")
expect_match(names(x$request_signatures$hash), "GET")
expect_is(x$request_signatures$hash[[1]]$count, 'numeric')
})
webmockr/tests/testthat/crul_obj.rda 0000644 0001762 0000144 00000000366 14113773445 017372 0 ustar ligges users ‹ mPMÂ0ëüšA¼yõàZ'âÇÙ_ài7é¶²)u•¶ûûj»™CJš¤y/yÍå^è€!p ]Ç\=palü(–¿Šèn 3[¬½ÅL[¸E¦õó„11å™Pút$„à”i 47€We?Íû1§Ju:MâBòkF󄳜œ>XÃqêGd(u8h3£RÌW½W–*kÃVüY xïï6>YÊu™þviw‰˜s