@@ -35,7 +35,6 @@ Port of the Ruby gem [webmock](https://github.com/bblimke/webmock)
3535* Stubbing HTTP requests at low http client lib level
3636* Setting and verifying expectations on HTTP requests
3737* Matching requests based on method, URI, headers and body
38- * Support for ` testthat ` via [ vcr] [ ]
3938* Can be used for testing or outside of a testing context
4039* Supports async http request mocking with ` crul ` only
4140
@@ -78,7 +77,7 @@ library(testthat)
7877
7978# make a stub
8079stub_request("get", "https://httpbin.org/get") %>%
81- to_return(body = "success!", status = 200)
80+ to_return(body = "success!", status = 200)
8281
8382# check that it's in the stub registry
8483stub_registry()
@@ -119,8 +118,9 @@ set return objects
119118``` {r}
120119stub_request("get", "https://httpbin.org/get") %>%
121120 wi_th(
122- query = list(hello = "world")) %>%
123- to_return(status = 418)
121+ query = list(hello = "world")
122+ ) %>%
123+ to_return(status = 418)
124124```
125125
126126``` {r}
@@ -131,9 +131,13 @@ x$get('get', query = list(hello = "world"))
131131
132132``` {r}
133133stub_request("get", "https://httpbin.org/get") %>%
134- wi_th(query = list(hello = "world"),
135- headers = list('User-Agent' = 'libcurl/7.51.0 r-curl/2.6 crul/0.3.6',
136- 'Accept-Encoding' = "gzip, deflate"))
134+ wi_th(
135+ query = list(hello = "world"),
136+ headers = list(
137+ 'User-Agent' = 'libcurl/7.51.0 r-curl/2.6 crul/0.3.6',
138+ 'Accept-Encoding' = "gzip, deflate"
139+ )
140+ )
137141```
138142
139143``` {r}
@@ -178,9 +182,9 @@ GET("https://httpbin.org/get")
178182#> Error: Real HTTP connections are disabled.
179183#> Unregistered request:
180184#> GET https://httpbin.org/get with headers {Accept: application/json, text/xml, application/xml, */*}
181- #>
185+ #>
182186#> You can stub this request with the following snippet:
183- #>
187+ #>
184188#> stub_request('get', uri = 'https://httpbin.org/get') %>%
185189#> wi_th(
186190#> headers = list('Accept' = 'application/json, text/xml, application/xml, */*')
@@ -193,9 +197,15 @@ make a stub
193197``` {r}
194198stub_request('get', uri = 'https://httpbin.org/get') %>%
195199 wi_th(
196- headers = list('Accept' = 'application/json, text/xml, application/xml, */*')
200+ headers = list(
201+ 'Accept' = 'application/json, text/xml, application/xml, */*'
202+ )
197203 ) %>%
198- to_return(status = 418, body = "I'm a teapot!!!", headers = list(im_a = "teapot"))
204+ to_return(
205+ status = 418,
206+ body = "I'm a teapot!!!",
207+ headers = list(im_a = "teapot")
208+ )
199209```
200210
201211now returns mocked response
@@ -227,9 +237,9 @@ req_perform(req)
227237#> Error: Real HTTP connections are disabled.
228238#> Unregistered request:
229239#> GET https://hb.opencpu.org/get
230- #>
240+ #>
231241#> You can stub this request with the following snippet:
232- #>
242+ #>
233243#> stub_request('get', uri = 'https://hb.opencpu.org/get')
234244#> ============================================================
235245```
@@ -238,7 +248,11 @@ make a stub
238248
239249``` {r}
240250stub_request('get', uri = 'https://hb.opencpu.org/get') %>%
241- to_return(status = 418, body = "I'm a teapot!!!", headers = list(im_a = "teapot"))
251+ to_return(
252+ status = 418,
253+ body = "I'm a teapot!!!",
254+ headers = list(im_a = "teapot")
255+ )
242256```
243257
244258now returns mocked response
@@ -271,8 +285,10 @@ f <- tempfile(fileext = ".json")
271285cat("{\"hello\":\"world\"}\n", file = f)
272286readLines(f)
273287## make the stub
274- invisible(stub_request("get", "https://httpbin.org/get") %>%
275- to_return(body = file(f)))
288+ invisible(
289+ stub_request("get", "https://httpbin.org/get") %>%
290+ to_return(body = file(f))
291+ )
276292## make a request
277293out <- HttpClient$new("https://httpbin.org/get")$get(disk = f)
278294readLines(file(f))
@@ -283,8 +299,10 @@ OR - you can use `mock_file()` to have `webmockr` handle file and contents
283299``` {r}
284300g <- tempfile(fileext = ".json")
285301## make the stub
286- invisible(stub_request("get", "https://httpbin.org/get") %>%
287- to_return(body = mock_file(g, "{\"hello\":\"mars\"}\n")))
302+ invisible(
303+ stub_request("get", "https://httpbin.org/get") %>%
304+ to_return(body = mock_file(g, "{\"hello\":\"mars\"}\n"))
305+ )
288306## make a request
289307out <- crul::HttpClient$new("https://httpbin.org/get")$get(disk = g)
290308readLines(out$content)
@@ -336,5 +354,3 @@ simply gives the last response you specified. Although if you set a `to_timeout`
336354* License: MIT
337355* Get citation information for ` webmockr ` in R doing ` citation(package = 'webmockr') `
338356* Please note that this package is released with a [ Contributor Code of Conduct] ( https://ropensci.org/code-of-conduct/ ) . By contributing to this project, you agree to abide by its terms.
339-
340- [ vcr ] : https://github.com/ropensci/vcr
0 commit comments