if (FALSE) {
# 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"))
## with crul
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"))
## with httr
library(httr)
httr_mock()
POST("https://example.com", body = list(foo = "bar"))
PUT("https://google.com", 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))
# many responses
## the first response matches the first to_return call, and so on
stub_request("get", "https://httpbin.org/get") %>%
to_return(status = 200, body = "foobar", headers = list(a = 5)) %>%
to_return(status = 200, body = "bears", headers = list(b = 6))
con <- crul::HttpClient$new(url = "https://httpbin.org")
con$get("get")$parse("UTF-8")
con$get("get")$parse("UTF-8")
## OR, use times with to_return() to repeat the same response many times
library(fauxpas)
stub_request("get", "https://httpbin.org/get") %>%
to_return(status = 200, body = "apple-pie", times = 2) %>%
to_raise(HTTPUnauthorized)
con <- crul::HttpClient$new(url = "https://httpbin.org")
con$get("get")$parse("UTF-8")
con$get("get")$parse("UTF-8")
con$get("get")$parse("UTF-8")
# partial matching
## query parameters
library(httr)
enable()
### matches
stub_request("get", "https://hb.opencpu.org/get") %>%
wi_th(query = including(list(fruit = "pear"))) %>%
to_return(body = "matched on partial query!")
resp <- GET("https://hb.opencpu.org/get",
query = list(fruit = "pear", bread = "scone")
)
rawToChar(content(resp))
### doesn't match
stub_registry_clear()
stub_request("get", "https://hb.opencpu.org/get") %>%
wi_th(query = list(fruit = "pear")) %>%
to_return(body = "didn't match, ugh!")
# GET("https://hb.opencpu.org/get",
# query = list(fruit = "pear", meat = "chicken"))
## request body
### matches - including
stub_request("post", "https://hb.opencpu.org/post") %>%
wi_th(body = including(list(fruit = "pear"))) %>%
to_return(body = "matched on partial body!")
resp <- POST("https://hb.opencpu.org/post",
body = list(fruit = "pear", meat = "chicken")
)
rawToChar(content(resp))
### matches - excluding
stub_request("post", "https://hb.opencpu.org/post") %>%
wi_th(body = excluding(list(fruit = "pear"))) %>%
to_return(body = "matched on partial body!")
res <- POST("https://hb.opencpu.org/post",
body = list(color = "blue")
)
rawToChar(content(res))
# POST("https://hb.opencpu.org/post",
# body = list(fruit = "pear", meat = "chicken"))
# clear all stubs
stub_registry()
stub_registry_clear()
}
Run the code above in your browser using DataLab