File: test-req-template.R

package info (click to toggle)
r-cran-httr2 1.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,604 kB
  • sloc: sh: 21; makefile: 2
file content (61 lines) | stat: -rw-r--r-- 1,698 bytes parent folder | download | duplicates (2)
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
59
60
61
test_that("can set path", {
  req <- request("http://test.com") %>% req_template("/x")
  expect_equal(req$url, "http://test.com/x")
})

test_that("can set method and path", {
  req <- request("http://test.com") %>% req_template("PATCH /x")
  expect_equal(req$url, "http://test.com/x")
  expect_equal(req$method, "PATCH")
})

test_that("can use args or env", {
  x <- "x"
  req <- request("http://test.com") %>% req_template("/:x")
  expect_equal(req$url, "http://test.com/x")

  req <- request("http://test.com") %>% req_template("/:x", x = "y")
  expect_equal(req$url, "http://test.com/y")
})

test_that("will append rather than replace path", {
  req <- request("http://test.com/x") %>% req_template("PATCH /y")
  expect_equal(req$url, "http://test.com/x/y")
})

test_that("generates useful errors", {
  req <- request("http://test.com")

  expect_snapshot(error = TRUE, {
    req_template(req, 1)
    req_template(req, "x", 1)
    req_template(req, "A B C")
  })
})

# templating --------------------------------------------------------------

test_that("template_process looks in args & env", {
  a <- 1
  expect_equal(template_process(":a"), "1")
  expect_equal(template_process(":a", list(a = 2)), "2")
})

test_that("template produces useful errors", {
  expect_snapshot(error = TRUE, {
    template_process(":b")
    template_process(":b", list(b = sum))
  })
})

test_that("supports three template styles", {
  x <- "x"
  expect_equal(template_process("/:x/"), "/x/")
  expect_equal(template_process("/{x}/"), "/x/")
  expect_equal(template_process("/constant"), "/constant")
})

test_that("can use colon in uri style", {
  x <- "x"
  expect_equal(template_process("/:{x}:/"), "/:x:/")
})