File: test-req-cache.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 (303 lines) | stat: -rw-r--r-- 9,650 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
test_that("nothing happens if cache not enabled", {
  req <- request("http://example.com")
  expect_equal(cache_pre_fetch(req), req)

  resp <- response()
  expect_equal(cache_post_fetch(req, resp), resp)
})

test_that("never retrieves POST request from cache", {
  req <- request("http://example.com") %>%
    req_method("POST") %>%
    req_cache(tempfile())

  # Fake an equivalent GET request in the cache
  resp <- response(200,
    headers = "Expires: Wed, 01 Jan 3000 00:00:00 GMT",
    body = charToRaw("abc")
  )
  cache_set(req, resp)

  expect_equal(cache_pre_fetch(req), req)
})

test_that("immutable objects retrieved directly from cache", {
  req <- request("http://example.com") %>% req_cache(tempfile())
  resp <- response(200,
    headers = "Expires: Wed, 01 Jan 3000 00:00:00 GMT",
    body = charToRaw("abc")
  )
  cache_set(req, resp)

  expect_equal(cache_pre_fetch(req), resp)
})

test_that("cached cache header added to request", {
  req <- request("http://example.com") %>% req_cache(tempfile())
  # If not cached, request returned as is
  req2 <- cache_pre_fetch(req)
  expect_equal(req2, req)

  resp <- response(200,
    headers = c('Etag: "abc"', "Last-Modified: Wed, 01 Jan 2020 00:00:00 GMT"),
    body = charToRaw("abc")
  )
  cache_set(req, resp)

  # After caching adds caching headers
  req3 <- cache_pre_fetch(req)
  expect_equal(req3$headers$`If-Modified-Since`, "Wed, 01 Jan 2020 00:00:00 GMT")
  expect_equal(req3$headers$`If-None-Match`, '"abc"')
})

test_that("error can use cached value", {
  req <- request("http://example.com") %>% req_cache(tempfile())
  resp <- response(200, body = charToRaw("OK"))
  cache_set(req, resp)

  expect_equal(cache_post_fetch(req, error_cnd()), error_cnd())

  req$policies$cache_use_on_error <- TRUE
  expect_equal(cache_post_fetch(req, error_cnd()), resp)
})

test_that("304 retains headers but gets cached body", {
  req <- request("http://example.com") %>% req_cache(tempfile())
  resp <- response(200, headers = "X: 1", body = charToRaw("OK"))
  cache_set(req, resp)

  cached <- cache_post_fetch(req, response(304, headers = "X: 2"))
  expect_equal(cached$headers$x, "2")
  expect_equal(cached$body, resp$body)

  cached <- cache_post_fetch(req, response(304, headers = "X: 3"))
  expect_equal(cached$headers$x, "3")
  expect_equal(cached$body, resp$body)
})

test_that("automatically adds to cache", {
  req <- request("http://example.com") %>% req_cache(tempfile())
  expect_true(is.null(cache_get(req)))

  resp <- response(200, headers = 'Etag: "abc"', body = charToRaw("OK"))
  cached <- cache_post_fetch(req, resp)
  expect_false(is.null(cache_get(req)))
  expect_equal(cache_get(req), resp)
})

test_that("cache emits useful debugging info", {
  req <- request("http://example.com") %>% req_cache(tempfile(), debug = TRUE)
  resp <- response(200,
    headers = "Expires: Wed, 01 Jan 3000 00:00:00 GMT",
    body = charToRaw("abc")
  )

  expect_snapshot({
    "Immutable"
    invisible(cache_pre_fetch(req))
    invisible(cache_post_fetch(req, resp))
    invisible(cache_pre_fetch(req))
  })

  req <- request("http://example.com") %>%
    req_cache(tempfile(), debug = TRUE, use_on_error = TRUE)
  resp <- response(200, headers = "X: 1", body = charToRaw("OK"))
  cache_set(req, resp)
  expect_snapshot({
    "freshness check"
    invisible(cache_pre_fetch(req))
    invisible(cache_post_fetch(req, response(304)))
    invisible(cache_post_fetch(req, error_cnd()))
  })
})

# cache -------------------------------------------------------------------

test_that("can get and set from cache", {
  req <- request("http://example.com") %>% req_cache(tempfile())
  resp <- response(
    200,
    headers = list(
      Etag = "ABC",
      `content-type` = "application/json",
      other = "header"
    ),
    body = charToRaw(jsonlite::toJSON(list(a = jsonlite::unbox(1))))
  )

  cached_resp <- response(
    304,
    headers = list(
      Etag = "DEF",
      other = "new"
    )
  )

  expect_true(is.null(cache_get(req)))
  cache_set(req, resp)
  expect_false(is.null(cache_get(req)))

  resp_from_cache <- cache_get(req)
  expect_equal(resp_from_cache, resp)

  # Uses new headers if available, otherwise cached headers
  out_headers <- cache_headers(resp_from_cache, cached_resp)
  expect_equal(out_headers$`content-type`, "application/json")
  expect_equal(out_headers$Etag, "DEF")
  expect_equal(out_headers$other, "new")

  # If path is null can leave resp as is
  expect_equal(cache_body(resp_from_cache, NULL), resp$body)
  expect_equal(resp_body_json(resp_from_cache), list(a = 1L))
  # If path is set, need to save to path
  path <- tempfile()
  body <- cache_body(resp_from_cache, path)
  expect_equal(body, new_path(path))
  expect_equal(readLines(path, warn = FALSE), rawToChar(resp$body))
})

test_that("handles responses with files", {
  req <- request("http://example.com") %>% req_cache(tempfile())

  path <- local_write_lines("Hi there")
  resp <- response(200, headers = "Etag: ABC", body = new_path(path))
  cache_set(req, resp)

  # File should be copied in cache directory, and response body updated
  body_path <- req_cache_path(req, ".body")
  expect_equal(readLines(body_path), "Hi there")

  resp_from_cache <- cache_get(req)
  expect_equal(resp_from_cache$body, new_path(body_path))

  # If path is null, just leave body as is, since req_body() already
  # papers over the differences
  expect_equal(cache_body(resp_from_cache, NULL), new_path(body_path))

  # If path is not null, copy to desired location, and update body
  path2 <- tempfile()
  body <- cache_body(resp_from_cache, path2)
  expect_equal(readLines(body), "Hi there")
  expect_equal(body, new_path(path2))
})

test_that("corrupt files are ignored", {
  cache_dir <- withr::local_tempdir()
  req <- request("http://example.com") %>% req_cache(cache_dir)

  writeLines(letters, req_cache_path(req))
  expect_true(is.null(cache_get(req)))

  saveRDS(1:10, req_cache_path(req))
  expect_false(is.null(cache_get(req)))
})

# pruning -----------------------------------------------------------------

test_that("pruning is throttled", {
  path <- withr::local_tempdir()
  req <- req_cache(request_test(), path = path)

  expect_true(cache_prune_if_needed(req))
  expect_false(cache_prune_if_needed(req))
  expect_true(cache_prune_if_needed(req, threshold = 0))

  the$cache_throttle[[path]] <- Sys.time() - 61
  expect_true(cache_prune_if_needed(req, threshold = 60))
})

test_that("can prune by number", {
  path <- withr::local_tempdir()
  file.create(file.path(path, c("a.rds", "b.rds", "c.rds")))
  Sys.sleep(0.1)
  file.create(file.path(path, c("d.rds")))

  cache_prune(path, list(n = 4, age = Inf, size = Inf), debug = TRUE)
  expect_equal(dir(path), c("a.rds", "b.rds", "c.rds", "d.rds"))

  expect_snapshot(
    cache_prune(path, list(n = 1, age = Inf, size = Inf), debug = TRUE)
  )
  expect_equal(dir(path), c("d.rds"))
})

test_that("can prune by age", {
  path <- withr::local_tempdir()
  file.create(file.path(path, c("a.rds", "b.rds")))
  Sys.setFileTime(file.path(path, "a.rds"), Sys.time() - 60)

  cache_prune(path, list(n = Inf, age = 120, size = Inf), debug = TRUE)
  expect_equal(dir(path), c("a.rds", "b.rds"))

  expect_snapshot({
    cache_prune(path, list(n = Inf, age = 30, size = Inf), debug = TRUE)
  })
  expect_equal(dir(path), "b.rds")
})

test_that("can prune by size", {
  path <- withr::local_tempdir()
  writeChar(paste0(letters, collapse = ""), file.path(path, "a.rds"))
  writeChar(paste0(letters, collapse = ""), file.path(path, "b.rds"))
  Sys.sleep(0.1)
  writeChar(paste0(letters, collapse = ""), file.path(path, "c.rds"))

  cache_prune(path, list(n = Inf, age = Inf, size = 200), debug = TRUE)
  expect_equal(dir(path), c("a.rds", "b.rds", "c.rds"))

  expect_snapshot({
    cache_prune(path, list(n = Inf, age = Inf, size = 50), debug = TRUE)
  })
  expect_equal(dir(path), "c.rds")
})

# headers -----------------------------------------------------------------

test_that("correctly determines if response is cacheable", {
  is_cacheable <- function(...) {
    resp_is_cacheable(response(...))
  }

  expect_equal(is_cacheable(200, headers = "Expires: ABC"), TRUE)
  expect_equal(is_cacheable(200, headers = "Cache-Control: max-age=10"), TRUE)
  expect_equal(is_cacheable(200, headers = "Etag: ABC"), TRUE)
  expect_equal(is_cacheable(200, headers = c("Etag: ABC", "Cache-Control: no-store")), FALSE)
  expect_equal(is_cacheable(200), FALSE)
  expect_equal(is_cacheable(404), FALSE)
  expect_equal(is_cacheable(method = "POST"), FALSE)
})

test_that("can extract cache info with correct types", {
  resp <- response(headers = c(
    "Expires: Wed, 01 Jan 2020 00:00:00 GMT",
    "Last-Modified: Wed, 01 Jan 2010 00:00:00 GMT",
    "Etag: \"abc\""
  ))
  info <- resp_cache_info(resp)

  expect_equal(info$expires, local_time("2020-01-01"))
  # passed as is back to server, so leave as string
  expect_equal(info$last_modified, "Wed, 01 Jan 2010 00:00:00 GMT")
  # quotes are part of the etag string
  expect_equal(info$etag, '"abc"')
})

test_that("can extract various expiry values", {
  # Prefer Date + max-age
  resp1 <- response(headers = c(
    "Date: Wed, 01 Jan 2020 00:00:00 GMT",
    "Cache-Control: max-age=3600",
    "Expiry: Wed, 01 Jan 2020 00:00:00 GMT"
  ))
  expect_equal(resp_cache_expires(resp1), local_time("2020-01-01 01:00"))

  # Fall back to Expires
  resp2 <- response(headers = c(
    "Expires: Wed, 01 Jan 2020 00:00:00 GMT"
  ))
  expect_equal(resp_cache_expires(resp2), local_time("2020-01-01 00:00"))

  # Returns NA if no expiry
  resp2 <- response()
  expect_equal(resp_cache_expires(resp2), NA)
})