File: test-req-headers.R

package info (click to toggle)
r-cran-httr2 1.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,684 kB
  • sloc: sh: 13; makefile: 2
file content (127 lines) | stat: -rw-r--r-- 4,134 bytes parent folder | download
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
test_that("can add and remove headers", {
  req <- request("http://example.com")
  req <- req |> req_headers(x = 1)
  expect_equal(req$headers, new_headers(list(x = 1)))
  req <- req |> req_headers(x = NULL)
  expect_equal(req$headers, new_headers(list()))
})

test_that("simple vectors automatically converted to strings", {
  req <- request("http://example.com")
  req <- req |> req_headers(lgl = TRUE, int = 1L, dbl = 1.1, chr = "a")
  resp <- req_dry_run(req, quiet = TRUE)

  expect_equal(resp$headers$lgl, "TRUE")
  expect_equal(resp$headers$int, "1")
  expect_equal(resp$headers$dbl, "1.1")
  expect_equal(resp$headers$chr, "a")
})

test_that("bad inputs get clear error", {
  req <- request("http://example.com")
  expect_snapshot(error = TRUE, {
    req_headers(req, fun = mean)
    req_headers(req, 1)
  })
})

test_that("can add header called req", {
  req <- request("http://example.com")
  req <- req |> req_headers(req = 1)
  expect_equal(req$headers, new_headers(list(req = 1)))
})

test_that("can add repeated headers", {
  resp <- request_test() |>
    req_headers(a = c("a", "b")) |>
    req_dry_run(quiet = TRUE)
  # https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.2
  expect_equal(resp$headers$a, "a,b")
})

test_that("replacing headers is case-insensitive", {
  req <- request("http://example.com")
  req <- req |> req_headers(A = 1)
  req <- req |> req_headers(a = 2)
  expect_equal(req$headers, new_headers(list(a = 2)))
})

# accessor ----------------------------------------------------------------

test_that("can control redaction", {
  req <- request("http://example.com")
  req <- req_headers(req, a = 1L, b = 2L, .redact = "a")

  expect_equal(req_get_headers(req, "drop"), list(b = "2"))
  expect_equal(req_get_headers(req, "redact"), list(a = "<REDACTED>", b = "2"))
  expect_equal(req_get_headers(req, "reveal"), list(a = "1", b = "2"))
})

test_that("empty redacted headers are always dropped", {
  req <- request("http://example.com")
  req <- req_headers(req, a = 1L, b = 2L, .redact = "a")
  req2 <- unserialize(serialize(req, NULL))

  expect_equal(req_get_headers(req2, "drop"), list(b = "2"))
  expect_equal(req_get_headers(req2, "redact"), list(b = "2"))
  expect_equal(req_get_headers(req2, "reveal"), list(b = "2"))
})

# redaction ---------------------------------------------------------------

test_that("can control which headers to redact", {
  req <- request("http://example.com")
  expect_redacted(req_headers(req, a = 1L, b = 2L), character())
  expect_redacted(req_headers(req, a = 1L, b = 2L, .redact = "a"), "a")
  expect_redacted(
    req_headers(req, a = 1L, b = 2L, .redact = c("a", "b")),
    c("a", "b")
  )
})

test_that("only redacts supplied headers", {
  req <- request("http://example.com")
  expect_redacted(req_headers(req, a = 1L, b = 2L, .redact = "d"), character())
})

test_that("redaction preserved across calls", {
  req <- request("http://example.com")
  req <- req_headers(req, a = 1L, .redact = "a")
  req <- req_headers(req, a = 2)
  expect_redacted(req, "a")
  expect_equal(headers_flatten(req$headers, FALSE), list(a = "2"))

  # and is reapplied regadless of case
  req <- req_headers(req, A = 3)
  expect_redacted(req, "A")
  expect_equal(headers_flatten(req$headers, FALSE), list(A = "3"))
})

test_that("req_headers_redacted redacts all headers", {
  req <- request("http://example.com")
  expect_redacted(req_headers_redacted(req, a = 1L, b = 2L), c("a", "b"))
})

test_that("is case insensitive", {
  req <- request("http://example.com")
  req <- req_headers(req, a = 1L, .redact = "A")
  expect_redacted(req, "a")
  expect_snapshot(req)

  # Test the other direction too, just to be safe
  req <- request("http://example.com")
  req <- req_headers(req, A = 1L, .redact = "a")
  expect_redacted(req, "A")
})

test_that("authorization is always redacted", {
  req <- request("http://example.com")
  expect_redacted(req_headers(req, Authorization = "X"), "Authorization")
})

test_that("checks input types", {
  req <- request("http://example.com")
  expect_snapshot(error = TRUE, {
    req_headers(req, a = 1L, b = 2L, .redact = 1L)
  })
})