-
Notifications
You must be signed in to change notification settings - Fork 51
/
test-req-body.R
82 lines (66 loc) · 2.35 KB
/
test-req-body.R
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
test_that("can send file", {
skip_on_os("windows") # fails due to line ending difference
path <- tempfile()
writeLines("this is a test", path)
resp <- request_test("/post") %>%
req_body_file(path, type = "text/plain") %>%
req_perform()
json <- resp_body_json(resp)
expect_equal(json$headers$`Content-Type`, "text/plain")
expect_equal(json$data, "this is a test\n")
})
test_that("can send string", {
resp <- request_httpbin("/post") %>%
req_body_raw("test") %>%
req_perform()
json <- resp_body_json(resp)
expect_equal(json$headers$`Content-Type`, NULL)
expect_equal(json$data, "test")
})
test_that("can send named list as json/form/multipart", {
data <- list(a = "1", b = "2")
resp <- request_test("/post") %>%
req_body_json(data) %>%
req_perform()
json <- resp_body_json(resp)
expect_equal(json$json, data)
resp <- request_test("/post") %>%
req_body_form(data) %>%
req_perform()
json <- resp_body_json(resp)
expect_equal(json$headers$`Content-Type`, "application/x-www-form-urlencoded")
expect_equal(json$form, data)
resp <- request_test("/post") %>%
req_body_multipart(data) %>%
req_perform()
json <- resp_body_json(resp)
expect_match(json$headers$`Content-Type`, "multipart/form-data; boundary=-")
expect_equal(json$form, list(a = "1", b = "2"))
})
test_that("can modify body data", {
req1 <- request_test() %>% req_body_form(list(a = 1))
expect_equal(req1$body$data, list(a = 1))
req2 <- req1 %>% req_body_form(list(b = 2))
expect_equal(req2$body$data, list(a = 1, b = 2))
req3 <- req1 %>% req_body_form(list(a = 3, a = 4))
expect_equal(req3$body$data, list(a = 3, a = 4))
})
test_that("can upload file with multipart", {
skip_on_os("windows") # fails due to line ending difference
path <- tempfile()
writeLines("this is a test", path)
resp <- request_httpbin("/post") %>%
req_body_multipart(list(file = curl::form_file(path))) %>%
req_perform()
json <- resp_body_json(resp)
expect_match(json$files$file, "this is a test\n")
})
test_that("can override body content type", {
req <- request_test("/post") %>%
req_body_raw('{"x":"y"}') %>%
req_headers("content-type" = "application/json")
resp <- req_perform(req)
headers <- resp_body_json(resp)$headers
expect_equal(headers$`Content-Type`, "application/json")
expect_equal(headers$`content-type`, NULL)
})