File: test-content-type.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 (78 lines) | stat: -rw-r--r-- 2,294 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
test_that("valid arguments are optional", {
  resp <- response_json()
  expect_no_error(resp_check_content_type(resp))
})

test_that("can check type of response", {
  resp1 <- response(headers = c("Content-type: application/json"))
  resp2 <- response(headers = c("Content-type: xxxxx"))

  expect_no_error(
    resp_check_content_type(resp1, "application/json")
  )
  expect_no_error(
    resp_check_content_type(resp1, "application/xml", check_type = FALSE)
  )
  expect_snapshot(error = TRUE, {
    resp_check_content_type(resp1, "application/xml")
    resp_check_content_type(resp2, "application/xml")
  })
})

test_that("useful error even if no content type", {
  resp <- response()
  expect_snapshot(resp_check_content_type(resp, "application/xml"), error = TRUE)
})

test_that("can parse content type", {
  expect_equal(
    parse_content_type("application/json"),
    list(type = "application", subtype = "json", suffix = "")
  )

  # can parse suffix
  expect_equal(
    parse_content_type("text/html+xml"),
    list(type = "text", subtype = "html", suffix = "xml")
  )

  # parameters don't matter
  expect_equal(
    parse_content_type("text/html+xml;charset=UTF-8"),
    list(type = "text", subtype = "html", suffix = "xml")
  )
})

test_that("invalid type returns empty strings", {
  expect_equal(
    parse_content_type(""),
    list(type = "", subtype = "", suffix = "")
  )
})

test_that("check_content_type() can consult suffixes", {
  expect_no_error(check_content_type("application/json", "application/json"))
  expect_snapshot(check_content_type("application/json", "application/xml"), error = TRUE)

  # works with suffixes
  expect_no_error(check_content_type("application/test+json", "application/json", "json"))
  expect_snapshot(
    check_content_type("application/test+json", "application/xml", "xml"),
    error = TRUE
  )

  # can use multiple valid types
  expect_no_error(
    check_content_type("application/test+json", c("text/html", "application/json"), "json")
  )
  expect_snapshot(
    check_content_type("application/xml", c("text/html", "application/json")),
    error = TRUE
  )
})

test_that("can detect text types", {
  expect_true(is_text_type("text/html"))
  expect_true(is_text_type("application/json"))
  expect_false(is_text_type("image/png"))
})