Load webmockr
Enable webmockr
## CrulAdapter enabled!
## HttrAdapter enabled!
## Httr2Adapter enabled!
library(crul)
library(testthat)
# make a stub
stub_request("get", "https://httpbin.org/get") %>%
to_return(body = "success!", status = 200)## <webmockr stub>
## method: get
## uri: https://httpbin.org/get
## with:
## query:
## body:
## request_headers:
## auth:
## to_return:
## - status: 200
## body: success!
## response_headers:
## should_timeout: FALSE
## should_raise: FALSE
## <webmockr stub registry>
## Registered Stubs
## GET: https://httpbin.org/get | to_return: with body "success!" with status 200
# make the request
z <- crul::HttpClient$new(url = "https://httpbin.org")$get("get")
# run tests (nothing returned means it passed)
expect_is(z, "HttpResponse")## Warning: `expect_is()` was deprecated in the 3rd edition.
## ℹ Use `expect_type()`, `expect_s3_class()`, or `expect_s4_class()` instead
## <webmockr stub>
## method: get
## uri: https://httpbin.org/get
## with:
## query:
## body:
## request_headers:
## auth:
## to_return:
## <crul response>
## url: https://httpbin.org/get
## request_headers:
## User-Agent: libcurl/8.18.0 r-curl/7.1.0 crul/1.6.0.9000
## Accept-Encoding: gzip, deflate
## Accept: application/json, text/xml, application/xml, */*
## response_headers:
## status: 200
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:
## auth:
## to_return:
## - status: 418
## body:
## response_headers:
## should_timeout: FALSE
## should_raise: FALSE
## <crul response>
## url: https://httpbin.org/get
## request_headers:
## User-Agent: libcurl/8.18.0 r-curl/7.1.0 crul/1.6.0.9000
## Accept-Encoding: gzip, deflate
## Accept: application/json, text/xml, application/xml, */*
## response_headers:
## status: 418
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-cur..., Accept-Encoding=gzip, deflate
## auth:
## to_return:
## <webmockr stub registry>
## Registered Stubs
## GET: https://httpbin.org/get
## GET: https://httpbin.org/get with query params hello=world | to_return: with status 418
## GET: https://httpbin.org/get with query params hello=world with headers {"User-Agent":"libcurl/7.51.0 r-curl/2.6 crul/0.3.6","Accept-Encoding":"gzip, deflate"}
## <crul response>
## url: https://httpbin.org/get
## request_headers:
## User-Agent: libcurl/8.18.0 r-curl/7.1.0 crul/1.6.0.9000
## Accept-Encoding: gzip, deflate
## Accept: application/json, text/xml, application/xml, */*
## response_headers:
## status: 418
## <webmockr stub>
## method: post
## uri: https://httpbin.org/post
## with:
## query:
## body:
## request_headers:
## auth:
## to_return:
## - status:
## body:
## response_headers:
## should_timeout: TRUE
## should_raise: FALSE
## Error:
## ! Request Timeout (HTTP 408).
## - The client did not produce a request within the time that the server was prepared to wait. The client MAY repeat the request without modifications at any later time.
## <webmockr stub>
## method: get
## uri: https://httpbin.org/get?a=b
## with:
## query:
## body:
## request_headers:
## auth:
## to_return:
## - status:
## body:
## response_headers:
## should_timeout: FALSE
## should_raise: HTTPBadRequest
## Error:
## ! Bad Request (HTTP 400).
## - The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.
##
## Attaching package: 'httr'
## The following object is masked from 'package:crul':
##
## handle
# no stub found
GET("https://httpbin.org/get")
#> Error: Real HTTP connections are disabled.
#> Unregistered request:
#> GET https://httpbin.org/get with headers {Accept: application/json, text/xml, application/xml, */*}
#>
#> You can stub this request with the following snippet:
#>
#> stub_request('get', uri = 'https://httpbin.org/get') %>%
#> wi_th(
#> headers = list('Accept' = 'application/json, text/xml, application/xml, */*')
#> )
#> ============================================================make a stub
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(im_a = "teapot")
)## <webmockr stub>
## method: get
## uri: https://httpbin.org/get
## with:
## query:
## body:
## request_headers: Accept=application/json, te...
## auth:
## to_return:
## - status: 418
## body: I'm a teapot!!!
## response_headers: im_a=teapot
## should_timeout: FALSE
## should_raise: FALSE
now returns mocked response
# no stub found
req <- request("https://hb.opencpu.org/get")
req_perform(req)
#> Error: Real HTTP connections are disabled.
#> Unregistered request:
#> GET https://hb.opencpu.org/get
#>
#> You can stub this request with the following snippet:
#>
#> stub_request('get', uri = 'https://hb.opencpu.org/get')
#> ============================================================make a stub
stub_request('get', uri = 'https://hb.opencpu.org/get') %>%
to_return(
status = 418,
body = "I'm a teapot!!!",
headers = list(im_a = "teapot")
)## <webmockr stub>
## method: get
## uri: https://hb.opencpu.org/get
## with:
## query:
## body:
## request_headers:
## auth:
## to_return:
## - status: 418
## body: I'm a teapot!!!
## response_headers: im_a=teapot
## should_timeout: FALSE
## should_raise: FALSE
now returns mocked response
Write to a file before mocked request
## 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
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 crul, httr,
and httr2
e.g., many redirects, then a final successful request
webmockr::enable()
library(crul)
library(fauxpas)
z <- stub_request("get", "https://httpbin.org/get")
to_return(z, status = 200, body = "foobar", headers = list(a = 5))
to_return(z, status = 200, body = "bears", headers = list(b = 6))
to_raise(z, HTTPBadRequest)
z
con <- crul::HttpClient$new(url = "https://httpbin.org")
# the first to_return()
first <- con$get("get")
first
first$parse("UTF-8")
# the second to_return()
second <- con$get("get")
second
second$parse("UTF-8")
# the third to_return() - fails as specified
third <- con$get("get")Note that subsequent requests past the number of responses given with
to_return()/etc. simply gives the last response you
specified. Although if you set a to_timeout or
to_raise this feature won’t happen since you fail out.