File: test-content-type.R

package info (click to toggle)
r-cran-crul 1.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,620 kB
  • sloc: sh: 13; makefile: 2
file content (99 lines) | stat: -rw-r--r-- 3,233 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
skip_on_cran()

res_html <- HttpClient$new(url = hb())$get()
res_json <- HttpClient$new(url = hb("/json"))$get()
res_xml <- HttpClient$new(url = hb("/xml"))$get()

test_that("html", {
  ## get the content type
  expect_match(res_html$response_headers$`content-type`,
    "text/html; charset=utf-8")

  ## check that the content type is text/html
  expect_null(res_html$raise_for_ct_html())

  ## it's def. not json or xml
  expect_error(res_html$raise_for_ct_json(), "did not match")
  expect_error(res_html$raise_for_ct_xml(), "did not match")
  ### behavior: warning
  expect_warning(res_html$raise_for_ct_json(behavior = "warning"),
    "did not match")

  ## give custom content type
  expect_null(res_html$raise_for_ct("text/html"))
  expect_error(res_html$raise_for_ct("application/json"),
    "did not match")
  ### behavior: warning
  expect_warning(res_html$raise_for_ct("application/json", behavior = "warning"),
    "did not match")
  expect_error(res_html$raise_for_ct("foo/bar"),
    "type not in allowed set")
})

test_that("json", {
  ## get the content type
  expect_match(res_json$response_headers$`content-type`,
    "application/json")

  ## check that the content type is text/html
  expect_null(res_json$raise_for_ct_json())

  ## it's def. not xml
  expect_error(res_json$raise_for_ct_xml(), "did not match")
  ### behavior: warning
  expect_warning(res_json$raise_for_ct_xml(behavior = "warning"),
    "did not match")

  ## give custom content type
  expect_null(res_json$raise_for_ct("application/json"))
  expect_error(res_json$raise_for_ct("application/xml"),
    "did not match")
  ### behavior: warning
  expect_warning(res_json$raise_for_ct("application/xml", behavior = "warning"),
    "did not match")
})

test_that("xml", {
  ## get the content type
  expect_match(res_xml$response_headers$`content-type`, "application/xml")

  ## check that the content type is text/html
  expect_null(res_xml$raise_for_ct_xml())

  ## it's def. not json
  expect_error(res_xml$raise_for_ct_json(), "did not match")
  ### behavior: warning
  expect_warning(res_xml$raise_for_ct_json(behavior = "warning"),
    "did not match")

  ## give custom content type
  expect_null(res_xml$raise_for_ct("application/xml"))
  expect_error(res_xml$raise_for_ct("application/json"),
    "did not match")
  ### behavior: warning
  expect_warning(res_xml$raise_for_ct("application/json", behavior = "warning"),
    "did not match")
  expect_error(res_xml$raise_for_ct("foo/bar"),
    "type not in allowed set")
})


test_that("charset works", {
  ## check charset in addition to the media type

  ### warning thrown that no charset detected - don't want to fail if no charset given by server
  expect_warning(res_json$raise_for_ct_json(charset = "utf-8"),
    "no charset detected")

  ### charset should be given with text/html response
  #### nothing happens if a match
  expect_null(res_html$raise_for_ct_html(charset = "utf-8"))
  #### error if not matched
  expect_error(res_html$raise_for_ct_html(charset = "utf-16"),
    "did not match")

  ### w/ custom type
  expect_null(res_html$raise_for_ct("text/html", charset = "utf-8"))
  expect_error(res_html$raise_for_ct("text/html", charset = "utf-16"),
    "did not match")
})