Skip to content

Commit 4f00350

Browse files
authored
example_url() improvements (#665)
Fixes #663
1 parent 38b7317 commit 4f00350

File tree

7 files changed

+19
-26
lines changed

7 files changed

+19
-26
lines changed

R/test.R

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ request_test <- function(template = "/get", ...) {
1818
#'
1919
#' @keywords internal
2020
#' @export
21-
example_url <- function() {
21+
example_url <- function(path = "/") {
2222
check_installed("webfakes")
2323
if (is_testing() && !is_interactive()) {
2424
testthat::skip_on_covr()
2525
}
26+
env_cache(the, "test_app", example_app())
27+
the$test_app$url(path)
28+
}
2629

30+
example_app <- function() {
2731
app <- webfakes::httpbin_app()
2832
# paginated iris endpoint
2933
app$get("/iris", function(req, res) {
@@ -47,13 +51,10 @@ example_url <- function() {
4751
)
4852
})
4953

50-
env_cache(the, "test_app",
51-
webfakes::new_app_process(
52-
app,
53-
opts = webfakes::server_opts(num_threads = 6, enable_keep_alive = TRUE)
54-
)
54+
webfakes::new_app_process(
55+
app,
56+
opts = webfakes::server_opts(num_threads = 6, enable_keep_alive = TRUE)
5557
)
56-
the$test_app$url()
5758
}
5859

5960
#' @export

man/example_url.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.

tests/testthat/test-iterate.R

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
test_that("can perform multiple requests", {
2-
req <- request(example_url()) %>%
3-
req_url_path("/iris") %>%
2+
req <- request(example_url("/iris")) %>%
43
req_url_query(limit = 5)
54

65
resps <- req_perform_iterative(
@@ -14,8 +13,7 @@ test_that("can perform multiple requests", {
1413
})
1514

1615
test_that("can save results to disk", {
17-
req <- request(example_url()) %>%
18-
req_url_path("/iris") %>%
16+
req <- request(example_url("/iris")) %>%
1917
req_url_query(limit = 5)
2018

2119
dir <- withr::local_tempdir()
@@ -32,8 +30,7 @@ test_that("can save results to disk", {
3230
})
3331

3432
test_that("user temination still returns data", {
35-
req <- request(example_url()) %>%
36-
req_url_path("/iris") %>%
33+
req <- request(example_url("/iris")) %>%
3734
req_url_query(limit = 5)
3835
next_req <- function(resp, req) interrupt()
3936

@@ -45,8 +42,7 @@ test_that("user temination still returns data", {
4542

4643

4744
test_that("can retrieve all pages", {
48-
req <- request(example_url()) %>%
49-
req_url_path("/iris") %>%
45+
req <- request(example_url("/iris")) %>%
5046
req_url_query(limit = 1)
5147

5248
i <- 1

tests/testthat/test-req-cookies.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,16 @@ test_that("can read/write cookies", {
2222
})
2323

2424
test_that("can set cookies", {
25-
resp <- request(example_url()) %>%
25+
resp <- request(example_url("/cookies")) %>%
2626
req_cookies_set(a = 1, b = 1) %>%
27-
req_url_path("/cookies") %>%
2827
req_perform()
2928

3029
expect_equal(resp_body_json(resp), list(cookies = list(a = "1", b = "1")))
3130
})
3231

3332
test_that("cookie values are usually escaped", {
34-
resp <- request(example_url()) %>%
33+
resp <- request(example_url("/cookies")) %>%
3534
req_cookies_set(a = I("%20"), b = "%") %>%
36-
req_url_path("/cookies") %>%
3735
req_perform()
3836

3937
expect_equal(resp_body_json(resp), list(cookies = list(a = "%20", b = "%25")))

tests/testthat/test-req-perform-stream.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
test_that("req_stream() is deprecated", {
2-
req <- request(example_url()) %>% req_url_path("/stream-bytes/100")
2+
req <- request(example_url("/stream-bytes/100"))
33
expect_snapshot(
44
resp <- req_stream(req, identity, buffer_kb = 32)
55
)

tests/testthat/test-req-perform.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ test_that("can cache requests with etags", {
114114
})
115115

116116
test_that("can cache requests with paths (cache-control)", {
117-
req <- request(example_url()) %>%
118-
req_url_path("/cache/2") %>%
117+
req <- request(example_url("/cache/2")) %>%
119118
req_cache(withr::local_tempfile())
120119

121120
path1 <- withr::local_tempfile()
@@ -146,8 +145,7 @@ test_that("can cache requests with paths (cache-control)", {
146145
})
147146

148147
test_that("can cache requests with paths (if-modified-since)", {
149-
req <- request(example_url()) %>%
150-
req_url_path("/cache") %>%
148+
req <- request(example_url("/cache")) %>%
151149
req_cache(tempfile())
152150

153151
path1 <- tempfile()

tests/testthat/test-resp-url.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
test_that("can extract url components from a response", {
22
resp <- req_perform(request_test("/get?a=1&b=2"))
33

4-
expect_equal(resp_url(resp), paste0(example_url(), "get?a=1&b=2"))
4+
expect_equal(resp_url(resp), example_url("/get?a=1&b=2"))
55
expect_equal(resp_url_path(resp), "/get")
66
expect_equal(resp_url_queries(resp), list(a = "1", b = "2"))
77

0 commit comments

Comments
 (0)