|
1 | 1 | #' @title RequestPattern class |
2 | 2 | #' @description class handling all request matchers |
3 | | -#' @export |
| 3 | +#' @keywords internal |
4 | 4 | #' @seealso pattern classes for HTTP method [MethodPattern], headers |
5 | 5 | #' [HeadersPattern], body [BodyPattern], and URI/URL [UriPattern] |
6 | | -#' @examples \dontrun{ |
7 | | -#' (x <- RequestPattern$new(method = "get", uri = "httpbin.org/get")) |
8 | | -#' x$body_pattern |
9 | | -#' x$headers_pattern |
10 | | -#' x$method_pattern |
11 | | -#' x$uri_pattern |
12 | | -#' x$to_s() |
13 | | -#' |
14 | | -#' # make a request signature |
15 | | -#' rs <- RequestSignature$new(method = "get", uri = "http://httpbin.org/get") |
16 | | -#' |
17 | | -#' # check if it matches |
18 | | -#' x$matches(rs) |
19 | | -#' |
20 | | -#' # regex uri |
21 | | -#' (x <- RequestPattern$new(method = "get", uri_regex = ".+ossref.org")) |
22 | | -#' x$uri_pattern |
23 | | -#' x$uri_pattern$to_s() |
24 | | -#' x$to_s() |
25 | | -#' |
26 | | -#' # uri with query parameters |
27 | | -#' (x <- RequestPattern$new( |
28 | | -#' method = "get", uri = "https://httpbin.org/get", |
29 | | -#' query = list(foo = "bar") |
30 | | -#' )) |
31 | | -#' x$to_s() |
32 | | -#' ## query params included in url, not separately |
33 | | -#' (x <- RequestPattern$new( |
34 | | -#' method = "get", uri = "https://httpbin.org/get?stuff=things" |
35 | | -#' )) |
36 | | -#' x$to_s() |
37 | | -#' x$query_params |
38 | | -#' |
39 | | -#' # just headers (via setting method=any & uri_regex=.+) |
40 | | -#' headers <- list( |
41 | | -#' "User-Agent" = "Apple", |
42 | | -#' "Accept-Encoding" = "gzip, deflate", |
43 | | -#' "Accept" = "application/json, text/xml, application/xml, */*" |
44 | | -#' ) |
45 | | -#' x <- RequestPattern$new( |
46 | | -#' method = "any", |
47 | | -#' uri_regex = ".+", |
48 | | -#' headers = headers |
49 | | -#' ) |
50 | | -#' x$to_s() |
51 | | -#' rs <- RequestSignature$new( |
52 | | -#' method = "any", uri = "http://foo.bar", |
53 | | -#' options = list(headers = headers) |
54 | | -#' ) |
55 | | -#' rs |
56 | | -#' x$matches(rs) |
57 | | -#' |
58 | | -#' # body |
59 | | -#' x <- RequestPattern$new( |
60 | | -#' method = "post", uri = "httpbin.org/post", |
61 | | -#' body = list(y = crul::upload(system.file("CITATION"))) |
62 | | -#' ) |
63 | | -#' x$to_s() |
64 | | -#' rs <- RequestSignature$new( |
65 | | -#' method = "post", uri = "http://httpbin.org/post", |
66 | | -#' options = list( |
67 | | -#' body = list(y = crul::upload(system.file("CITATION"))) |
68 | | -#' ) |
69 | | -#' ) |
70 | | -#' rs |
71 | | -#' x$matches(rs) |
72 | | -#' |
73 | | -#' # basic auth |
74 | | -#' x <- RequestPattern$new( |
75 | | -#' method = "post", |
76 | | -#' uri = "httpbin.org/post", |
77 | | -#' basic_auth = c("user", "pass") |
78 | | -#' ) |
79 | | -#' x |
80 | | -#' x$headers_pattern$to_s() |
81 | | -#' x$to_s() |
82 | | -#' rs <- RequestSignature$new( |
83 | | -#' method = "post", uri = "http://httpbin.org/post", |
84 | | -#' options = list(headers = prep_auth("user:pass")) |
85 | | -#' ) |
86 | | -#' rs |
87 | | -#' x$matches(rs) # TRUE |
88 | | -#' rs <- RequestSignature$new( |
89 | | -#' method = "post", uri = "http://httpbin.org/post", |
90 | | -#' options = list(headers = prep_auth("user:longpassword")) |
91 | | -#' ) |
92 | | -#' x$matches(rs) # FALSE |
93 | | -#' } |
94 | 6 | RequestPattern <- R6::R6Class( |
95 | 7 | "RequestPattern", |
96 | 8 | public = list( |
@@ -206,21 +118,8 @@ RequestPattern <- R6::R6Class( |
206 | 118 |
|
207 | 119 | #' @title MethodPattern |
208 | 120 | #' @description method matcher |
209 | | -#' @export |
210 | 121 | #' @keywords internal |
211 | 122 | #' @details Matches regardless of case. e.g., POST will match to post |
212 | | -#' @examples |
213 | | -#' (x <- MethodPattern$new(pattern = "post")) |
214 | | -#' x$pattern |
215 | | -#' x$matches(method = "post") |
216 | | -#' x$matches(method = "POST") |
217 | | -#' |
218 | | -#' # all matches() calls should be TRUE |
219 | | -#' (x <- MethodPattern$new(pattern = "any")) |
220 | | -#' x$pattern |
221 | | -#' x$matches(method = "post") |
222 | | -#' x$matches(method = "GET") |
223 | | -#' x$matches(method = "HEAD") |
224 | 123 | MethodPattern <- R6::R6Class( |
225 | 124 | "MethodPattern", |
226 | 125 | public = list( |
@@ -249,40 +148,13 @@ MethodPattern <- R6::R6Class( |
249 | 148 |
|
250 | 149 | #' @title HeadersPattern |
251 | 150 | #' @description headers matcher |
252 | | -#' @export |
253 | 151 | #' @keywords internal |
254 | 152 | #' @details |
255 | 153 | #' `webmockr` normalises headers and treats all forms of same headers as equal: |
256 | 154 | #' i.e the following two sets of headers are equal: |
257 | 155 | #' `list(Header1 = "value1", content_length = 123, X_CuStOm_hEAder = "foo")` |
258 | 156 | #' and |
259 | 157 | #' `list(header1 = "value1", "Content-Length" = 123, "x-cuSTOM-HeAder" = "foo")` |
260 | | -#' @examples |
261 | | -#' (x <- HeadersPattern$new(pattern = list(a = 5))) |
262 | | -#' x$pattern |
263 | | -#' x$matches(list(a = 5)) |
264 | | -#' |
265 | | -#' # different cases |
266 | | -#' (x <- HeadersPattern$new(pattern = list(Header1 = "value1"))) |
267 | | -#' x$pattern |
268 | | -#' x$matches(list(header1 = "value1")) |
269 | | -#' x$matches(list(header1 = "value2")) |
270 | | -#' |
271 | | -#' # different symbols |
272 | | -#' (x <- HeadersPattern$new(pattern = list(`Hello_World` = "yep"))) |
273 | | -#' x$pattern |
274 | | -#' x$matches(list(`hello-world` = "yep")) |
275 | | -#' x$matches(list(`hello-worlds` = "yep")) |
276 | | -#' |
277 | | -#' headers <- list( |
278 | | -#' "User-Agent" = "Apple", |
279 | | -#' "Accept-Encoding" = "gzip, deflate", |
280 | | -#' "Accept" = "application/json, text/xml, application/xml, */*" |
281 | | -#' ) |
282 | | -#' (x <- HeadersPattern$new(pattern = headers)) |
283 | | -#' x$to_s() |
284 | | -#' x$pattern |
285 | | -#' x$matches(headers) |
286 | 158 | HeadersPattern <- R6::R6Class( |
287 | 159 | "HeadersPattern", |
288 | 160 | public = list( |
@@ -350,69 +222,7 @@ seems_like_json <- function(x) { |
350 | 222 |
|
351 | 223 | #' @title BodyPattern |
352 | 224 | #' @description body matcher |
353 | | -#' @export |
354 | 225 | #' @keywords internal |
355 | | -#' @examples |
356 | | -#' # make a request signature |
357 | | -#' bb <- RequestSignature$new( |
358 | | -#' method = "get", |
359 | | -#' uri = "https:/httpbin.org/get", |
360 | | -#' options = list( |
361 | | -#' body = list(foo = "bar", a = 5) |
362 | | -#' ) |
363 | | -#' ) |
364 | | -#' |
365 | | -#' # make body pattern object |
366 | | -#' ## FALSE |
367 | | -#' z <- BodyPattern$new(pattern = list(foo = "bar")) |
368 | | -#' z$pattern |
369 | | -#' z$matches(bb$body) |
370 | | -#' ## TRUE |
371 | | -#' z <- BodyPattern$new(pattern = list(foo = "bar", a = 5)) |
372 | | -#' z$pattern |
373 | | -#' z$matches(bb$body) |
374 | | -#' |
375 | | -#' # uploads in bodies |
376 | | -#' ## upload NOT in a list |
377 | | -#' bb <- RequestSignature$new( |
378 | | -#' method = "post", uri = "https:/httpbin.org/post", |
379 | | -#' options = list(body = crul::upload(system.file("CITATION"))) |
380 | | -#' ) |
381 | | -#' bb$body |
382 | | -#' z <- BodyPattern$new( |
383 | | -#' pattern = |
384 | | -#' crul::upload(system.file("CITATION")) |
385 | | -#' ) |
386 | | -#' z$pattern |
387 | | -#' z$matches(bb$body) |
388 | | -#' |
389 | | -#' ## upload in a list |
390 | | -#' bb <- RequestSignature$new( |
391 | | -#' method = "post", uri = "https:/httpbin.org/post", |
392 | | -#' options = list(body = list(y = crul::upload(system.file("CITATION")))) |
393 | | -#' ) |
394 | | -#' bb$body |
395 | | -#' z <- BodyPattern$new( |
396 | | -#' pattern = |
397 | | -#' list(y = crul::upload(system.file("CITATION"))) |
398 | | -#' ) |
399 | | -#' z$pattern |
400 | | -#' z$matches(bb$body) |
401 | | -#' |
402 | | -#' # partial matching |
403 | | -#' ## including |
404 | | -#' partial_incl <- including(list(foo = "bar")) |
405 | | -#' z <- BodyPattern$new(pattern = partial_incl) |
406 | | -#' z$pattern |
407 | | -#' z$matches(list(foo = "bar", a = 5)) # TRUE |
408 | | -#' |
409 | | -#' ## excluding |
410 | | -#' partial_excl <- excluding(list(hello = "world")) |
411 | | -#' z <- BodyPattern$new(pattern = partial_excl) |
412 | | -#' z$pattern |
413 | | -#' z$matches(list(a = 5)) # TRUE |
414 | | -#' z$matches(list(hello = "mars", a = 5)) # TRUE |
415 | | -#' z$matches(list(hello = "world")) # FALSE |
416 | 226 | BodyPattern <- R6::R6Class( |
417 | 227 | "BodyPattern", |
418 | 228 | public = list( |
@@ -606,98 +416,7 @@ promote_attr <- function(ll) { |
606 | 416 |
|
607 | 417 | #' @title UriPattern |
608 | 418 | #' @description uri matcher |
609 | | -#' @export |
610 | 419 | #' @keywords internal |
611 | | -#' @examples |
612 | | -#' # trailing slash |
613 | | -#' (z <- UriPattern$new(pattern = "http://foobar.com")) |
614 | | -#' z$matches("http://foobar.com") # TRUE |
615 | | -#' z$matches("http://foobar.com/") # TRUE |
616 | | -#' |
617 | | -#' # without scheme |
618 | | -#' ## matches http by default: does not match https by default |
619 | | -#' (z <- UriPattern$new(pattern = "foobar.com")) |
620 | | -#' z$matches("http://foobar.com") # TRUE |
621 | | -#' z$matches("http://foobar.com/") # TRUE |
622 | | -#' z$matches("https://foobar.com") # FALSE |
623 | | -#' z$matches("https://foobar.com/") # FALSE |
624 | | -#' ## to match https, you'll have to give the complete url |
625 | | -#' (z <- UriPattern$new(pattern = "https://foobar.com")) |
626 | | -#' z$matches("https://foobar.com/") # TRUE |
627 | | -#' z$matches("http://foobar.com/") # FALSE |
628 | | -#' |
629 | | -#' # default ports |
630 | | -#' (z <- UriPattern$new(pattern = "http://foobar.com")) |
631 | | -#' z$matches("http://foobar.com:80") # TRUE |
632 | | -#' z$matches("http://foobar.com:80/") # TRUE |
633 | | -#' z$matches("http://foobar.com:443") # TRUE |
634 | | -#' z$matches("http://foobar.com:443/") # TRUE |
635 | | -#' |
636 | | -#' # user info - FIXME, not sure we support this yet |
637 | | -#' (z <- UriPattern$new(pattern = "http://foobar.com")) |
638 | | -#' z$matches("http://user:pass@foobar.com") |
639 | | -#' |
640 | | -#' # regex |
641 | | -#' (z <- UriPattern$new(regex_pattern = ".+ample\\..")) |
642 | | -#' z$matches("http://sample.org") # TRUE |
643 | | -#' z$matches("http://example.com") # TRUE |
644 | | -#' z$matches("http://tramples.net") # FALSE |
645 | | -#' |
646 | | -#' # add query parameters |
647 | | -#' (z <- UriPattern$new(pattern = "http://foobar.com")) |
648 | | -#' z$add_query_params(list(pizza = "cheese", cheese = "cheddar")) |
649 | | -#' z |
650 | | -#' z$pattern |
651 | | -#' z$matches("http://foobar.com?pizza=cheese&cheese=cheddar") # TRUE |
652 | | -#' z$matches("http://foobar.com?pizza=cheese&cheese=swiss") # FALSE |
653 | | -#' |
654 | | -#' # query parameters in the uri |
655 | | -#' (z <- UriPattern$new(pattern = "https://httpbin.org/get?stuff=things")) |
656 | | -#' z$add_query_params() # have to run this method to gather query params |
657 | | -#' z$matches("https://httpbin.org/get?stuff=things") # TRUE |
658 | | -#' z$matches("https://httpbin.org/get?stuff2=things") # FALSE |
659 | | -#' |
660 | | -#' # regex add query parameters |
661 | | -#' (z <- UriPattern$new(regex_pattern = "https://foobar.com/.+/order")) |
662 | | -#' z$add_query_params(list(pizza = "cheese")) |
663 | | -#' z |
664 | | -#' z$pattern |
665 | | -#' z$matches("https://foobar.com/pizzas/order?pizza=cheese") # TRUE |
666 | | -#' z$matches("https://foobar.com/pizzas?pizza=cheese") # FALSE |
667 | | -#' |
668 | | -#' # query parameters in the regex uri |
669 | | -#' (z <- UriPattern$new(regex_pattern = "https://x.com/.+/order\\?fruit=apple")) |
670 | | -#' z$add_query_params() # have to run this method to gather query params |
671 | | -#' z$matches("https://x.com/a/order?fruit=apple") # TRUE |
672 | | -#' z$matches("https://x.com/a?fruit=apple") # FALSE |
673 | | -#' |
674 | | -#' # any pattern |
675 | | -#' (z <- UriPattern$new(regex_pattern = "stuff\\.com.+")) |
676 | | -#' z$regex |
677 | | -#' z$pattern |
678 | | -#' z$matches("http://stuff.com") # FALSE |
679 | | -#' z$matches("https://stuff.com/stff") # TRUE |
680 | | -#' z$matches("https://stuff.com/apple?bears=brown&bats=grey") # TRUE |
681 | | -#' |
682 | | -#' # partial matching |
683 | | -#' ## including |
684 | | -#' z <- UriPattern$new(pattern = "http://foobar.com") |
685 | | -#' z$add_query_params(including(list(hello = "world"))) |
686 | | -#' z$matches(uri = "http://foobar.com?hello=world&bye=mars") # TRUE |
687 | | -#' z$matches("http://foobar.com?bye=mars") # FALSE |
688 | | -#' |
689 | | -#' ## excluding |
690 | | -#' z <- UriPattern$new(pattern = "http://foobar.com") |
691 | | -#' z$add_query_params(excluding(list(hello = "world"))) |
692 | | -#' z$matches(uri = "http://foobar.com?hello=world&bye=mars") # FALSE |
693 | | -#' z$matches("http://foobar.com?bye=mars") # TRUE |
694 | | -#' |
695 | | -#' ## match on list keys (aka: names) only, ignore values 0 |
696 | | -#' z <- UriPattern$new(pattern = "http://foobar.com") |
697 | | -#' z$add_query_params(including(list(hello = NULL))) |
698 | | -#' z$matches(uri = "http://foobar.com?hello=world&bye=mars") # TRUE |
699 | | -#' z$matches("http://foobar.com?hello=stuff") # TRUE |
700 | | -#' z$matches("http://foobar.com?bye=stuff") # FALSE |
701 | 420 | UriPattern <- R6::R6Class( |
702 | 421 | "UriPattern", |
703 | 422 | public = list( |
|
0 commit comments