-
Notifications
You must be signed in to change notification settings - Fork 51
/
test-req-url.R
58 lines (49 loc) · 2.61 KB
/
test-req-url.R
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
test_that("can override url", {
req <- request("http://example.com/")
expect_equal(req_url(req, "http://foo.com:10")$url, "http://foo.com:10")
})
test_that("automatically adds /", {
req1 <- request("http://example.com")
req2 <- request("http://example.com/")
expect_equal(req_url_path(req1, "/index.html")$url, "http://example.com/index.html")
expect_equal(req_url_path(req1, "index.html")$url, "http://example.com/index.html")
expect_equal(req_url_path(req2, "/index.html")$url, "http://example.com/index.html")
expect_equal(req_url_path(req2, "index.html")$url, "http://example.com/index.html")
expect_equal(req_url_path_append(req1, "index.html")$url, "http://example.com/index.html")
expect_equal(req_url_path_append(req1, "/index.html")$url, "http://example.com/index.html")
expect_equal(req_url_path_append(req2, "index.html")$url, "http://example.com/index.html")
# specifically requested to add in both
expect_equal(req_url_path_append(req2, "/index.html")$url, "http://example.com//index.html")
})
test_that("can append multiple components", {
req <- request("http://example.com/x")
expect_equal(req_url_path(req, "a", "b")$url, "http://example.com/a/b")
expect_equal(req_url_path_append(req, "a", "b")$url, "http://example.com/x/a/b")
})
test_that("can set query params", {
req <- request("http://example.com/")
expect_equal(req_url_query(req, a = 1, b = 2)$url, "http://example.com/?a=1&b=2")
expect_equal(req_url_query(req, a = 1, b = 2, c = NULL)$url, "http://example.com/?a=1&b=2")
expect_equal(req_url_query(req, !!!list(a = 1, b = 2))$url, "http://example.com/?a=1&b=2")
expect_equal(req_url_query(req, a = 1, a = 2)$url, "http://example.com/?a=1&a=2")
expect_equal(req_url_query(req, !!!list(a = 1, a = 2))$url, "http://example.com/?a=1&a=2")
})
test_that("empty query doesn't affect url", {
req <- request("http://example.com/")
expect_equal(req_url_query(req)$url, "http://example.com/")
expect_equal(req_url_query(req, a = NULL)$url, "http://example.com/")
})
test_that("can modify query params iteratively", {
req <- request("http://example.com/?a=1&b=2")
expect_equal(req_url_query(req, c = 3)$url, "http://example.com/?a=1&b=2&c=3")
expect_equal(req_url_query(req, a = 2)$url, "http://example.com/?b=2&a=2")
expect_equal(req_url_query(req, a = 1, a = 2)$url, "http://example.com/?b=2&a=1&a=2")
expect_equal(req_url_query(req, b = NULL)$url, "http://example.com/?a=1")
})
test_that("query components must be length 1", {
expect_snapshot(error = TRUE, {
req <- request("http://example.com/")
req %>% req_url_query(a = mean)
req %>% req_url_query(a = letters)
})
})