#> method: get
#> uri: https://httpbin.org/get
#> with:
#> query:
#> body:
#> request_headers: Accept=application/json, te...
#> to_return:
#> status: 418
#> body: I'm a teapot!!!
#> response_headers: im_a=teapot
#> should_timeout: FALSE
#> should_raise: FALSE
```
now returns mocked response
```r
(res <- GET("https://httpbin.org/get"))
res$status_code
#> [1] 418
res$headers
#> $im_a
#> [1] "teapot"
```
## Writing to disk
Write to a file before mocked request
```r
## make a temp file
f <- tempfile(fileext = ".json")
## write something to the file
cat("{\"hello\":\"world\"}\n", file = f)
readLines(f)
#> [1] "{\"hello\":\"world\"}"
## make the stub
invisible(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)
readLines(file(f))
#> [1] "{\"hello\":\"world\"}"
```
OR - you can use `mock_file()` to have `webmockr` handle file and contents
```r
g <- tempfile(fileext = ".json")
## make the stub
invisible(stub_request("get", "https://httpbin.org/get") %>%
to_return(body = mock_file(g, "{\"hello\":\"mars\"}\n")))
## make a request
out <- crul::HttpClient$new("https://httpbin.org/get")$get(disk = g)
readLines(out$content)
#> [1] "{\"hello\":\"world\"}"
```
Writing to disk is supported in both `crul` and `httr`
## Meta
* Please [report any issues or bugs](https://github.com/ropensci/webmockr/issues).
* License: MIT
* Get citation information for `webmockr` in R doing `citation(package = 'webmockr')`
* Please note that this project is released with a [Contributor Code of Conduct][coc].
By participating in this project you agree to abide by its terms.
[](https://ropensci.org)
[vcr]: https://github.com/ropensci/vcr
[coc]: https://github.com/ropensci/webmockr/blob/master/CODE_OF_CONDUCT.md
webmockr/man/ 0000755 0001762 0000144 00000000000 13571575147 012655 5 ustar ligges users webmockr/man/enable.Rd 0000644 0001762 0000144 00000001740 13457524755 014376 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())
enabled(adapter = "crul")
disable(adapter = NULL, options = list())
}
\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.}
}
\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 00000004522 13571510275 015361 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$put("foo bar")
x$put("foo bar")
x$put("hello world")
x$put("hello world")
x$put("hello world")
x$hash
}
\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}
}
\if{html}{\out{
}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-put}{\code{HashCounter$put()}}
\item \href{#method-get}{\code{HashCounter$get()}}
\item \href{#method-clone}{\code{HashCounter$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\subsection{Method \code{put()}}{
Register a request by it's key
\subsection{Usage}{
\if{html}{\out{}}\preformatted{HashCounter$put(key)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{key}}{a character string of the request, serialized from
\link{CrulAdapter} or another adapter}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; registers request and iterates
internal counter
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\subsection{Method \code{get()}}{
Get a request by key
\subsection{Usage}{
\if{html}{\out{}}\preformatted{HashCounter$get(key)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{key}}{a character string of the request, serialized from
\link{CrulAdapter} or another adapter}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
(character) an http request as a string
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\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 00000007351 13571350262 015664 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 uri, partial, or a
regular expression to match many incantations of a uri. required.}
\item{uri_regex}{(character) A URI represented as regex. See examples}
}
\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).
}
\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'))
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'))
# 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))
# 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 13145163224 017166 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 13241473156 015321 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 00000001662 13241664137 014746 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()}}
}
webmockr/man/RequestPattern.Rd 0000644 0001762 0000144 00000010011 13571506002 016103 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 = "https://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 = "https://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()
# 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)
}
}
\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-new}{\code{RequestPattern$new()}}
\item \href{#method-matches}{\code{RequestPattern$matches()}}
\item \href{#method-to_s}{\code{RequestPattern$to_s()}}
\item \href{#method-clone}{\code{RequestPattern$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\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{}}
\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{}}
\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{}}
\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 13415716025 017043 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 00000010272 13571521416 015231 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")
z$matches("http://foobar.com/")
# default ports
(z <- UriPattern$new(pattern = "http://foobar.com"))
z$matches("http://foobar.com:80")
z$matches("http://foobar.com:80/")
z$matches("http://foobar.com:443")
z$matches("http://foobar.com:443/")
# user info
(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")
z$matches("http://example.com")
z$matches("http://tramples.net")
# add query parameters
(z <- UriPattern$new(pattern = "http://foobar.com"))
z$add_query_params(list(pizza = "cheese", cheese = "cheddar"))
z$pattern
(z <- UriPattern$new(pattern = "http://foobar.com"))
z$pattern
z$add_query_params(list(pizza = "deep dish", cheese = "cheddar"))
z$pattern
# any pattern
(z <- UriPattern$new(regex_pattern = ".+"))
z$regex
z$pattern
z$matches("http://stuff.com")
z$matches("https://stuff.com")
z$matches("https://stuff.com/stff")
z$matches("https://stuff.com/apple?bears=3")
}
\keyword{internal}
\section{Public fields}{
\if{html}{\out{}}
\describe{
\item{\code{pattern}}{(character) pattern holder}
\item{\code{regex}}{a logical}
}
\if{html}{\out{
}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-new}{\code{UriPattern$new()}}
\item \href{#method-matches}{\code{UriPattern$matches()}}
\item \href{#method-add_query_params}{\code{UriPattern$add_query_params()}}
\item \href{#method-to_s}{\code{UriPattern$to_s()}}
\item \href{#method-clone}{\code{UriPattern$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\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{}}
\subsection{Method \code{matches()}}{
Match a list of headers against that stored
\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{}}
\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{}}
\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{}}
\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/CrulAdapter.Rd 0000644 0001762 0000144 00000005224 13571521323 015340 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/adapter-crul.R
\name{CrulAdapter}
\alias{CrulAdapter}
\title{CrulAdapter}
\description{
\pkg{crul} library adapter
}
\details{
This adapter modifies \pkg{crul} to allow mocking HTTP requests
}
\seealso{
Other http_lib_adapters:
\code{\link{HttrAdapter}}
}
\concept{http_lib_adapters}
\section{Public fields}{
\if{html}{\out{}}
\describe{
\item{\code{name}}{adapter name}
}
\if{html}{\out{
}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-enable}{\code{CrulAdapter$enable()}}
\item \href{#method-disable}{\code{CrulAdapter$disable()}}
\item \href{#method-handle_request}{\code{CrulAdapter$handle_request()}}
\item \href{#method-remove_crul_stubs}{\code{CrulAdapter$remove_crul_stubs()}}
\item \href{#method-clone}{\code{CrulAdapter$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\subsection{Method \code{enable()}}{
Enable the adapter
\subsection{Usage}{
\if{html}{\out{}}\preformatted{CrulAdapter$enable()}\if{html}{\out{
}}
}
\subsection{Returns}{
\code{TRUE}, invisibly
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\subsection{Method \code{disable()}}{
Disable the adapter
\subsection{Usage}{
\if{html}{\out{}}\preformatted{CrulAdapter$disable()}\if{html}{\out{
}}
}
\subsection{Returns}{
\code{FALSE}, invisibly
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\subsection{Method \code{handle_request()}}{
All logic for handling a request
\subsection{Usage}{
\if{html}{\out{}}\preformatted{CrulAdapter$handle_request(req)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{req}}{a crul request}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
various outcomes
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\subsection{Method \code{remove_crul_stubs()}}{
Remove all crul stubs
\subsection{Usage}{
\if{html}{\out{}}\preformatted{CrulAdapter$remove_crul_stubs()}\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; removes all crul request stubs
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\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{
}}
}
}
}
webmockr/man/build_httr_response.Rd 0000644 0001762 0000144 00000000530 13415716025 017205 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/HttrAdapter.Rd 0000644 0001762 0000144 00000007215 13571521323 015356 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/adapter-httr.R
\name{HttrAdapter}
\alias{HttrAdapter}
\title{HttrAdapter}
\description{
\code{httr} library adapter
}
\details{
This adapter modifies \pkg{httr} to allow mocking HTTP requests
}
\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)
}
}
}
\seealso{
Other http_lib_adapters:
\code{\link{CrulAdapter}}
}
\concept{http_lib_adapters}
\section{Public fields}{
\if{html}{\out{}}
\describe{
\item{\code{name}}{adapter name}
}
\if{html}{\out{
}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-enable}{\code{HttrAdapter$enable()}}
\item \href{#method-disable}{\code{HttrAdapter$disable()}}
\item \href{#method-handle_request}{\code{HttrAdapter$handle_request()}}
\item \href{#method-remove_httr_stubs}{\code{HttrAdapter$remove_httr_stubs()}}
\item \href{#method-clone}{\code{HttrAdapter$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\subsection{Method \code{enable()}}{
Enable the adapter
\subsection{Usage}{
\if{html}{\out{}}\preformatted{HttrAdapter$enable()}\if{html}{\out{
}}
}
\subsection{Returns}{
\code{TRUE}, invisibly
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\subsection{Method \code{disable()}}{
Disable the adapter
\subsection{Usage}{
\if{html}{\out{}}\preformatted{HttrAdapter$disable()}\if{html}{\out{
}}
}
\subsection{Returns}{
\code{FALSE}, invisibly
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\subsection{Method \code{handle_request()}}{
All logic for handling a request
\subsection{Usage}{
\if{html}{\out{}}\preformatted{HttrAdapter$handle_request(req)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{req}}{a httr request}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
various outcomes
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\subsection{Method \code{remove_httr_stubs()}}{
Remove all crul stubs
\subsection{Usage}{
\if{html}{\out{}}\preformatted{HttrAdapter$remove_httr_stubs()}\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; removes all httr request stubs
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\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{
}}
}
}
}
webmockr/man/webmockr_enable-defunct.Rd 0000644 0001762 0000144 00000000405 13242424030 017663 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 13571350775 016055 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 00000003373 13457524755 014266 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}.}
\item{.list}{named list, has to be one of 'query', 'body',
and/or 'headers'. 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, and/or request headers
}
\details{
\code{with} is a function in the \code{base} package, so we went with
\code{wi_th}
Values for query, body, and headers:
\itemize{
\item query: (list) a named list
\item body: various, including character string, list, raw, numeric, etc
\item headers: (list) a named list
}
}
\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)
# 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")))
}
webmockr/man/mocking-disk-writing.Rd 0000644 0001762 0000144 00000004046 13571350775 017206 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/to_return_-defunct.Rd 0000644 0001762 0000144 00000000366 13457524755 016761 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 13457524755 016045 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 00000001551 13457524755 016352 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{Stubbing and setting expectations on HTTP requests}
\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}
}
\keyword{package}
webmockr/man/to_return.Rd 0000644 0001762 0000144 00000004031 13571350262 015150 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())
}
\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}
}
\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()}}
}
\examples{
# first, make a stub object
(req <- stub_request("post", "https://httpbin.org/post"))
# add status, body and/or headers
to_return(req, status = 200)
to_return(req, body = "stuff")
to_return(req, body = list(a = list(b = "world")))
to_return(req, headers = list(a = 5))
to_return(req, status = 200, body = "stuff", headers = list(a = 5))
# .list - pass in a named list instead
to_return(req, .list = list(body = list(foo = "bar")))
}
webmockr/man/httr_mock.Rd 0000644 0001762 0000144 00000000643 13415716025 015126 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}
\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 sets a callback that routes httr request
through webmockr
}
\description{
Turn on httr mocking
}
webmockr/man/webmockr_disable-defunct.Rd 0000644 0001762 0000144 00000000410 13242424030 020034 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 00000021077 13571507246 014744 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-new}{\code{Response$new()}}
\item \href{#method-print}{\code{Response$print()}}
\item \href{#method-set_url}{\code{Response$set_url()}}
\item \href{#method-get_url}{\code{Response$get_url()}}
\item \href{#method-set_request_headers}{\code{Response$set_request_headers()}}
\item \href{#method-get_request_headers}{\code{Response$get_request_headers()}}
\item \href{#method-set_response_headers}{\code{Response$set_response_headers()}}
\item \href{#method-get_respone_headers}{\code{Response$get_respone_headers()}}
\item \href{#method-set_body}{\code{Response$set_body()}}
\item \href{#method-get_body}{\code{Response$get_body()}}
\item \href{#method-set_status}{\code{Response$set_status()}}
\item \href{#method-get_status}{\code{Response$get_status()}}
\item \href{#method-set_exception}{\code{Response$set_exception()}}
\item \href{#method-get_exception}{\code{Response$get_exception()}}
\item \href{#method-clone}{\code{Response$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\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{}}
\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{}}
\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{}}
\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{}}
\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{}}
\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{}}
\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{}}
\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{}}
\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{}}
\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{}}
\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{}}
\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{}}
\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{}}
\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{}}
\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/build_crul_request.Rd 0000644 0001762 0000144 00000000507 13145357177 017040 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 00000002570 13571575147 016570 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 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 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 13457524755 016400 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 00000001147 13571350775 017220 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{Clear the stub registry}
\usage{
stub_registry_clear()
}
\value{
nothing, well technically an empty list invisibly, but
it's not anything useful
}
\description{
Clear all stubs
}
\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 13107235041 014057 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 13571350775 015076 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 00000005737 13571514572 016334 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()
x$register_request(request = "GET http://scottchamberlain.info")
x$register_request(request = "GET http://scottchamberlain.info")
x$register_request(request = "POST https://httpbin.org/post")
# print method to list requests
x
# hashes, and number of times each requested
x$request_signatures$hash
# 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-print}{\code{RequestRegistry$print()}}
\item \href{#method-reset}{\code{RequestRegistry$reset()}}
\item \href{#method-register_request}{\code{RequestRegistry$register_request()}}
\item \href{#method-clone}{\code{RequestRegistry$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\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{}}
\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{}}
\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
\link{CrulAdapter} or another adapter}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; registers the request
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\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 00000004317 13571350775 017021 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,
net_http_connect_on_start = FALSE,
show_stubbing_instructions = FALSE,
query_values_notation = FALSE,
show_body_diff = FALSE
)
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{net_http_connect_on_start}{(logical) Default: \code{FALSE}. ignored for
now}
\item{show_stubbing_instructions}{(logical) Default: \code{FALSE}. ignored for
now}
\item{query_values_notation}{(logical) Default: \code{FALSE}. ignored for
now}
\item{show_body_diff}{(logical) Default: \code{FALSE}. ignored for
now}
\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 00000005062 13571506002 015362 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
z <- BodyPattern$new(pattern = list(foo = "bar"))
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-new}{\code{BodyPattern$new()}}
\item \href{#method-matches}{\code{BodyPattern$matches()}}
\item \href{#method-to_s}{\code{BodyPattern$to_s()}}
\item \href{#method-clone}{\code{BodyPattern$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\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{}}
\subsection{Method \code{matches()}}{
Match a list of headers against that stored
\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{}}
\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{}}
\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 13270161742 017133 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 00000012667 13571514572 015621 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-print}{\code{StubRegistry$print()}}
\item \href{#method-register_stub}{\code{StubRegistry$register_stub()}}
\item \href{#method-find_stubbed_request}{\code{StubRegistry$find_stubbed_request()}}
\item \href{#method-request_stub_for}{\code{StubRegistry$request_stub_for()}}
\item \href{#method-remove_request_stub}{\code{StubRegistry$remove_request_stub()}}
\item \href{#method-remove_all_request_stubs}{\code{StubRegistry$remove_all_request_stubs()}}
\item \href{#method-is_registered}{\code{StubRegistry$is_registered()}}
\item \href{#method-clone}{\code{StubRegistry$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\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{}}
\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{}}
\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{}}
\subsection{Method \code{request_stub_for()}}{
Find a stubbed request
\subsection{Usage}{
\if{html}{\out{}}\preformatted{StubRegistry$request_stub_for(request_signature)}\if{html}{\out{
}}
}
\subsection{Arguments}{
\if{html}{\out{}}
\describe{
\item{\code{request_signature}}{an object of class \link{RequestSignature}}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
logical, 1 or more
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\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{}}
\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{}}
\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{}}
\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/HttpLibAdapaterRegistry.Rd 0000644 0001762 0000144 00000004154 13571352126 017677 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-print}{\code{HttpLibAdapaterRegistry$print()}}
\item \href{#method-register}{\code{HttpLibAdapaterRegistry$register()}}
\item \href{#method-clone}{\code{HttpLibAdapaterRegistry$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\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{}}
\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{}}
\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 00000015347 13571515356 016113 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()
# 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()
# 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
x$to_s()
# to timeout
(x <- StubbedRequest$new(method = "get", uri_regex = ".+ossref.org"))
x$to_s()
x$to_timeout()
x$to_s()
# to raise
library(fauxpas)
(x <- StubbedRequest$new(method = "get", uri_regex = ".+ossref.org"))
x$to_s()
x$to_raise(HTTPBadGateway)
x$to_s()
}
}
\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{request_headers}}{(xx) xx}
\item{\code{response_headers}}{(xx) xx}
\item{\code{responses_sequences}}{(xx) xx}
\item{\code{status_code}}{(xx) xx}
\item{\code{timeout}}{(xx) xx}
\item{\code{exceptions}}{(xx) xx}
\item{\code{raise}}{(xx) xx}
}
\if{html}{\out{
}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-new}{\code{StubbedRequest$new()}}
\item \href{#method-print}{\code{StubbedRequest$print()}}
\item \href{#method-with}{\code{StubbedRequest$with()}}
\item \href{#method-to_return}{\code{StubbedRequest$to_return()}}
\item \href{#method-to_timeout}{\code{StubbedRequest$to_timeout()}}
\item \href{#method-to_raise}{\code{StubbedRequest$to_raise()}}
\item \href{#method-to_s}{\code{StubbedRequest$to_s()}}
\item \href{#method-clone}{\code{StubbedRequest$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\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}
\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{}}
\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{}}
\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)}\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.}
}
\if{html}{\out{
}}
}
\subsection{Returns}{
nothing returned; sets only
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\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{}}
\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{}}
\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{}}
\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{}}
\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 00000005217 13571506002 015707 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-new}{\code{MethodPattern$new()}}
\item \href{#method-matches}{\code{MethodPattern$matches()}}
\item \href{#method-to_s}{\code{MethodPattern$to_s()}}
\item \href{#method-clone}{\code{MethodPattern$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\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{}}
\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{}}
\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{}}
\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 00000007534 13571521416 016054 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-new}{\code{HeadersPattern$new()}}
\item \href{#method-matches}{\code{HeadersPattern$matches()}}
\item \href{#method-empty_headers}{\code{HeadersPattern$empty_headers()}}
\item \href{#method-to_s}{\code{HeadersPattern$to_s()}}
\item \href{#method-clone}{\code{HeadersPattern$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\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{}}
\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{}}
\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{}}
\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{}}
\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 00000007224 13571523506 016453 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}
}
\if{html}{\out{
}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-new}{\code{RequestSignature$new()}}
\item \href{#method-print}{\code{RequestSignature$print()}}
\item \href{#method-to_s}{\code{RequestSignature$to_s()}}
\item \href{#method-clone}{\code{RequestSignature$clone()}}
}
}
\if{html}{\out{
}}
\if{html}{\out{}}
\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{}}
\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{}}
\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{}}
\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 13571350775 017250 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 00000003106 13572030423 013570 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.5.0
Authors@R: c(
person("Scott", "Chamberlain", role = c("aut", "cre"), email =
"myrmecocystus+r@gmail.com", comment = c(ORCID="0000-0003-1444-9135")),
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)
BugReports: https://github.com/ropensci/webmockr/issues
LazyData: true
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)
Suggests: roxygen2 (>= 7.0.2), testthat, xml2, vcr, httr
RoxygenNote: 7.0.2
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: 2019-12-04 21:57:25 UTC; sckott
Author: Scott Chamberlain [aut, cre] (),
rOpenSci [fnd] (https://ropensci.org)
Maintainer: Scott Chamberlain
Repository: CRAN
Date/Publication: 2019-12-04 22:20:03 UTC
webmockr/tests/ 0000755 0001762 0000144 00000000000 13572025705 013233 5 ustar ligges users webmockr/tests/test-all.R 0000644 0001762 0000144 00000000053 13077016675 015107 0 ustar ligges users library("testthat")
test_check("webmockr")
webmockr/tests/testthat/ 0000755 0001762 0000144 00000000000 13572025705 015073 5 ustar ligges users webmockr/tests/testthat/test-StubbedRequest.R 0000644 0001762 0000144 00000012467 13571350262 021144 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")
# 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$body, "list")
expect_named(aa$responses_sequences$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...")
})
webmockr/tests/testthat/test-to_return.R 0000644 0001762 0000144 00000011053 13516121243 020204 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_named(aa$responses_sequences, c("status", "body", "headers", "body_raw"))
expect_equal(aa$responses_sequences$status, 200)
expect_equal(aa$responses_sequences$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")
})
webmockr/tests/testthat/httr_obj_auth.rda 0000644 0001762 0000144 00000000557 13570002016 020412 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 00000014015 13516121243 017516 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("util fxns: webmockr_configuration")
test_that("webmockr_configuration", {
expect_is(webmockr_configuration(), "webmockr_config")
expect_named(
webmockr_configuration(),
c('show_stubbing_instructions', 'show_body_diff', 'query_values_notation',
'allow', 'net_http_connect_on_start', '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 13457524755 017407 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 13457524755 020364 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 13570002016 022267 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-StubRegistry.R 0000644 0001762 0000144 00000006225 13571523714 020650 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/test-HashCounter.R 0000644 0001762 0000144 00000001571 13107060333 020410 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()
x$put("foo bar")
expect_length(x$hash, 1)
expect_equal(x$hash$`foo bar`, 1)
x$put("foo bar")
expect_length(x$hash, 1)
expect_equal(x$hash$`foo bar`, 2)
x$put("hello world")
expect_length(x$hash, 2)
expect_equal(x$hash$`hello world`, 1)
x$put("hello world")
x$put("hello world")
expect_length(x$hash, 2)
expect_equal(x$hash$`hello world`, 3)
})
test_that("HashCounter fails well", {
x <- HashCounter$new()
expect_error(x$get(), "'key' required")
expect_error(x$put(), "'key' required")
})
webmockr/tests/testthat/test-Response.R 0000644 0001762 0000144 00000006474 13415716025 020002 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")
aa$set_body(body = "stuff")
expect_equal(aa$body, "stuff")
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")
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 13571350262 021234 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-b-no-cassette-in-use.R 0000644 0001762 0000144 00000001066 13457524755 022053 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 00000002064 13457524755 020400 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_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")
# to_timeout expected stuff
expect_true(aa$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 13272757522 020310 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 13571350262 020723 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 00000003243 13457524755 023126 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")
library("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()
})
unloadNamespace("crul")
webmockr/tests/testthat/test-CrulAdapter.R 0000644 0001762 0000144 00000011014 13415716025 020374 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_crul_stubs, "function")
expect_is(aa$name, "character")
expect_equal(aa$name, "crul_adapter")
})
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")
})
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'
# )
# ))
})
webmockr/tests/testthat/test-to_return_body.R 0000644 0001762 0000144 00000003705 13242646670 021242 0 ustar ligges users context("to_return: response body types behave correctly for crul pkg")
test_that("to_return: setting body behaves correctly", {
webmockr::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-HttrAdapter.R 0000644 0001762 0000144 00000021640 13571565321 020422 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_httr_stubs, "function")
expect_is(aa$name, "character")
expect_equal(aa$name, "httr_adapter")
})
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")
})
# 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", 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()
})
webmockr/tests/testthat/test-to_raise.R 0000644 0001762 0000144 00000002701 13274216310 017771 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_null(aa$responses_sequences)
expect_false(aa$timeout)
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
expect_true(aa$raise)
expect_is(aa$exceptions, "list")
expect_is(aa$exceptions[[1]], "R6ClassGenerator")
expect_equal(aa$exceptions[[1]]$classname, "HTTPAccepted")
expect_equal(aa$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 13320534135 022127 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-request_registry.R 0000644 0001762 0000144 00000001507 13570002016 021602 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]], 'numeric')
})
webmockr/tests/testthat/crul_obj.rda 0000644 0001762 0000144 00000000366 13107754664 017376 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