Skip to content

Commit 54e4d40

Browse files
authored
Mocking change (#150)
* #149 crul mocking changes, not done yet * tickle checks * strip out vcr components everywhere
1 parent e7294d5 commit 54e4d40

34 files changed

+173
-599
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ Imports:
3636
Suggests:
3737
testthat,
3838
xml2,
39-
vcr,
4039
crul,
4140
httr,
4241
httr2,
4342
diffobj,
4443
withr
44+
Remotes: ropensci/crul@change-mocking
4545
RoxygenNote: 7.3.2
4646
Config/testthat/edition: 3
4747
X-schema.org-applicationCategory: Web

Makefile

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,6 @@ FILE_TARGET := "R/${FILE}.R"
66

77
all: move rmd2md
88

9-
move:
10-
cp inst/vign/vcr.md vignettes;\
11-
cp inst/vign/configuration.md vignettes
12-
13-
rmd2md:
14-
cd vignettes;\
15-
mv vcr.md vcr.Rmd;\
16-
mv configuration.md configuration.Rmd
17-
189
install: doc build
1910
R CMD INSTALL . && rm *.tar.gz
2011

R/adapter-crul.R

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ build_crul_request <- function(x) {
7575
)
7676
}
7777

78+
crul_mock <- function(on = TRUE) {
79+
check_installed("crul")
80+
if (on) {
81+
options(crul_mock = function(req) {
82+
webmockr::CrulAdapter$new()$handle_request(req)
83+
})
84+
} else {
85+
options(crul_mock = NULL)
86+
}
87+
invisible(on)
88+
}
89+
7890
#' @rdname Adapter
7991
#' @export
8092
#' @keywords internal
@@ -89,29 +101,12 @@ CrulAdapter <- R6::R6Class(
89101
),
90102
private = list(
91103
pluck_url = function(request) request$url$url,
92-
mock = function(on) crul::mock(on),
104+
# mock = function(on) crul::mock(on),
105+
mock = function(on) crul_mock(on),
93106
build_request = build_crul_request,
94107
build_response = build_crul_response,
95108
fetch_request = function(request) {
96109
private$build_response(request, webmockr_crul_fetch(request))
97-
},
98-
request_handler = function(request) vcr::RequestHandlerCrul$new(request),
99-
update_vcr_disk_path = function(response) {
100-
write_disk_path <- vcr::vcr_configuration()$write_disk_path
101-
102-
# if crul_resp$content is character, it must be a file path (I THINK?)
103-
if (is.null(write_disk_path)) {
104-
abort(c(
105-
"if writing to disk, write_disk_path must be given",
106-
"see ?vcr::vcr_configure"
107-
))
108-
}
109-
110-
response$content <- file.path(
111-
write_disk_path,
112-
basename(response$content)
113-
)
114-
response
115110
}
116111
)
117112
)

R/adapter-httr.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ HttrAdapter <- R6::R6Class(
135135
mock = function(on) httr_mock(on),
136136
build_request = build_httr_request,
137137
build_response = build_httr_response,
138-
request_handler = function(request) vcr::RequestHandlerHttr$new(request),
139138
fetch_request = function(request) {
140139
METHOD <- eval(parse(text = paste0("httr::", request$method)))
141140
METHOD(

R/adapter-httr2.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ Httr2Adapter <- R6::R6Class(
111111
mock = function(on) httr2_mock(on),
112112
build_request = build_httr2_request,
113113
build_response = build_httr2_response,
114-
request_handler = function(request) vcr::RequestHandlerHttr2$new(request),
115114
fetch_request = function(request) httr2::req_perform(request)
116115
)
117116
)

R/adapter.R

Lines changed: 8 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Adapter <- R6::R6Class(
4242

4343
switch(
4444
self$client,
45-
crul = crul::mock(on = TRUE),
45+
crul = crul_mock(on = TRUE),
4646
httr = httr_mock(on = TRUE),
4747
httr2 = httr2_mock(on = TRUE)
4848
)
@@ -61,7 +61,7 @@ Adapter <- R6::R6Class(
6161

6262
switch(
6363
self$client,
64-
crul = crul::mock(on = FALSE),
64+
crul = crul_mock(on = FALSE),
6565
httr = httr_mock(on = FALSE),
6666
httr2 = httr2_mock(on = FALSE)
6767
)
@@ -88,87 +88,20 @@ Adapter <- R6::R6Class(
8888
# get stub with response and return that
8989
resp <- private$build_stub_response(ss)
9090

91-
# generate response / vcr: recordable/ignored
92-
if (vcr_cassette_inserted()) {
93-
# use RequestHandler - gets current cassette & record interaction
94-
resp <- private$request_handler(req)$handle()
95-
96-
# if written to disk, see if we should modify file path
97-
if (self$client == "crul" && is.character(resp$content)) {
98-
resp <- private$update_vcr_disk_path(resp)
99-
}
100-
101-
# no vcr
102-
} else {
103-
resp <- private$build_response(req, resp)
104-
# add to_return() elements if given
105-
resp <- private$add_response_sequences(ss, resp)
106-
}
91+
resp <- private$build_response(req, resp)
92+
# add to_return() elements if given
93+
resp <- private$add_response_sequences(ss, resp)
10794

10895
# request is not in cache but connections are allowed
10996
} else if (webmockr_net_connect_allowed(uri = private$pluck_url(req))) {
11097
# if real requests || localhost || certain exceptions ARE
11198
# allowed && nothing found above
112-
113-
# if vcr loaded: record http interaction into vcr namespace
114-
# VCR: recordable
115-
if (vcr_loaded()) {
116-
# FIXME: maybe use RequestHandler instead?
117-
# which gets current cassette for us
118-
resp <- private$request_handler(req)$handle()
119-
120-
# if written to disk, see if we should modify file path
121-
if (self$client == "crul" && is.character(resp$content)) {
122-
if (file.exists(resp$content)) {
123-
resp <- private$update_vcr_disk_path(resp)
124-
}
125-
}
126-
127-
if (self$client == "httr2") {
128-
req$method <- req_method_get_w(req)
129-
}
130-
131-
# stub request so next time we match it
132-
req_url <- private$pluck_url(req)
133-
urip <- curl::curl_parse_url(req_url)
134-
urip$params <- as.list(urip$params)
135-
m <- vcr::vcr_configuration()$match_requests_on
136-
137-
if (all(m %in% c("method", "uri")) && length(m) == 2) {
138-
stub_request(req$method, req_url)
139-
} else if (
140-
all(m %in% c("method", "uri", "query")) && length(m) == 3
141-
) {
142-
tmp <- stub_request(req$method, req_url)
143-
wi_th(tmp, .list = list(query = urip$params))
144-
} else if (
145-
all(m %in% c("method", "uri", "headers")) && length(m) == 3
146-
) {
147-
tmp <- stub_request(req$method, req_url)
148-
wi_th(tmp, .list = list(headers = req$headers))
149-
} else if (
150-
all(m %in% c("method", "uri", "headers", "query")) && length(m) == 4
151-
) {
152-
tmp <- stub_request(req$method, req_url)
153-
wi_th(
154-
tmp,
155-
.list = list(query = urip$params, headers = req$headers)
156-
)
157-
}
158-
} else {
159-
private$mock(on = FALSE)
160-
resp <- private$fetch_request(req)
161-
private$mock(on = TRUE)
162-
}
99+
private$mock(on = FALSE)
100+
resp <- private$fetch_request(req)
101+
private$mock(on = TRUE)
163102

164103
# request is not in cache and connections are not allowed
165104
} else {
166-
# throw vcr error: should happen when user not using
167-
# use_cassette or insert_cassette
168-
if (vcr_loaded()) {
169-
private$request_handler(req)$handle()
170-
}
171-
172105
# no stubs found and net connect not allowed - STOP
173106
x <- c(
174107
"Real HTTP connections are disabled.",

R/globals.R

Lines changed: 0 additions & 3 deletions
This file was deleted.

R/webmockr-package.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#' - Matching requests based on method, URI, headers and body
66
#' - Supports multiple HTTP libraries, including \pkg{crul},
77
#' \pkg{httr}, and \pkg{httr2}
8-
#' - Integration with HTTP test caching library \pkg{vcr}
98
#' - Supports async http request mocking with \pkg{crul} only
109
#'
1110
#' @examples

R/zzz.R

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -272,16 +272,3 @@ last <- function(x) {
272272
}
273273
x[[length(x)]]
274274
}
275-
276-
277-
vcr_loaded <- function() {
278-
isNamespaceLoaded("vcr")
279-
}
280-
281-
# check whether a cassette is inserted without assuming vcr is installed
282-
vcr_cassette_inserted <- function() {
283-
if (vcr_loaded()) {
284-
return(length(vcr::current_cassette()) > 0)
285-
}
286-
return(FALSE)
287-
}

README.Rmd

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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
8079
stub_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
8483
stub_registry()
@@ -119,8 +118,9 @@ set return objects
119118
```{r}
120119
stub_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}
133133
stub_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}
194198
stub_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

201211
now 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}
240250
stub_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

244258
now returns mocked response
@@ -271,8 +285,10 @@ f <- tempfile(fileext = ".json")
271285
cat("{\"hello\":\"world\"}\n", file = f)
272286
readLines(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
277293
out <- HttpClient$new("https://httpbin.org/get")$get(disk = f)
278294
readLines(file(f))
@@ -283,8 +299,10 @@ OR - you can use `mock_file()` to have `webmockr` handle file and contents
283299
```{r}
284300
g <- 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
289307
out <- crul::HttpClient$new("https://httpbin.org/get")$get(disk = g)
290308
readLines(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

Comments
 (0)