Skip to contents

Stubbing and setting expectations on HTTP requests

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

  • Supports multiple HTTP libraries, including crul and httr

  • Integration with HTTP test caching library vcr

Author

Scott Chamberlain myrmecocystus+r@gmail.com

Aaron Wolen

Examples

library(webmockr)
stub_request("get", "https://httpbin.org/get")
#> <webmockr stub> 
#>   method: get
#>   uri: https://httpbin.org/get
#>   with: 
#>     query: 
#>     body: 
#>     request_headers: 
#>   to_return: 
stub_request("post", "https://httpbin.org/post")
#> <webmockr stub> 
#>   method: post
#>   uri: https://httpbin.org/post
#>   with: 
#>     query: 
#>     body: 
#>     request_headers: 
#>   to_return: 
stub_registry()
#> <webmockr stub registry> 
#>  Registered Stubs
#>    POST: https://httpbin.org/post   | to_return:    with status 200 
#>    POST: https://httpbin.org/post   | to_return:   with body "stuff" 
#>    POST: https://httpbin.org/post   | to_return:   with body {"a":{"b":"world"}} 
#>    POST: https://httpbin.org/post   | to_return:     with headers {"a":5} 
#>    POST: https://httpbin.org/post   | to_return:   with body "stuff"  with status 200  with headers {"a":5} 
#>    POST: https://httpbin.org/post   | to_return:   with body {"foo":"bar"} 
#>    POST: https://httpbin.org/post   | to_return:   with body "stuff"     | to_return:   with body "things" 
#>    POST: https://httpbin.org/post   | to_return:   with body "stuff"     | to_return:   with body "stuff"     | to_return:   with body "stuff" 
#>    GET: https://httpbin.org/get 
#>    POST: https://httpbin.org/post