Debian Codegraph @

tests/testthat/test-cookies.R

raw
1 h <- new_handle()
2
3 test_that("No cookies", {
4 cookies <- handle_cookies(h);
5 expect_is(cookies, "data.frame")
6 expect_equal(nrow(cookies), 0)
7 })
8
9 test_that("Add some cookies", {
10 req <- curl_fetch_memory(httpbin("cookies/set?foo=123&bar=ftw"), handle = h)
11 cookies <- handle_cookies(h);
12 expect_is(cookies, "data.frame")
13 expect_equal(nrow(cookies), 2)
14 expect_equal(sort(cookies$name), c("bar", "foo"))
15 expect_equal(sort(cookies$value), c("123","ftw"))
16 expect_true(all(cookies$expiration == Inf))
17 })
18
19 test_that("Coockie with connection", {
20 con <- curl(httpbin("cookies"), handle = h)
21 expect_equal(jsonlite::fromJSON(con)$cookies$foo, "123")
22 })
23
24 test_that("Delete a cookie", {
25 req <- curl_fetch_memory(httpbin("cookies/delete?foo"), handle = h)
26 cookies <- handle_cookies(h)
27 foo <- subset(cookies, name == "foo")
28 bar <- subset(cookies, name == "bar")
29 expect_true(bar$expiration > Sys.time())
30 expect_equal(bar$value, "ftw")
31
32 # Recent versions of curl immediately drop the cookie
33 if(nrow(foo)){
34 expect_true(foo$expiration < Sys.time())
35 expect_true(is.na(foo$value))
36 }
37 })
38
39 test_that("Overwrite a cookie", {
40 req <- curl_fetch_memory(httpbin("cookies/set?foo=888&bar=999"), handle = h)
41 cookies <- handle_cookies(h)
42 foo <- subset(cookies, name == "foo")
43 bar <- subset(cookies, name == "bar")
44 expect_equal(foo$value, "888")
45 expect_equal(bar$value, "999")
46 expect_true(all(cookies$expiration == Inf))
47 })
48
49 rm(h)
50 test_that("GC works", {
51 gc()
52 expect_equal(total_handles(), 0L)
53 })