File: test-parse.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 (90 lines) | stat: -rw-r--r-- 2,528 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
test_that("can parse media type", {
  # no params
  expect_equal(parse_media("text/plain"), list(type = "text/plain"))

  # single param
  expect_equal(
    parse_media("text/plain; charset=utf-8"),
    list(type = "text/plain", charset = "utf-8")
  )

  # single param with quotes
  expect_equal(
    parse_media("text/plain; charset=\"utf-8\""),
    list(type = "text/plain", charset = "utf-8")
  )

  # quoted param containing ;
  expect_equal(
    parse_media("text/plain; charset=\";\""),
    list(type = "text/plain", charset = ";")
  )

  expect_equal(parse_media(""), list(type = NA_character_))
})

test_that("can parse authenticate header", {
  header <- paste0(
    'Bearer realm="example",',
    'error="invalid_token",',
    'error_description="The access token expired"'
  )
  out <- parse_www_authenticate(header)
  expect_equal(out$scheme, "Bearer")
  expect_equal(out$realm, "example")
  expect_equal(out$error_description, "The access token expired")
})

test_that("can parse links", {
  header <- paste0(
    '<https://example.com/1>; rel="next",',
    '<https://example.com/2>; rel="last"'
  )
  expect_equal(
    parse_link(header),
    list(
      list(url = "https://example.com/1", rel = "next"),
      list(url = "https://example.com/2", rel = "last")
    )
  )
})

# Helpers -----------------------------------------------------------------

test_that("parse_in_half handles common cases", {
  parsed <- parse_in_half(c("a=b", "c=d", "e", "=f", "g=", "h=i=j"), "=")
  expect_equal(parsed$left, c("a", "c", "e", "", "g", "h"))
  expect_equal(parsed$right, c("b", "d", "", "f", "", "i=j"))
})

test_that("parse_in_half handles problematic inputs", {
  expect_equal(
    parse_in_half(character(0), "="),
    list(left = character(0), right = character(0))
  )
  expect_equal(
    parse_in_half("", "="),
    list(left = "", right = "")
  )
  expect_equal(
    parse_in_half(NA, "="),
    list(left = NA_character_, right = NA_character_)
  )
})

test_that("parse_in_half always returns two pieces", {
  expect_equal(parse_in_half("a", " "), list(left = "a", right = ""))
  expect_equal(parse_in_half("a b", " "), list(left = "a", right = "b"))
  expect_equal(parse_in_half("a b c", " "), list(left = "a", right = "b c"))
})

test_that("parse_name_equals_value handles empty values", {
  expect_equal(parse_name_equals_value("a"), c(a = ""))
})

test_that("parse_match converts missing matches to NULL", {
  expect_equal(
    parse_match("abbbd", "(a)(b+)(c*)(d)"),
    list("a", "bbb", NULL, "d"
  ))
})