-
Notifications
You must be signed in to change notification settings - Fork 17
/
08-vcr-usage.Rmd
94 lines (78 loc) · 2.39 KB
/
08-vcr-usage.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
```{r echo = FALSE}
knitr::opts_chunk$set(
comment = "#>",
warning = FALSE,
message = FALSE
)
```
# Advanced vcr usage {#vcr-usage}
Now that we've covered basic `vcr` usage, it's time for some more advanced usage topics.
```{r}
library("vcr")
```
## Mocking writing to disk {#vcr-disk}
If you have http requests for which you write the response to disk, then
use `vcr_configure()` to set the `write_disk_path` option. See more about
the [write_disk_path configuration option](#write-disk-path).
Here, we create a temporary directory, then set the fixtures
```{r}
tmpdir <- tempdir()
vcr_configure(
dir = file.path(tmpdir, "fixtures"),
write_disk_path = file.path(tmpdir, "files")
)
```
Then pass a file path (that doesn't exist yet) to crul's `disk` parameter.
`vcr` will take care of handling writing the response to that file in
addition to the cassette.
```{r}
library(crul)
## make a temp file
f <- tempfile(fileext = ".json")
## make a request
cas <- use_cassette("test_write_to_disk", {
out <- HttpClient$new("https://httpbin.org/get")$get(disk = f)
})
file.exists(out$content)
out$parse()
```
This also works with `httr`. The only difference is that you write to disk
with a function `httr::write_disk(path)` rather than a parameter.
::: {.alert .alert-dismissible .alert-info}
Writing to disk with `{httr2}` does not yet work with `{vcr}` -- see <https://github.com/ropensci/vcr/issues/270>
:::
Note that when you write to disk when using `vcr`, the cassette is slightly
changed. Instead of holding the http response body itself, the cassette
has the file path with the response body.
```yaml
http_interactions:
- request:
method: get
uri: https://httpbin.org/get
response:
headers:
status: HTTP/1.1 200 OK
access-control-allow-credentials: 'true'
body:
encoding: UTF-8
file: yes
string: /private/var/folders/fc/n7g_vrvn0sx_st0p8lxb3ts40000gn/T/Rtmp5W4olr/files/file177e2e5d97ec.json
```
And the file has the response body that otherwise would have been in the `string`
yaml field above:
```json
{
"args": {},
"headers": {
"Accept": "application/json, text/xml, application/xml, */*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "libcurl/7.54.0 r-curl/4.3 crul/0.9.0"
},
"origin": "24.21.229.59, 24.21.229.59",
"url": "https://httpbin.org/get"
}
```
```{r echo=FALSE}
invisible(vcr_configure_reset())
```