-
Notifications
You must be signed in to change notification settings - Fork 17
/
04-webmockr-stubs.Rmd
137 lines (112 loc) · 3.63 KB
/
04-webmockr-stubs.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
```{r echo = FALSE}
knitr::opts_chunk$set(
comment = "#>",
warning = FALSE,
message = FALSE
)
```
# stubs {#webmockr-stubs}
```{r}
library("webmockr")
```
set return objects
```{r}
stub_request("get", "https://httpbin.org/get") %>%
wi_th(
query = list(hello = "world")) %>%
to_return(status = 418)
```
```{r}
x$get('get', query = list(hello = "world"))
```
**Stubbing requests based on method, uri and query params**
```{r}
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"))
```
```{r}
stub_registry()
```
```{r}
x <- HttpClient$new(url = "https://httpbin.org")
x$get('get', query = list(hello = "world"))
```
**Stubbing requests and set expectation of a timeout**
```{r eval=FALSE}
stub_request("post", "https://httpbin.org/post") %>% to_timeout()
x <- HttpClient$new(url = "https://httpbin.org")
x$post('post')
#> 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.
```
**Stubbing requests and set HTTP error expectation**
```{r eval=FALSE}
library(fauxpas)
stub_request("get", "https://httpbin.org/get?a=b") %>% to_raise(HTTPBadRequest)
x <- HttpClient$new(url = "https://httpbin.org")
x$get('get', query = list(a = "b"))
#> 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.
```
## Writing to disk {#webmockr-disk}
There are two ways to deal with mocking writing to disk. First, you can create a file
with the data you'd like in that file, then tell crul, httr, or httr2 where that file is.
Second, you can simply give webmockr a file path (that doesn't exist yet) and some
data, and webmockr can take care of putting the data in the file.
```{r echo=FALSE}
stub_registry_clear()
request_registry_clear()
```
Here's the first method, where you put data in a file as your mock, then pass the
file as a connection (with `file(<file path>)`) to `to_return()`.
```{r}
## make a temp file
f <- tempfile(fileext = ".json")
## write something to the file
cat("{\"hello\":\"world\"}\n", file = f)
## 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)
## view stubbed file content
readLines(file(f))
```
With the second method, use `webmockr::mock_file()` to have `webmockr` handle file
and contents.
```{r}
g <- tempfile(fileext = ".json")
## make the stub
invisible(stub_request("get", "https://httpbin.org/get?a=b") %>%
to_return(body = mock_file(path = g, payload = "{\"hello\":\"mars\"}\n")))
## make a request
out <- crul::HttpClient$new("https://httpbin.org/get?a=b")$get(disk = g)
## view stubbed file content
readLines(out$content)
```
`webmockr` also supports `httr::write_disk()`, here letting `webmockr` handle the
mock file creation:
```{r}
library(httr)
httr_mock()
## make a temp file
f <- tempfile(fileext = ".json")
## make the stub
invisible(stub_request("get", "https://httpbin.org/get?cheese=swiss") %>%
to_return(
body = mock_file(path = f, payload = "{\"foo\": \"bar\"}"),
headers = list('content-type' = "application/json")
))
## make a request
out <- GET("https://httpbin.org/get?cheese=swiss", write_disk(f, TRUE))
## view stubbed file content
readLines(out$content)
```
```{r cleanup, echo=FALSE}
unlink(c(f, g))
httr_mock(FALSE)
```