|
78 | 78 | #' # list stubs |
79 | 79 | #' stub_registry() |
80 | 80 | #' |
| 81 | +#' # request headers |
| 82 | +#' stub_request("get", "https://httpbin.org/get") %>% |
| 83 | +#' wi_th(headers = list("User-Agent" = "R")) |
| 84 | +#' \dontshow{stub_registry_clear()} |
| 85 | +#' |
| 86 | +#' # request body |
| 87 | +#' stub_request("post", "https://httpbin.org/post") %>% |
| 88 | +#' wi_th(body = list(foo = "bar")) |
| 89 | +#' stub_registry() |
| 90 | +#' library(crul) |
| 91 | +#' x <- crul::HttpClient$new(url = "https://httpbin.org") |
| 92 | +#' enable(adapter = "crul") |
| 93 | +#' x$post("post", body = list(foo = "bar")) |
| 94 | +#' |
| 95 | +#' # add expectation with to_return |
| 96 | +#' stub_request("get", "https://httpbin.org/get") %>% |
| 97 | +#' wi_th( |
| 98 | +#' query = list(hello = "world"), |
| 99 | +#' headers = list("User-Agent" = "R") |
| 100 | +#' ) %>% |
| 101 | +#' to_return(status = 200, body = "stuff", headers = list(a = 5)) |
| 102 | +#' |
| 103 | +#' # list stubs again |
| 104 | +#' stub_registry() |
| 105 | +#' |
| 106 | +#' # regex |
| 107 | +#' stub_request("get", uri_regex = ".+ample\\..") |
| 108 | +#' |
| 109 | +#' # set stub an expectation to timeout |
| 110 | +#' stub_request("get", "https://httpbin.org/get") %>% to_timeout() |
| 111 | +#' x <- crul::HttpClient$new(url = "https://httpbin.org") |
| 112 | +#' try(x$get("get")) |
| 113 | +#' \dontshow{stub_registry_clear()} |
| 114 | +#' |
| 115 | +#' # raise exception |
| 116 | +#' library(fauxpas) |
| 117 | +#' stub_request("get", "https://httpbin.org/get") %>% to_raise(HTTPAccepted) |
| 118 | +#' stub_request("get", "https://httpbin.org/get") %>% |
| 119 | +#' to_raise(HTTPAccepted, HTTPGone) |
| 120 | +#' |
| 121 | +#' x <- crul::HttpClient$new(url = "https://httpbin.org") |
| 122 | +#' stub_request("get", "https://httpbin.org/get") %>% to_raise(HTTPBadGateway) |
| 123 | +#' enable(adapter = "crul") |
| 124 | +#' try(x$get("get")) |
| 125 | +#' \dontshow{stub_registry_clear()} |
| 126 | +#' |
| 127 | +#' # pass a list to .list |
| 128 | +#' z <- stub_request("get", "https://httpbin.org/get") |
| 129 | +#' wi_th(z, .list = list(query = list(foo = "bar"))) |
| 130 | +#' \dontshow{stub_registry_clear()} |
| 131 | +#' |
| 132 | +#' # just body |
| 133 | +#' stub_request("any", uri_regex = ".+") %>% |
| 134 | +#' wi_th(body = list(foo = "bar")) |
| 135 | +#' ## with crul |
| 136 | +#' library(crul) |
| 137 | +#' x <- crul::HttpClient$new(url = "https://httpbin.org") |
| 138 | +#' enable(adapter = "crul") |
| 139 | +#' x$post("post", body = list(foo = "bar")) |
| 140 | +#' x$put("put", body = list(foo = "bar")) |
| 141 | +#' ## with httr |
| 142 | +#' library(httr) |
| 143 | +#' httr_mock() |
| 144 | +#' POST("https://example.com", body = list(foo = "bar")) |
| 145 | +#' PUT("https://google.com", body = list(foo = "bar")) |
| 146 | +#' \dontshow{stub_registry_clear()} |
| 147 | +#' |
| 148 | +#' # just headers |
| 149 | +#' headers <- list( |
| 150 | +#' "Accept-Encoding" = "gzip, deflate", |
| 151 | +#' "Accept" = "application/json, text/xml, application/xml, */*" |
| 152 | +#' ) |
| 153 | +#' stub_request("any", uri_regex = ".+") %>% wi_th(headers = headers) |
| 154 | +#' library(crul) |
| 155 | +#' x <- crul::HttpClient$new(url = "https://httpbin.org", headers = headers) |
| 156 | +#' enable(adapter = "crul") |
| 157 | +#' x$post("post") |
| 158 | +#' x$put("put", body = list(foo = "bar")) |
| 159 | +#' |
| 160 | +#' # many responses |
| 161 | +#' ## the first response matches the first to_return call, and so on |
| 162 | +#' stub_request("get", "https://httpbin.org/get") %>% |
| 163 | +#' to_return(status = 200, body = "foobar", headers = list(a = 5)) %>% |
| 164 | +#' to_return(status = 200, body = "bears", headers = list(b = 6)) |
| 165 | +#' con <- crul::HttpClient$new(url = "https://httpbin.org") |
| 166 | +#' con$get("get")$parse("UTF-8") |
| 167 | +#' con$get("get")$parse("UTF-8") |
| 168 | +#' \dontshow{stub_registry_clear()} |
| 169 | +#' |
| 170 | +#' ## OR, use times with to_return() to repeat the same response many times |
| 171 | +#' library(fauxpas) |
| 172 | +#' stub_request("get", "https://httpbin.org/get") %>% |
| 173 | +#' to_return(status = 200, body = "apple-pie", times = 2) %>% |
| 174 | +#' to_raise(HTTPUnauthorized) |
| 175 | +#' con <- crul::HttpClient$new(url = "https://httpbin.org") |
| 176 | +#' con$get("get")$parse("UTF-8") |
| 177 | +#' con$get("get")$parse("UTF-8") |
| 178 | +#' try(con$get("get")$parse("UTF-8")) |
| 179 | +#' \dontshow{stub_registry_clear()} |
| 180 | +#' |
| 181 | +#' # partial matching |
| 182 | +#' ## query parameters |
| 183 | +#' library(httr) |
| 184 | +#' enable(adapter = "httr") |
| 185 | +#' ### matches |
| 186 | +#' stub_request("get", "https://hb.opencpu.org/get") %>% |
| 187 | +#' wi_th(query = including(list(fruit = "pear"))) %>% |
| 188 | +#' to_return(body = "matched on partial query!") |
| 189 | +#' resp <- GET("https://hb.opencpu.org/get", |
| 190 | +#' query = list(fruit = "pear", bread = "scone") |
| 191 | +#' ) |
| 192 | +#' rawToChar(content(resp)) |
| 193 | +#' ### doesn't match |
| 194 | +#' stub_registry_clear() |
| 195 | +#' stub_request("get", "https://hb.opencpu.org/get") %>% |
| 196 | +#' wi_th(query = list(fruit = "pear")) %>% |
| 197 | +#' to_return(body = "didn't match, ugh!") |
| 198 | +#' try({ |
| 199 | +#' GET("https://hb.opencpu.org/get", |
| 200 | +#' query = list(fruit = "pear", meat = "chicken")) |
| 201 | +#' }) |
| 202 | +#' \dontshow{stub_registry_clear()} |
| 203 | +#' |
| 204 | +#' ## request body |
| 205 | +#' ### matches - including |
| 206 | +#' stub_request("post", "https://hb.opencpu.org/post") %>% |
| 207 | +#' wi_th(body = including(list(fruit = "pear"))) %>% |
| 208 | +#' to_return(body = "matched on partial body!") |
| 209 | +#' resp <- POST("https://hb.opencpu.org/post", |
| 210 | +#' body = list(fruit = "pear", meat = "chicken") |
| 211 | +#' ) |
| 212 | +#' rawToChar(content(resp)) |
| 213 | +#' ### matches - excluding |
| 214 | +#' stub_request("post", "https://hb.opencpu.org/post") %>% |
| 215 | +#' wi_th(body = excluding(list(fruit = "pear"))) %>% |
| 216 | +#' to_return(body = "matched on partial body!") |
| 217 | +#' res <- POST("https://hb.opencpu.org/post", |
| 218 | +#' body = list(color = "blue") |
| 219 | +#' ) |
| 220 | +#' rawToChar(content(res)) |
| 221 | +#' POST("https://hb.opencpu.org/post", |
| 222 | +#' body = list(fruit = "pear", meat = "chicken")) |
| 223 | +#' |
81 | 224 | #' # clear all stubs |
82 | 225 | #' stub_registry() |
83 | 226 | #' stub_registry_clear() |
|
0 commit comments