File: test-set.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 (163 lines) | stat: -rw-r--r-- 4,260 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
context("set curl options: crul_settings")
test_that("crul_settings structure", {
  skip_on_cran()

  expect_is(crul_settings, "function")

  aa <- crul_settings()
  
  expect_is(aa, "ls_str")
  expect_null(names(aa))
  expect_equal(as.list(aa)[[1]], "mock")
  expect_equal(crul_settings(TRUE), crul_settings(FALSE))
})

#reset
crul_settings(reset = TRUE)

context("set curl options: set_opts")
test_that("set_opts works", {
  skip_on_cran()

  expect_is(set_opts, "function")
  
  # without any curl options set
  aa <- crul_settings()
  expect_length(aa, 1)
  expect_equal(as.list(aa)[[1]], "mock")

  # with empty curl options set
  set_opts()
  aa <- crul_settings()
  expect_length(aa, 2)
  expect_equal(unlist(as.list(aa)), c("mock", "opts"))

  # a single setting: timeout_ms
  set_opts(timeout_ms = 1000)
  aa <- crul_settings()
  expect_length(aa, 2)
  expect_equal(unlist(as.list(aa)), c("mock", "opts"))
  expect_named(get("opts", envir = crul_opts), 'timeout_ms')
  expect_equal(get("opts", envir = crul_opts)$timeout_ms, 1000)
  
  # resetting a previously set option
  set_opts(timeout_ms = 4000)
  aa <- crul_settings()
  expect_length(aa, 2)
  expect_equal(unlist(as.list(aa)), c("mock", "opts"))
  expect_named(get("opts", envir = crul_opts), 'timeout_ms')
  expect_equal(get("opts", envir = crul_opts)$timeout_ms, 4000)

  # verbose
  set_opts(verbose = TRUE)
  aa <- crul_settings()
  expect_length(aa, 2)
  expect_equal(unlist(as.list(aa)), c("mock", "opts"))
  expect_named(get("opts", envir = crul_opts), 
    c('timeout_ms', 'verbose'))
  expect_equal(get("opts", envir = crul_opts)$timeout_ms, 4000)
  expect_true(get("opts", envir = crul_opts)$verbose)
})
#reset
crul_settings(reset = TRUE)

test_that("set_opts in a http request", { 
  set_opts(timeout_ms = 1)
  expect_error(
    HttpClient$new(hb())$get('get')
  )
})


context("set curl options: fails well")
test_that("fails well", {
  skip_on_cran()

  expect_error(set_auth(), "argument \"x\" is missing")
  expect_error(set_proxy(), "argument \"x\" is missing")
})

#reset
crul_settings(reset = TRUE)

context("set curl options: set_auth")
test_that("set_auth works", {
  expect_is(set_auth, "function")

  # without it set
  aa <- crul_settings()
  expect_length(aa, 1)
  expect_equal(as.list(aa)[[1]], "mock")

  # set it
  set_auth(auth(user = "foo", pwd = "bar", auth = "basic"))

  # after set_auth set
  aa <- crul_settings()
  expect_length(aa, 2)
  expect_equal(unlist(as.list(aa)), c("crul_auth", "mock"))
  expect_named(get("crul_auth", envir = crul_opts), c("userpwd", "httpauth"))
  expect_equal(get("crul_auth", envir = crul_opts)$userpwd, "foo:bar")
  expect_equal(get("crul_auth", envir = crul_opts)$httpauth, 1)
})
#reset
crul_settings(reset = TRUE)



context("set curl options: set_headers")
test_that("set_headers works", {
  expect_is(set_headers, "function")

  # without it set
  aa <- crul_settings()
  expect_length(aa, 1)
  expect_equal(as.list(aa)[[1]], "mock")

  # set it
  set_headers(foo = "bar")
  set_headers(`User-Agent` = "hello world")

  # after set_auth set
  aa <- crul_settings()
  expect_length(aa, 2)
  expect_equal(unlist(as.list(aa)), c("headers", "mock"))
  expect_named(get("headers", envir = crul_opts), c("foo", "User-Agent"))
  expect_equal(get("headers", envir = crul_opts)$foo, "bar")
  expect_equal(get("headers", envir = crul_opts)$`User-Agent`, "hello world")
})
#reset
crul_settings(reset = TRUE)



context("set curl options: set_proxy")
test_that("set_proxy works", {
  expect_is(set_proxy, "function")

  # without it set
  aa <- crul_settings()
  expect_length(aa, 1)
  expect_equal(as.list(aa)[[1]], "mock")

  # set it
  set_proxy(proxy("http://97.77.104.22:3128"))

  # after set_auth set
  aa <- crul_settings()
  expect_length(aa, 2)
  expect_equal(unlist(as.list(aa)), c("mock", "proxies"))
  expect_is(get("proxies", envir = crul_opts), "proxy")
  expect_named(get("proxies", envir = crul_opts), c("proxy", "proxyport", "proxyauth"))
  expect_equal(get("proxies", envir = crul_opts)$proxy, "97.77.104.22")
  expect_equal(get("proxies", envir = crul_opts)$proxyport, 3128)
  expect_equal(get("proxies", envir = crul_opts)$proxyauth, 1)
})
#reset
crul_settings(reset = TRUE)




#reset
crul_settings(reset = TRUE)