Learn R Programming

⚠️There's a newer version (2.0.0) of this package.Take me there.

webmockr

R library for stubbing and setting expectations on HTTP requests.

Port of the Ruby gem webmock

Features

  • Stubbing HTTP requests at low http client lib level
  • Setting and verifying expectations on HTTP requests
  • Matching requests based on method, URI, headers and body
  • Support for testthat coming soon via vcr

Supported HTTP libraries

more to come

Install

from cran

install.packages("webmockr")

Dev version

devtools::install_github("ropensci/webmockr")
library(webmockr)

Turn on webmockr

webmockr::enable()
#> CrulAdapter enabled!
#> [1] TRUE
crul::mock()

Outside a test framework

library(crul)

Stubbed request based on uri only and with the default response

stub_request("any", "https://httpbin.org/get")
#> <webmockr stub> 
#>   method: any
#>   uri: https://httpbin.org/get
#>   with: 
#>     query: 
#>     body: 
#>     request_headers: 
#>   to_return: 
#>     status: 
#>     body: 
#>     response_headers:
x <- HttpClient$new(url = "https://httpbin.org")
x$get('get')
#> $url
#> $url$url
#> [1] "https://httpbin.org/get"
#> 
#> $url$handle
#> <curl handle> (empty)
#> 
#> 
#> $method
#> [1] "get"
#> 
#> $options
#> $options$httpget
#> [1] TRUE
#> 
#> 
#> $headers
#> $headers$`User-Agent`
#> [1] "libcurl/7.51.0 r-curl/2.6 crul/0.3.5.9313"
#> 
#> $headers$`Accept-Encoding`
#> [1] "gzip, deflate"

set return objects

stub_request("get", "https://httpbin.org/get") %>%
  wi_th(
    query = list(hello = "world")) %>%
    to_return(status = 418)
#> <webmockr stub> 
#>   method: get
#>   uri: https://httpbin.org/get
#>   with: 
#>     query: hello=world
#>     body: 
#>     request_headers: 
#>   to_return: 
#>     status: 418
#>     body: 
#>     response_headers:
x$get('get', query = list(hello = "world"))
#> $url
#> $url$url
#> [1] "https://httpbin.org/get?hello=world"
#> 
#> $url$handle
#> <curl handle> (empty)
#> 
#> 
#> $method
#> [1] "get"
#> 
#> $options
#> $options$httpget
#> [1] TRUE
#> 
#> 
#> $headers
#> $headers$`User-Agent`
#> [1] "libcurl/7.51.0 r-curl/2.6 crul/0.3.5.9313"
#> 
#> $headers$`Accept-Encoding`
#> [1] "gzip, deflate"

Stubbing requests based on method, uri and query params

stub_request("get", "https://httpbin.org/get") %>%
  wi_th(query = list(hello = "world"), 
        headers = list('User-Agent' = 'libcurl/7.51.0 r-curl/2.6 crul/0.3.6', 
                       'Accept-Encoding' = "gzip, deflate"))
#> <webmockr stub> 
#>   method: get
#>   uri: https://httpbin.org/get
#>   with: 
#>     query: hello=world
#>     body: 
#>     request_headers: User-Agent=libcurl/7.51.0 r-curl/2.6 crul/0.3.6, Accept-Encoding=gzip, deflate
#>   to_return: 
#>     status: 
#>     body: 
#>     response_headers:
stub_registry()
#> <webmockr stub registry> 
#>  Registered Stubs
#>    any: https://httpbin.org/get 
#>    get: https://httpbin.org/get?hello=world   | to_return:   with status 418 
#>    get: https://httpbin.org/get?hello=world   with headers {"User-Agent":"libcurl/7.51.0 r-curl/2.6 crul/0.3.6","Accept-Encoding":"gzip, deflate"}
x <- HttpClient$new(url = "https://httpbin.org")
x$get('get', query = list(hello = "world"))
#> $url
#> $url$url
#> [1] "https://httpbin.org/get?hello=world"
#> 
#> $url$handle
#> <curl handle> (empty)
#> 
#> 
#> $method
#> [1] "get"
#> 
#> $options
#> $options$httpget
#> [1] TRUE
#> 
#> 
#> $headers
#> $headers$`User-Agent`
#> [1] "libcurl/7.51.0 r-curl/2.6 crul/0.3.5.9313"
#> 
#> $headers$`Accept-Encoding`
#> [1] "gzip, deflate"

Meta

By participating in this project you agree to abide by its terms.

Copy Link

Version

Install

install.packages('webmockr')

Monthly Downloads

5,477

Version

0.1.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Scott Chamberlain

Last Published

May 20th, 2017

Functions in webmockr (0.1.0)

RequestPattern

RequestPattern class
RequestRegistry

Request registry
StubRegistry

Stub registry
StubbedRequest

StubbedRequest class
UriPattern

UriPattern
enable

Enable or disable webmockr
webmockr-package

Stubbing and setting expectations on HTTP requests
webmockr_configure

webmockr configuration
RequestSignature

General purpose request signature builder
Response

Response class
BodyPattern

BodyPattern
CrulAdapter

crul library adapter
%>%

Pipe operator
remove_request_stub

Remove a request stub
wi_th

Set additional parts of a stubbed request
HttpLibAdapaterRegistry

http lib adapter registry
MethodPattern

MethodPattern
stub_registry

List stubs in the stub registry
HashCounter

hash with counter, to store requests, and count each time it is used
HeadersPattern

HeadersPattern
stub_request

Stub an http request
to_return

Expectation for what's returned from a stubbed request
stub_registry_clear

Clear the stub registry