Debian Codegraph @

tests/testthat/test-handle.R

raw
1 h <- new_handle()
2
3 test_that("Perform", {
4 expect_equal(curl_fetch_memory(httpbin("get"), handle = h)$status, 200)
5 expect_equal(curl_fetch_memory(httpbin("cookies"), handle = h)$status, 200)
6 expect_equal(curl_fetch_memory(httpbin("status/418"), handle = h)$status, 418)
7 })
8
9 test_that("Redirect", {
10 expect_equal(curl_fetch_memory(httpbin("redirect/6"), handle = h)$status, 200)
11 expect_equal(curl_fetch_memory(httpbin("relative-redirect/6"), handle = h)$status, 200)
12 expect_equal(curl_fetch_memory(httpbin("absolute-redirect/6"), handle = h)$status, 200)
13 })
14
15 test_that("Cookies", {
16 expect_equal(curl_fetch_memory(httpbin("cookies/set?foo=123&bar=456"), handle = h)$status, 200)
17 expect_equal(jsonlite::fromJSON(rawToChar(curl_fetch_memory(httpbin("cookies"), handle = h)$content))$cookies$bar, "456")
18 expect_equal(curl_fetch_memory(httpbin("cookies/delete?bar"), handle = h)$status, 200)
19 if(curl::curl_version()$version == "7.62.0") Sys.sleep(1) #workaround for curl bug #3351
20 expect_null(jsonlite::fromJSON(rawToChar(curl_fetch_memory(httpbin("cookies"), handle = h)$content))$cookies$bar)
21 })
22
23 test_that("Keep-Alive", {
24 con <- rawConnection(raw(), "wb")
25 handle_setopt(h, verbose=TRUE, debugfunction = function(type, msg){
26 if(type < 3) writeBin(msg, con)
27 })
28 req <- curl_fetch_memory(httpbin("get"), handle = h)
29 logs <- rawToChar(rawConnectionValue(con))
30 close(con)
31 expect_true(grepl("existing", logs))
32 handle_setopt(h, verbose=FALSE)
33 })
34
35 test_that("Opening and closing a connection",{
36 # Create connection
37 con <- curl(httpbin("cookies"), handle = h)
38
39 # Handle is still usable
40 expect_equal(curl_fetch_memory(httpbin("get"), handle = h)$status, 200)
41
42 # Opening the connection locks the handle
43 open(con)
44
45 # Recent versions of libcurl will raise an error
46 #if(compareVersion(curl_version()$version, "7.37") > 0){
47 # expect_error(curl_fetch_memory(httpbin("get", handle = h))
48 #}
49
50 expect_equal(jsonlite::fromJSON(readLines(con))$cookies$foo, "123")
51
52 # After closing it is free again
53 close(con)
54 expect_equal(curl_fetch_memory(httpbin("get"), handle = h)$status, 200)
55
56 # Removing the connection also unlocks the handle
57 con <- curl(httpbin("cookies"), "rb", handle = h)
58
59 # Recent versions of libcurl will raise an error
60 #if(compareVersion(curl_version()$version, "7.37") > 0){
61 # expect_error(curl_fetch_memory(httpbin("get", handle = h))
62 #}
63 close(con)
64 rm(con)
65 expect_equal(curl_fetch_memory(httpbin("get"), handle = h)$status, 200)
66 })
67
68 test_that("Downloading to a file", {
69 tmp <- tempfile()
70 expect_error(curl_download(httpbin("status/418"), tmp, handle = h))
71
72 curl_download(httpbin("get?test=boeboe"), tmp, handle = h)
73 expect_equal(jsonlite::fromJSON(tmp)$args$test, "boeboe")
74
75 curl_download(httpbin("cookies"), tmp, handle = h)
76 expect_equal(jsonlite::fromJSON(tmp)$cookies$foo, "123")
77 })
78
79 test_that("handle_setopt validates options", {
80 h <- new_handle()
81 expect_identical(class(h), "curl_handle")
82 expect_error(handle_setopt(h, invalid.option="foo"),
83 "Unknown option: invalid.option")
84 expect_error(handle_setopt(h, badopt1="foo", badopt2="bar"),
85 "Unknown options: badopt1, badopt2")
86 expect_identical(class(handle_setopt(h, username="foo")),
87 "curl_handle") ## i.e. that's a valid option, so it succeeds
88 })
89
90 test_that("setting request headers", {
91 h <- new_handle()
92 expect_length(curl:::handle_getheaders(h), 0)
93 handle_setheaders(h, foo = 'bar', baz = 'bak')
94 expect_equal(curl:::handle_getheaders(h), c("foo: bar", "baz: bak", "Expect: "))
95 handle_setheaders(h, foo = '123')
96 expect_equal(curl:::handle_getheaders(h), c("foo: 123", "Expect: "))
97 handle_setopt(h, httpheader = c("test: blabla", "foobar: 123"))
98 expect_equal(curl:::handle_getheaders(h), c("test: blabla", "foobar: 123"))
99 handle_setheaders(h)
100 expect_equal(curl:::handle_getheaders(h), c("Expect: "))
101 handle_reset(h)
102 expect_length(curl:::handle_getheaders(h), 0)
103 })
104
105
106 test_that("Set blank and NULL headers", {
107 skip_if_not_installed('httpuv')
108 skip_if_not_installed('webutils')
109 h <- new_handle(url = 'https://httpbin.org/get')
110 handle_setheaders(h, "accept-encoding" = "", "accept" = "", "user-agent" = "", foo = " ", bar = "\t")
111 req <- curl::curl_echo(h)
112 expect_equal(req$headers, c(bar = "", foo = "", host = "httpbin.org"))
113 })
114
115 test_that("Custom vector options", {
116 h <- new_handle()
117 skip_if_not('smtp' %in% curl_version()$protocols)
118 x <- c("[email protected]", "[email protected]")
119 handle_setopt(h, mail_rcpt = x)
120 expect_equal(curl:::handle_getcustom(h), x)
121
122 # This leaks a bit
123 x <- c(x, "[email protected]")
124 handle_setopt(h, quote = c("bla"))
125 handle_setopt(h, mail_rcpt = x)
126 expect_equal(curl:::handle_getcustom(h), x)
127
128 # Test free'ing
129 handle_setopt(h, quote = NULL)
130 handle_setopt(h, mail_rcpt = NULL)
131 handle_reset(h)
132 handle_setopt(h, quote = c("bla"))
133 })
134
135 test_that("Error classes", {
136 expect_error(new_handle(port = 99999), 'port', class = 'curl_error_bad_function_argument')
137 expect_error(new_handle(http_version = 99999), 'http_version', class = 'curl_error_bad_function_argument')
138 expect_error(curl_fetch_memory('https://asdfsdfsafdsfsdafsafssdfdsf.bla'), 'resolve', class = 'curl_error_couldnt_resolve_host')
139 expect_error(curl_fetch_memory('https://asdfsdfsafdsfsdafsafssdfdsf.bla'), 'resolve', class = 'curl_error_couldnt_resolve_host')
140
141 # NB: there is a bug in curl 8.7.1 (on e.g. macos) that causes failonerror
142 # to throw 'curle_recv_error' instead of 'curle_http_returned_error'
143 expect_error(curl_download('https://httpbin.org/status/418', 'output.txt'))
144 })
145
146
147 # This test was flaky with httpbin because of caching
148 test_that("Timeout error", {
149 skip_on_cran()
150 # http://www2.census.gov/acs2011_5yr/pums/csv_pus.zip has cert issues
151 expect_error(curl_fetch_memory("https://sin-speed.hetzner.com/10GB.bin", handle = new_handle(timeout = 1L)),
152 'Timeout was reached [sin-speed.hetzner.com]', class = 'curl_error_operation_timedout', fixed = TRUE)
153 })
154
155 test_that("Platform specific features", {
156 if(.Platform$OS.type == 'windows'){
157 ssl_version <- curl_version()$ssl_version
158 if(get_windows_build() < 7600 || grepl("openssl", Sys.getenv('CURL_SSL_BACKEND'), TRUE)){
159 expect_match(ssl_version, "OpenSSL.*\\(Schannel\\)")
160 } else {
161 expect_match(ssl_version, "\\(OpenSSL.*\\) Schannel")
162 }
163 } else if(!is.na(curl_options()['unix_socket_path'])){
164 # This should simply not error
165 expect_is(new_handle(UNIX_SOCKET_PATH = ""), "curl_handle")
166 }
167 })
168
169 rm(h)
170 test_that("GC works", {
171 gc()
172 expect_equal(total_handles(), 0L)
173 })