File: test-resp-headers.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 (76 lines) | stat: -rw-r--r-- 2,692 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
test_that("can extract headers and check for existence", {
  resp <- response(headers = "Content-Type: application/json")
  expect_type(resp_headers(resp), "list")
  expect_equal(resp_header(resp, "Content-Type"), "application/json")
  expect_equal(resp_header_exists(resp, "Content-Type"), TRUE)
})

test_that("headers are case-insenstive", {
  resp <- response(headers = "Content-Type: application/json")
  expect_equal(resp_header(resp, "content-type"), "application/json")
  expect_equal(resp_header_exists(resp, "content-type"), TRUE)
})

test_that("has tools for non-existent headers", {
  resp <- response()

  expect_equal(resp_header(resp, "non-existent"), NULL)
  expect_equal(resp_header(resp, "non-existent", "xyz"), "xyz")
  expect_equal(resp_header_exists(resp, "non-existent"), FALSE)
})

test_that("can extract content type/encoding", {
  resp <- response(headers = "Content-Type: text/html; charset=latin1")
  expect_equal(resp_content_type(resp), "text/html")
  expect_equal(resp_encoding(resp), "latin1")
})

test_that("can parse date header", {
  resp <- response(headers = "Date: Mon, 18 Jul 2016 16:06:00 GMT")
  expect_equal(resp_date(resp), local_time('2016-07-18 16:06:06'))
})

test_that("can parse both forms of retry-after header", {
  resp_abs <- response(headers = c(
    "Retry-After: Mon, 18 Jul 2016 16:06:10 GMT",
    "Date: Mon, 18 Jul 2016 16:06:00 GMT"
  ))
  expect_equal(resp_retry_after(resp_abs), 10)

  resp_rel <- response(headers = c(
    "Retry-After: 20"
  ))
  expect_equal(resp_retry_after(resp_rel), 20)

  resp_rel <- response()
  expect_equal(resp_retry_after(resp_rel), NA)
})

# resp_link_url() --------------------------------------------------------------

test_that("can extract specified link url", {
  resp <- response(headers = paste0(
    'Link: <https://example.com/1>; rel="next",',
    '<https://example.com/2>; rel="last"'
  ))
  expect_equal(resp_link_url(resp, "next"), "https://example.com/1")
  expect_equal(resp_link_url(resp, "last"), "https://example.com/2")

  # Falling back to NULL if not found
  expect_equal(resp_link_url(resp, "first"), NULL)
  expect_equal(resp_link_url(response(), "first"), NULL)
})

test_that("can extract from multiple link headers", {
  resp <- response(headers = c(
    'Link: <https://example.com/1>; rel="next"',
    'Link: <https://example.com/2>; rel="last"'
  ))
  expect_equal(resp_link_url(resp, "next"), "https://example.com/1")
  expect_equal(resp_link_url(resp, "last"), "https://example.com/2")
})

test_that("is case insensitive", {
  resp <- response(headers = 'LINK: <https://example.com/1>; rel="next"')
  expect_equal(resp_link_url(resp, "next"), "https://example.com/1")
})