Skip to content

Commit 580b5f1

Browse files
committed
#145 request_registry_filter to new file, add tests, bump version, add news
1 parent a75ff6b commit 580b5f1

6 files changed

Lines changed: 320 additions & 79 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Description: Stubbing and setting expectations on 'HTTP' requests.
66
'HTTP' method, query parameters, request body, headers and
77
more. Can be used for unit tests or outside of a testing
88
context.
9-
Version: 2.2.0.9000
9+
Version: 2.2.1.91
1010
Authors@R: c(
1111
person("Scott", "Chamberlain", role = c("aut", "cre"), email =
1212
"myrmecocystus+r@gmail.com", comment = c(ORCID="0000-0003-1444-9135")),

NEWS.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
# webmockr (development version)
1+
webmockr (development version)
2+
==============
3+
4+
* Gains new function `request_registry_filter()` to filter requests that have matched stubs. can be used to introspect requests made during tests, or outside of tests (#145)
25

36
webmockr 2.2.0
47
==============

R/RequestRegistry.R

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -104,79 +104,3 @@ RequestRegistry <- R6::R6Class(
104104
}
105105
)
106106
)
107-
108-
#' Request registry filter
109-
#'
110-
#' If no filters are given, returns all requests
111-
#'
112-
#' @export
113-
#' @param method (character) http method. default: `NULL`
114-
#' @param url (character) a url. default: `NULL`
115-
#' @param body (various) a request body. default: `NULL`.
116-
#' Can be any of: `character`, `json`, `list`, `raw`, `numeric`,
117-
#' `NULL`, `FALSE`
118-
#' @param headers (list) if given, must be a named list. default: `NULL`
119-
#' @return list, length of number of unique requests recorded, or
120-
#' those requests matching filters supplied by parameters
121-
#' @examples
122-
#' enable(adapter="httr")
123-
#'
124-
#' stub_request("any", uri_regex = ".+")
125-
#'
126-
#' library(httr)
127-
#' GET("http://example.com")
128-
#' GET("https://hb.cran.dev/get")
129-
#' POST("https://hb.cran.dev/post", body = list(fruit = "apple"))
130-
#' POST("https://hb.cran.dev/post", body = list(cheese = "swiss"))
131-
#' GET("https://hb.cran.dev/get", add_headers(Accept = "application/json"))
132-
#'
133-
#' request_registry_filter()
134-
#' request_registry_filter(method="get")
135-
#' request_registry_filter(method="post")
136-
#' request_registry_filter(method="get", url="http://example.com")
137-
#' request_registry_filter(method="post", body=list(fruit = "apple"))
138-
#' request_registry_filter(method="post", body=list(cheese = "swiss"))
139-
#' request_registry_filter(method="post", body=list(cheese = "cheddar"))
140-
#' request_registry_filter(method="get", headers=list(Accept = "application/json"))
141-
#'
142-
#' match <- request_registry_filter(method="post")[[1]]
143-
#' match$request
144-
#' match$request$to_s()
145-
#' match$count
146-
#'
147-
#' disable()
148-
request_registry_filter <- function(
149-
method = NULL,
150-
url = NULL,
151-
body = NULL,
152-
headers = NULL
153-
) {
154-
assert_is(url, "character")
155-
assert_is(method, "character")
156-
assert_is(headers, "list")
157-
if (!all(hz_namez(headers))) {
158-
abort("'headers' must be a named list")
159-
}
160-
data <- request_registry()
161-
reqs <- unname(data$request_signatures$hash)
162-
out <- lapply(reqs, \(x) {
163-
tmp <- c(key = x$key, count = x$count, x$sig$as_list(), x)
164-
names(tmp)[names(tmp) == "sig"] <- "request"
165-
tmp
166-
})
167-
168-
if (!is.null(method)) {
169-
out <- Filter(\(x) MethodPattern$new(x$method)$matches(method), out)
170-
}
171-
if (!is.null(url)) {
172-
out <- Filter(\(x) UriPattern$new(x$uri)$matches(url), out)
173-
}
174-
if (!is.null(body)) {
175-
out <- Filter(\(x) BodyPattern$new(x$body)$matches(body), out)
176-
}
177-
if (!is.null(headers)) {
178-
out <- Filter(\(x) HeadersPattern$new(x$headers)$matches(headers), out)
179-
}
180-
181-
out
182-
}

R/request_registry_filter.R

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
convert_registry_to_list <- function() {
2+
data <- request_registry()
3+
reqs <- unname(data$request_signatures$hash)
4+
out <- lapply(reqs, \(x) {
5+
tmp <- c(key = x$key, count = x$count, x$sig$as_list(), x)
6+
names(tmp)[names(tmp) == "sig"] <- "request"
7+
tmp
8+
})
9+
out
10+
}
11+
12+
#' Request registry filter
13+
#'
14+
#' If no filters are given, returns all requests
15+
#'
16+
#' @export
17+
#' @param method (character) http method. default: `NULL`
18+
#' @param url (character) a url. default: `NULL`
19+
#' @param body (various) a request body. default: `NULL`.
20+
#' Can be any of: `character`, `json`, `list`, `raw`, `numeric`,
21+
#' `NULL`, `FALSE`
22+
#' @param headers (list) if given, must be a named list. default: `NULL`
23+
#' @return list, length of number of unique requests recorded, or
24+
#' those requests matching filters supplied by parameters
25+
#' @examples
26+
#' enable(adapter="httr")
27+
#'
28+
#' stub_request("any", uri_regex = ".+")
29+
#'
30+
#' library(httr)
31+
#' GET("http://example.com")
32+
#' GET("https://hb.cran.dev/get")
33+
#' POST("https://hb.cran.dev/post", body = list(fruit = "apple"))
34+
#' POST("https://hb.cran.dev/post", body = list(cheese = "swiss"))
35+
#' GET("https://hb.cran.dev/get", add_headers(Accept = "application/json"))
36+
#'
37+
#' request_registry_filter()
38+
#' request_registry_filter(method="get")
39+
#' request_registry_filter(method="post")
40+
#' request_registry_filter(method="get", url="http://example.com")
41+
#' request_registry_filter(method="post", body=list(fruit = "apple"))
42+
#' request_registry_filter(method="post", body=list(cheese = "swiss"))
43+
#' request_registry_filter(method="post", body=list(cheese = "cheddar"))
44+
#' request_registry_filter(method="get", headers=list(Accept = "application/json"))
45+
#'
46+
#' match <- request_registry_filter(method="post")[[1]]
47+
#' match$request
48+
#' match$request$to_s()
49+
#' match$count
50+
#'
51+
#' disable()
52+
request_registry_filter <- function(
53+
method = NULL,
54+
url = NULL,
55+
body = NULL,
56+
headers = NULL
57+
) {
58+
assert_is(url, "character")
59+
assert_is(method, "character")
60+
assert_is(headers, "list")
61+
if (!all(hz_namez(headers))) {
62+
abort("'headers' must be a named list")
63+
}
64+
requests <- convert_registry_to_list()
65+
66+
if (!is.null(method)) {
67+
requests <- Filter(
68+
\(x) MethodPattern$new(x$method)$matches(method),
69+
requests
70+
)
71+
}
72+
if (!is.null(url)) {
73+
requests <- Filter(\(x) UriPattern$new(x$uri)$matches(url), requests)
74+
}
75+
if (!is.null(body)) {
76+
requests <- Filter(\(x) BodyPattern$new(x$body)$matches(body), requests)
77+
}
78+
if (!is.null(headers)) {
79+
requests <- Filter(
80+
\(x) HeadersPattern$new(x$headers)$matches(headers),
81+
requests
82+
)
83+
}
84+
85+
requests
86+
}

man/request_registry_filter.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)