File: test-curl.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 (231 lines) | stat: -rw-r--r-- 6,995 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
test_that("must be call to curl", {
  expect_snapshot(error = TRUE, curl_translate("echo foo"))
})

test_that("must have cmd argument if non-interactive", {
  expect_snapshot(error = TRUE, curl_translate())
})

test_that("captures key components of call", {
  expect_equal(curl_args("curl http://x.com"), list(`<url>` = "http://x.com"))

  # Quotes are stripped
  expect_equal(curl_args("curl 'http://x.com'"), list(`<url>` = "http://x.com"))
  expect_equal(curl_args('curl "http://x.com"'), list(`<url>` = "http://x.com"))

  # Url can come before or after arguments
  expect_equal(
    curl_args("curl -H 'A: 1' 'http://example.com'"),
    curl_args("curl 'http://example.com' -H 'A: 1'")
  )
  # long name and short name are equivalent
  expect_equal(
    curl_args("curl 'http://example.com' --header 'A: 1'"),
    curl_args("curl 'http://example.com' -H 'A: 1'")
  )

  # can repeat args
  expect_equal(
    curl_args("curl 'http://example.com' -H 'A: 1' -H 'B: 2'")$`--header`,
    c("A: 1", "B: 2")
  )

  # Captures flags
  expect_equal(curl_args("curl 'http://example.com' --verbose")$`--verbose`, TRUE)
})

test_that("can accept multiple data arguments", {
  expect_equal(
    curl_args("curl https://example.com -d x=1 -d y=2")$`--data`,
    c("x=1", "y=2")
  )
})

test_that("can handle line breaks", {
  expect_equal(
    curl_args("curl 'http://example.com' \\\n -H 'A: 1' \\\n -H 'B: 2'")$`--header`,
    c("A: 1", "B: 2")
  )
})

test_that("headers are parsed", {
  expect_equal(
    curl_normalize("curl http://x.com -H 'A: 1'")$headers,
    new_headers(list(A = "1"))
  )
  expect_equal(
    curl_normalize("curl http://x.com -H 'B:'")$headers,
    new_headers(list(B = ""))
  )
})

test_that("user-agent and referer become headers", {
  expect_equal(
    curl_normalize("curl http://x.com -A test")$headers,
    as_headers(list("user-agent" = "test"))
  )

  expect_equal(
    curl_normalize("curl http://x.com -e test")$headers,
    as_headers(list("referer" = "test"))
  )
})

test_that("common headers can be removed", {
  sec_fetch_headers <- "-H 'Sec-Fetch-Dest: empty' -H 'Sec-Fetch-Mode: cors'"
  sec_ch_ua_headers <- "-H 'sec-ch-ua-mobile: ?0'"
  other_headers <- "-H 'Accept: application/vnd.api+json'"
  cmd <- paste("curl http://x.com -A agent -e ref", sec_fetch_headers, sec_ch_ua_headers, other_headers)
  headers <- curl_normalize(cmd)$headers
  expect_snapshot({
    print(curl_simplify_headers(headers, simplify_headers = TRUE))
    print(curl_simplify_headers(headers, simplify_headers = FALSE))
  })
})

test_that("extract user name and password", {
  expect_equal(
    curl_normalize("curl http://x.com -u name:pass")$auth,
    list(username = "name", password = "pass")
  )
  expect_equal(
    curl_normalize("curl http://x.com -u name")$auth,
    list(username = "name", password = "")
  )
})

test_that("can override default method", {
  expect_equal(curl_normalize("curl http://x.com")$method, NULL)
  expect_equal(curl_normalize("curl http://x.com --get")$method, "GET")
  expect_equal(curl_normalize("curl http://x.com --head")$method, "HEAD")
  expect_equal(curl_normalize("curl http://x.com -X PUT")$method, "PUT")
})

test_that("prefers explicit url", {
  expect_equal(curl_normalize("curl 'http://x.com'")$url, "http://x.com")
  expect_equal(curl_normalize("curl --url 'http://x.com'")$url, "http://x.com")

  # prefers explicit
  expect_equal(
    curl_normalize("curl 'http://x.com' --url 'http://y.com'")$url,
    "http://y.com"
  )
})

test_that("can translate to httr calls", {
  skip_if(getRversion() < "4.1")

  expect_snapshot({
    curl_translate("curl http://x.com")
    curl_translate("curl http://x.com -X DELETE")
    curl_translate("curl http://x.com -H A:1")
    curl_translate("curl http://x.com -H 'A B:1'")
    curl_translate("curl http://x.com -u u:p")
    curl_translate("curl http://x.com --verbose")
  })
})

test_that("can translate query", {
  skip_if(getRversion() < "4.1")

  expect_snapshot({
    curl_translate("curl http://x.com?string=abcde&b=2")
  })
})

test_that("can translate data", {
  skip_if(getRversion() < "4.1")

  expect_snapshot({
    curl_translate("curl http://example.com --data abcdef")
    curl_translate("curl http://example.com --data abcdef -H Content-Type:text/plain")
  })
})

test_that("can translate ocokies", {
  skip_if(getRversion() < "4.1")

  expect_snapshot({
    curl_translate("curl 'http://test' -H 'Cookie: x=1; y=2;z=3'")
  })
})

test_that("can translate json", {
  skip_if(getRversion() < "4.1")

  expect_snapshot({
    curl_translate(r"--{curl http://example.com --data-raw '{"a": 1, "b": "text"}' -H Content-Type:application/json}--")
    curl_translate(r"--{curl http://example.com --json '{"a": 1, "b": "text"}'}--")
  })
})

test_that("content type stays in header if no data", {
  skip_if(getRversion() < "4.1")

  expect_snapshot(
    curl_translate("curl http://example.com -H Content-Type:text/plain")
  )
})

test_that("can evaluate simple calls", {
  request_test() # hack to start server

  resp <- curl_translate_eval(glue("curl {the$test_app$url()}/get -H A:1"))
  body <- resp_body_json(resp)
  expect_equal(body$headers$A, "1")

  resp <- curl_translate_eval(glue("curl {the$test_app$url()}/post --data A=1"))
  body <- resp_body_json(resp)
  expect_equal(body$form$A, "1")

  resp <- curl_translate_eval(glue("curl {the$test_app$url()}/delete -X delete"))
  body <- resp_body_json(resp)
  expect_equal(body$method, "delete")

  resp <- curl_translate_eval(glue("curl {the$test_app$url()}//basic-auth/u/p -u u:p"))
  body <- resp_body_json(resp)
  expect_true(body$authenticated)
})

test_that("can read from clipboard", {
  skip_on_cran()
  skip_if_not_installed("clipr")
  skip_if(getRversion() < "4.1")

  # pretend we're interactive and can use the clipboard
  withr::local_envvar(CLIPR_ALLOW = TRUE)
  rlang::local_interactive()
  skip_if_not(clipr::clipr_available())

  # restore the existing clipboard to be nice to the tester
  old_clip <- suppressWarnings(clipr::read_clip())
  if (!is.null(old_clip)) {
    withr::defer(clipr::write_clip(old_clip))
  }

  clipr::write_clip("curl 'http://example.com' \\\n -H 'A: 1' \\\n -H 'B: 2'")
  expect_snapshot({
    curl_translate()
    # also writes to clip
    writeLines(clipr::read_clip())
  })
})

test_that("encode_string2() produces simple strings", {
  # double quotes is standard
  expect_equal(encode_string2("x"), encodeString("x", quote = '"'))
  # use single quotes if double quotes but not single quotes
  expect_equal(encode_string2('x"x'), encodeString('x"x', quote = "'"))

  skip_if_not(getRversion() >= "4.0.0")
  # use raw string if single and double quotes are used
  expect_equal(encode_string2('x"\'x'), 'r"---{x"\'x}---"')

  skip_if(getRversion() < "4.1")
  cmd <- paste0("curl 'http://example.com' \
  -X 'PATCH' \
  -H 'Content-Type: application/json' \
  --data-raw ", '{"data":{"x":1,"y":"a","nested":{"z":[1,2,3]}}}', "\
  --compressed")
  expect_snapshot(curl_translate(cmd))
})