webmockr

Build Status codecov

R library for stubbing and setting expectations on HTTP requests.

Port of the Ruby gem webmock

Features

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

ropensci_footer