Debian Codegraph @

tests/testthat/test-post.R

raw
1 h <- new_handle()
2
3 test_that("Post text data", {
4 handle_setopt(h, COPYPOSTFIELDS = "moo=moomooo");
5 handle_setheaders(h,
6 "Content-Type" = "text/moo",
7 "Cache-Control" = "no-cache",
8 "User-Agent" = "A cow"
9 )
10 req <- curl_fetch_memory(httpbin("post"), handle = h)
11 res <- jsonlite::fromJSON(rawToChar(req$content))
12
13 expect_equal(res$data, "moo=moomooo")
14 expect_equal(res$headers$`Content-Type`, "text/moo")
15 expect_equal(res$headers$`User-Agent`, "A cow")
16
17 # Using connection interface
18 input <- jsonlite::fromJSON(rawToChar(req$content))
19 con <- curl(httpbin("post"), handle = h)
20 output <- jsonlite::fromJSON(con)
21 expect_equal(input, output)
22
23 # Using download interface
24 tmp <- tempfile()
25 on.exit(unlink(tmp), add = TRUE)
26 curl_download(httpbin("post"), tmp, handle = h)
27 txt2 <- readLines(tmp)
28 expect_equal(rawToChar(req$content), paste0(txt2, "\n", collapse=""))
29 })
30
31 test_that("Change headers", {
32 # Default to application/url-encoded
33 handle_setheaders(h, "User-Agent" = "Not a cow")
34 req <- curl_fetch_memory(httpbin("post"), handle = h)
35 res <- jsonlite::fromJSON(rawToChar(req$content))
36 expect_equal(res$form$moo, "moomooo")
37 expect_equal(res$headers$`User-Agent`, "Not a cow")
38
39 })
40
41 test_that("Post JSON data", {
42 hx <- new_handle()
43 handle_setopt(hx, COPYPOSTFIELDS = jsonlite::toJSON(mtcars));
44 handle_setheaders(hx, "Content-Type" = "application/json")
45 req <- curl_fetch_memory(httpbin("post"), handle = hx)
46 expect_equal(req$status_code, 200)
47
48 # For debugging
49 if(req$status_code > 200)
50 stop(rawToChar(req$content))
51
52 # Note that httpbin reoders columns alphabetically
53 output <- jsonlite::fromJSON(rawToChar(req$content))
54 expect_is(output$json, "data.frame")
55 expect_equal(sort(names(output$json)), sort(names(mtcars)))
56 })
57
58 test_that("Multipart form post", {
59 # Don't reset options manually, curl should figure this out.
60 hx <- handle_setform(new_handle(),
61 foo = "blabla",
62 bar = charToRaw("boeboe"),
63 iris = form_data(serialize(iris, NULL), "data/rda"),
64 description = form_file(system.file("DESCRIPTION")),
65 logo = form_file(file.path(Sys.getenv("R_DOC_DIR"), "html/logo.jpg"), "image/jpeg")
66 )
67 req <- curl_fetch_memory(httpbin("post"), handle = hx)
68
69 # For debugging
70 expect_equal(req$status_code, 200)
71 if(req$status_code > 200)
72 stop(rawToChar(req$content))
73
74 res <- jsonlite::fromJSON(rawToChar(req$content))
75 expect_match(res$headers$`Content-Type`, "multipart")
76 expect_equal(sort(names(res$files)), c("description", "logo"))
77 expect_equal(sort(names(res$form)), c("bar", "foo", "iris"))
78 })
79
80 test_that("Empty values", {
81 hx <- handle_setform(new_handle())
82 req <- curl_fetch_memory(httpbin("post"), handle = hx)
83 expect_equal(req$status_code, 200)
84 res <- jsonlite::fromJSON(rawToChar(req$content))
85 expect_length(res$form, 0)
86 expect_equal(as.numeric(res$headers$`Content-Length`), 0)
87
88 hx <- handle_setform(new_handle(), x = "", y = raw(0))
89 req <- curl_fetch_memory(httpbin("post"), handle = hx)
90
91 # For debugging
92 expect_equal(req$status_code, 200)
93 if(req$status_code > 200)
94 stop(rawToChar(req$content))
95
96 res <- jsonlite::fromJSON(rawToChar(req$content))
97 expect_match(res$headers$`Content-Type`, "multipart")
98 expect_length(res$form, 2)
99 expect_equal(res$form$x, "")
100 expect_equal(res$form$y, "")
101 })
102
103 rm(h)
104 test_that("GC works", {
105 gc()
106 expect_equal(total_handles(), 0L)
107 })
108