Skip to content

Commit e989709

Browse files
refactor: Have CachePolicy::try_new take a time
closes #13
1 parent 8a4d158 commit e989709

File tree

10 files changed

+162
-21
lines changed

10 files changed

+162
-21
lines changed

src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ impl CachePolicy {
181181
/// Cacheability of an HTTP response depends on how it was requested, so
182182
/// both request and response are required to create the policy.
183183
///
184+
/// `response_time` is a timestamp when the response has been received, usually `SystemTime::now()`.
185+
///
186+
/// # Errors
187+
///
184188
/// This constructor returns a [`Result`] to indicate if the cache policy is considered
185189
/// storable. In the common case of disregarding these policies you can just ignore the
186190
/// `Err(_)` case like so
@@ -189,9 +193,10 @@ impl CachePolicy {
189193
/// # use http_cache_semantics::CachePolicy;
190194
/// # let req = http::Request::<()>::default();
191195
/// # let res = http::Response::<()>::default();
196+
/// # let now = std::time::SystemTime::now();
192197
/// # let mut cache = std::collections::HashMap::new();
193198
/// # let url = ();
194-
/// if let Ok(policy) = CachePolicy::try_new(&req, &res) {
199+
/// if let Ok(policy) = CachePolicy::try_new(&req, &res, now) {
195200
/// cache.insert(url, policy);
196201
/// }
197202
/// ```
@@ -204,9 +209,10 @@ impl CachePolicy {
204209
/// # use http_cache_semantics::CachePolicy;
205210
/// # let req = http::Request::<()>::default();
206211
/// # let res = http::Response::<()>::default();
212+
/// # let now = std::time::SystemTime::now();
207213
/// # let mut cache = std::collections::HashMap::new();
208214
/// # let url = ();
209-
/// let policy = CachePolicy::try_new(&req, &res).unwrap_or_else(|err| err.0);
215+
/// let policy = CachePolicy::try_new(&req, &res, now).unwrap_or_else(|err| err.0);
210216
/// // ... do something with `policy`
211217
/// if policy.is_storable() {
212218
/// cache.insert(url, policy);
@@ -216,13 +222,12 @@ impl CachePolicy {
216222
pub fn try_new<Req: RequestLike, Res: ResponseLike>(
217223
req: &Req,
218224
res: &Res,
225+
response_time: SystemTime,
219226
) -> Result<Self, NotStorable> {
220-
Self::try_new_with_options(req, res, SystemTime::now(), Default::default())
227+
Self::try_new_with_options(req, res, response_time, Default::default())
221228
}
222229

223230
/// Caching with customized behavior. See [`CacheOptions`] for details.
224-
///
225-
/// `response_time` is a timestamp when the response has been received, usually `SystemTime::now()`.
226231
#[inline]
227232
pub fn try_new_with_options<Req: RequestLike, Res: ResponseLike>(
228233
req: &Req,

tests/okhttp.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ fn test_get_headers_deletes_cached_100_level_warnings() {
448448
.header("cache-control", "immutable")
449449
.header(header::WARNING, "199 test danger, 200 ok ok"),
450450
),
451+
now,
451452
).unwrap();
452453

453454
assert_eq!(
@@ -467,6 +468,7 @@ fn test_do_not_cache_partial_response() {
467468
.header(header::CONTENT_RANGE, "bytes 100-100/200")
468469
.header(header::CACHE_CONTROL, "max-age=60"),
469470
),
471+
SystemTime::now(),
470472
).unwrap_err();
471473
let policy = not_storable.0;
472474

tests/request.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ fn test_no_store_kills_cache() {
2929
.header(header::CACHE_CONTROL, "no-store"),
3030
),
3131
&public_cacheable_response(),
32+
now,
3233
).unwrap_err();
3334
let policy = not_storable.0;
3435

@@ -41,6 +42,7 @@ fn test_post_not_cacheable_by_default() {
4142
let not_storable = CachePolicy::try_new(
4243
&request_parts(Request::builder().method(Method::POST)),
4344
&response_parts(Response::builder().header(header::CACHE_CONTROL, "public")),
45+
now,
4446
).unwrap_err();
4547
let policy = not_storable.0;
4648

@@ -53,6 +55,7 @@ fn test_post_cacheable_explicitly() {
5355
let policy = CachePolicy::try_new(
5456
&request_parts(Request::builder().method(Method::POST)),
5557
&public_cacheable_response(),
58+
now,
5659
).unwrap();
5760

5861
assert!(!policy.is_stale(now));
@@ -68,6 +71,7 @@ fn test_public_cacheable_auth_is_ok() {
6871
.header(header::AUTHORIZATION, "test"),
6972
),
7073
&public_cacheable_response(),
74+
now,
7175
).unwrap();
7276

7377
assert!(!policy.is_stale(now));
@@ -104,6 +108,7 @@ fn test_revalidate_auth_is_ok() {
104108
&response_parts(
105109
Response::builder().header(header::CACHE_CONTROL, "max-age=88,must-revalidate"),
106110
),
111+
SystemTime::now(),
107112
).unwrap();
108113

109114
assert!(policy.is_storable());
@@ -119,6 +124,7 @@ fn test_auth_prevents_caching_by_default() {
119124
.header(header::AUTHORIZATION, "test"),
120125
),
121126
&cacheable_response(),
127+
now,
122128
).unwrap_err();
123129
let policy = not_storable.0;
124130

tests/response.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ fn test_simple_miss() {
2424
let policy = CachePolicy::try_new(
2525
&request_parts(Request::builder().method(Method::GET)),
2626
&response_parts(Response::builder()),
27+
now,
2728
).unwrap();
2829

2930
assert!(policy.is_stale(now));
@@ -37,6 +38,7 @@ fn test_simple_hit() {
3738
&response_parts(
3839
Response::builder().header(header::CACHE_CONTROL, "public, max-age=999999"),
3940
),
41+
now,
4042
).unwrap();
4143

4244
assert!(!policy.is_stale(now));
@@ -51,6 +53,7 @@ fn test_quoted_syntax() {
5153
&response_parts(
5254
Response::builder().header(header::CACHE_CONTROL, " max-age = \"678\" "),
5355
),
56+
now,
5457
).unwrap();
5558

5659
assert!(!policy.is_stale(now));
@@ -84,6 +87,7 @@ fn test_pre_check_tolerated() {
8487
let not_storable = CachePolicy::try_new(
8588
&request_parts(Request::builder().method(Method::GET)),
8689
&response_parts(Response::builder().header(header::CACHE_CONTROL, cache_control)),
90+
now,
8791
).unwrap_err();
8892
let policy = not_storable.0;
8993

@@ -152,6 +156,7 @@ fn test_age_can_make_stale() {
152156
.header(header::CACHE_CONTROL, "max-age=100")
153157
.header(header::AGE, "101"),
154158
),
159+
now,
155160
).unwrap();
156161

157162
assert!(policy.is_stale(now));
@@ -168,6 +173,7 @@ fn test_age_not_always_stale() {
168173
.header(header::CACHE_CONTROL, "max-age=20")
169174
.header(header::AGE, "15"),
170175
),
176+
now,
171177
).unwrap();
172178

173179
assert!(!policy.is_stale(now));
@@ -183,6 +189,7 @@ fn test_bogus_age_ignored() {
183189
.header(header::CACHE_CONTROL, "max-age=20")
184190
.header(header::AGE, "golden"),
185191
),
192+
now,
186193
).unwrap();
187194

188195
assert!(!policy.is_stale(now));
@@ -198,6 +205,7 @@ fn test_cache_old_files() {
198205
.header(header::DATE, now_rfc2822())
199206
.header(header::LAST_MODIFIED, "Mon, 07 Mar 2016 11:52:56 GMT"),
200207
),
208+
now,
201209
).unwrap();
202210

203211
assert!(!policy.is_stale(now));
@@ -212,6 +220,7 @@ fn test_immutable_simple_hit() {
212220
&response_parts(
213221
Response::builder().header(header::CACHE_CONTROL, "immutable, max-age=999999"),
214222
),
223+
now,
215224
).unwrap();
216225

217226
assert!(!policy.is_stale(now));
@@ -224,6 +233,7 @@ fn test_immutable_can_expire() {
224233
let policy = CachePolicy::try_new(
225234
&request_parts(Request::builder().method(Method::GET)),
226235
&response_parts(Response::builder().header(header::CACHE_CONTROL, "immutable, max-age=0")),
236+
now,
227237
).unwrap();
228238

229239
assert!(policy.is_stale(now));
@@ -241,6 +251,7 @@ fn test_cache_immutable_files() {
241251
.header(header::CACHE_CONTROL, "immutable")
242252
.header(header::LAST_MODIFIED, now_rfc2822()),
243253
),
254+
now,
244255
).unwrap();
245256

246257
assert!(!policy.is_stale(now));
@@ -279,6 +290,7 @@ fn test_pragma_no_cache() {
279290
.header(header::PRAGMA, "no-cache")
280291
.header(header::LAST_MODIFIED, "Mon, 07 Mar 2016 11:52:56 GMT"),
281292
),
293+
now,
282294
).unwrap();
283295

284296
assert!(policy.is_stale(now));
@@ -295,6 +307,7 @@ fn test_blank_cache_control_and_pragma_no_cache() {
295307
.header(header::PRAGMA, "no-cache")
296308
.header(header::LAST_MODIFIED, "Mon, 07 Mar 2016 11:52:56 GMT"),
297309
),
310+
now,
298311
).unwrap();
299312

300313
assert!(!policy.is_stale(now));
@@ -308,6 +321,7 @@ fn test_no_store() {
308321
&response_parts(
309322
Response::builder().header(header::CACHE_CONTROL, "no-store, public, max-age=1"),
310323
),
324+
now,
311325
).unwrap_err();
312326
let policy = not_storable.0;
313327

@@ -324,7 +338,7 @@ fn test_observe_private_cache() {
324338
let response =
325339
response_parts(Response::builder().header(header::CACHE_CONTROL, private_header));
326340

327-
let not_storable = CachePolicy::try_new(&request, &response).unwrap_err();
341+
let not_storable = CachePolicy::try_new(&request, &response, now).unwrap_err();
328342
let shared_policy = not_storable.0;
329343

330344
let unshared_policy = CachePolicy::try_new_with_options(
@@ -353,7 +367,7 @@ fn test_do_not_share_cookies() {
353367
.header(header::CACHE_CONTROL, "max-age=99"),
354368
);
355369

356-
let shared_policy = CachePolicy::try_new(&request, &response).unwrap();
370+
let shared_policy = CachePolicy::try_new(&request, &response, now).unwrap();
357371

358372
let unshared_policy = CachePolicy::try_new_with_options(
359373
&request,
@@ -381,6 +395,7 @@ fn test_do_share_cookies_if_immutable() {
381395
.header(header::SET_COOKIE, "foo=bar")
382396
.header(header::CACHE_CONTROL, "immutable, max-age=99"),
383397
),
398+
now,
384399
).unwrap();
385400

386401
assert!(!policy.is_stale(now));
@@ -397,6 +412,7 @@ fn test_cache_explicitly_public_cookie() {
397412
.header(header::SET_COOKIE, "foo=bar")
398413
.header(header::CACHE_CONTROL, "max-age=5, public"),
399414
),
415+
now,
400416
).unwrap();
401417

402418
assert!(!policy.is_stale(now));
@@ -409,6 +425,7 @@ fn test_miss_max_age_equals_zero() {
409425
let policy = CachePolicy::try_new(
410426
&request_parts(Request::builder().method(Method::GET)),
411427
&response_parts(Response::builder().header(header::CACHE_CONTROL, "public, max-age=0")),
428+
now,
412429
).unwrap();
413430

414431
assert!(policy.is_stale(now));
@@ -425,6 +442,7 @@ fn test_uncacheable_503() {
425442
.status(503)
426443
.header(header::CACHE_CONTROL, "public, max-age=0"),
427444
),
445+
now,
428446
).unwrap_err();
429447
let policy = not_storable.0;
430448

@@ -442,6 +460,7 @@ fn test_cacheable_301() {
442460
.status(301)
443461
.header(header::LAST_MODIFIED, "Mon, 07 Mar 2016 11:52:56 GMT"),
444462
),
463+
now,
445464
).unwrap();
446465

447466
assert!(!policy.is_stale(now));
@@ -457,6 +476,7 @@ fn test_uncacheable_303() {
457476
.status(303)
458477
.header(header::LAST_MODIFIED, "Mon, 07 Mar 2016 11:52:56 GMT"),
459478
),
479+
now,
460480
).unwrap_err();
461481
let policy = not_storable.0;
462482

@@ -474,6 +494,7 @@ fn test_cacheable_303() {
474494
.status(303)
475495
.header(header::CACHE_CONTROL, "max-age=1000"),
476496
),
497+
now,
477498
).unwrap();
478499

479500
assert!(!policy.is_stale(now));
@@ -489,6 +510,7 @@ fn test_uncacheable_412() {
489510
.status(412)
490511
.header(header::CACHE_CONTROL, "public, max-age=1000"),
491512
),
513+
now,
492514
).unwrap_err();
493515
let policy = not_storable.0;
494516

@@ -506,6 +528,7 @@ fn test_expired_expires_cache_with_max_age() {
506528
.header(header::CACHE_CONTROL, "public, max-age=9999")
507529
.header(header::EXPIRES, "Sat, 07 May 2016 15:35:18 GMT"),
508530
),
531+
now,
509532
).unwrap();
510533

511534
assert!(!policy.is_stale(now));
@@ -522,6 +545,7 @@ fn request_mismatches() {
522545
.header(header::CACHE_CONTROL, "public, max-age=9999")
523546
.header(header::EXPIRES, "Sat, 07 May 2016 15:35:18 GMT"),
524547
),
548+
now,
525549
).unwrap();
526550

527551
let mismatch = policy.before_request(&request_parts(Request::builder().method(Method::POST).uri("/test")), now);
@@ -537,6 +561,7 @@ fn request_matches() {
537561
Response::builder()
538562
.header(header::CACHE_CONTROL, "public, max-age=0")
539563
),
564+
now,
540565
).unwrap();
541566

542567
let mismatch = policy.before_request(&request_parts(Request::builder().method(Method::GET).uri("/test")), now);
@@ -553,7 +578,7 @@ fn test_expired_expires_cached_with_s_maxage() {
553578
.header(header::EXPIRES, "Sat, 07 May 2016 15:35:18 GMT"),
554579
);
555580

556-
let shared_policy = CachePolicy::try_new(&request, &response).unwrap();
581+
let shared_policy = CachePolicy::try_new(&request, &response, now).unwrap();
557582

558583
let unshared_policy = CachePolicy::try_new_with_options(
559584
&request,
@@ -588,6 +613,7 @@ fn test_max_age_wins_over_future_expires() {
588613
.unwrap(),
589614
),
590615
),
616+
now,
591617
).unwrap();
592618

593619
assert!(!policy.is_stale(now));

0 commit comments

Comments
 (0)