File: test-url_build_parse.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 (96 lines) | stat: -rw-r--r-- 2,665 bytes parent folder | download | duplicates (3)
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
context("url build")

test_that("url build works", {
  skip_on_cran()

  aa <- url_build(hb())
  bb <- url_build(hb(), "get")
  cc <- url_build(hb(), "get", list(foo = "bar"))

  expect_is(aa, "character")
  expect_match(aa, "https")
  expect_match(aa, "httpbin.org")

  expect_is(bb, "character")
  expect_match(bb, "https")
  expect_match(bb, "httpbin.org")
  expect_match(bb, "get")

  expect_is(cc, "character")
  expect_match(cc, "https")
  expect_match(cc, "httpbin.org")
  expect_match(cc, "?foo=bar")
})

test_that("build fails well", {
  skip_on_cran()

  # url param required
  expect_error(url_build(), "argument \"url\" is missing")

  # wrong types
  expect_error(url_build(5), "url must be of class character")
  expect_error(url_build("ASDf", path = 5), "path must be of class character")
  expect_error(url_build("adff", query = 5), "query must be of class list")

  # length
  expect_error(url_build(rep(hb(), 2)), 
    "length\\(url\\) == 1 is not TRUE")
  expect_error(url_build(hb(), c('foo', 'bar')), 
    "length\\(path\\) <= 1 is not TRUE")

  # query list is named
  expect_error(url_build("As", query = list(4, 5)),
               "all query elements must be named")
})


context("url parse")

test_that("url parse works", {
  skip_on_cran()

  aa <- url_parse(hb())
  bb <- url_parse(hb("/get?foo=bar"))
  cc <- url_parse(hb("/get?foo=bar&stuff=things"))

  expect_is(aa, "list")
  expect_named(aa, c('scheme', 'domain', 'port', 'path', 'parameter',
                     'fragment'))
  expect_is(aa$scheme, "character")
  expect_equal(aa$scheme, "https")
  expect_is(aa$domain, "character")
  expect_true(is.na(aa$path))
  expect_true(is.na(aa$parameter))

  expect_is(bb, "list")
  expect_named(bb, c('scheme', 'domain', 'port', 'path', 'parameter',
                     'fragment'))
  expect_is(bb$scheme, "character")
  expect_equal(bb$scheme, "https")
  expect_is(bb$domain, "character")
  expect_equal(bb$path, "get")
  expect_is(bb$parameter, "list")
  expect_equal(bb$parameter$foo, "bar")

  expect_is(cc, "list")
  expect_named(cc, c('scheme', 'domain', 'port', 'path', 'parameter',
                     'fragment'))
  expect_is(cc$scheme, "character")
  expect_equal(cc$scheme, "https")
  expect_is(cc$domain, "character")
  expect_equal(cc$path, "get")
  expect_is(cc$parameter, "list")
  expect_equal(cc$parameter$foo, "bar")
  expect_equal(cc$parameter$stuff, "things")
})

test_that("build and parse fails well", {
  skip_on_cran()

  # url param required
  expect_error(url_parse(), "argument \"url\" is missing")

  # scalar character required
  expect_error(url_parse(rep(hb(), 2)), "length\\(url\\) == 1 is not TRUE")
})