@@ -42,7 +42,8 @@ fn assert_cached(should_put: bool, response_code: u16) {
42
42
43
43
let request = request_parts ( Request :: get ( "/" ) ) ;
44
44
45
- let policy = CachePolicy :: new_options ( & request, & response, now, options) ;
45
+ let policy = CachePolicy :: try_new_with_options ( & request, & response, now, options)
46
+ . unwrap_or_else ( |n_s| n_s. 0 ) ;
46
47
47
48
assert_eq ! ( should_put, policy. is_storable( ) ) ;
48
49
}
@@ -106,7 +107,7 @@ fn test_default_expiration_date_fully_cached_for_less_than_24_hours() {
106
107
..Default :: default ( )
107
108
} ;
108
109
109
- let policy = CachePolicy :: new_options (
110
+ let policy = CachePolicy :: try_new_with_options (
110
111
& request_parts ( Request :: get ( "/" ) ) ,
111
112
& response_parts (
112
113
Response :: builder ( )
@@ -115,7 +116,7 @@ fn test_default_expiration_date_fully_cached_for_less_than_24_hours() {
115
116
) ,
116
117
now,
117
118
options,
118
- ) ;
119
+ ) . unwrap ( ) ;
119
120
120
121
assert ! ( policy. time_to_live( now) . as_millis( ) > 4000 ) ;
121
122
}
@@ -128,7 +129,7 @@ fn test_default_expiration_date_fully_cached_for_more_than_24_hours() {
128
129
..Default :: default ( )
129
130
} ;
130
131
131
- let policy = CachePolicy :: new_options (
132
+ let policy = CachePolicy :: try_new_with_options (
132
133
& request_parts ( Request :: get ( "/" ) ) ,
133
134
& response_parts (
134
135
Response :: builder ( )
@@ -137,7 +138,7 @@ fn test_default_expiration_date_fully_cached_for_more_than_24_hours() {
137
138
) ,
138
139
now,
139
140
options,
140
- ) ;
141
+ ) . unwrap ( ) ;
141
142
142
143
assert ! ( ( policy. time_to_live( now) + policy. age( now) ) . as_secs( ) >= 10 * 3600 * 24 ) ;
143
144
assert ! ( policy. time_to_live( now) . as_millis( ) + 1000 >= 5 * 3600 * 24 ) ;
@@ -159,7 +160,7 @@ fn test_max_age_in_the_past_with_date_header_but_no_last_modified_header() {
159
160
. header ( header:: AGE , 120 )
160
161
. header ( header:: CACHE_CONTROL , "max-age=60" ) ,
161
162
) ;
162
- let policy = CachePolicy :: new_options ( & request, & response, now, options) ;
163
+ let policy = CachePolicy :: try_new_with_options ( & request, & response, now, options) . unwrap ( ) ;
163
164
164
165
assert ! ( policy. is_stale( now) ) ;
165
166
}
@@ -172,7 +173,7 @@ fn test_max_age_preferred_over_lower_shared_max_age() {
172
173
..Default :: default ( )
173
174
} ;
174
175
175
- let policy = CachePolicy :: new_options (
176
+ let policy = CachePolicy :: try_new_with_options (
176
177
& request_parts ( Request :: builder ( ) ) ,
177
178
& response_parts (
178
179
Response :: builder ( )
@@ -181,7 +182,7 @@ fn test_max_age_preferred_over_lower_shared_max_age() {
181
182
) ,
182
183
now,
183
184
options,
184
- ) ;
185
+ ) . unwrap ( ) ;
185
186
186
187
assert_eq ! ( ( policy. time_to_live( now) + policy. age( now) ) . as_secs( ) , 180 ) ;
187
188
}
@@ -200,7 +201,7 @@ fn test_max_age_preferred_over_higher_max_age() {
200
201
. header ( header:: AGE , 3 * 60 )
201
202
. header ( header:: CACHE_CONTROL , "s-maxage=60, max-age=180" ) ,
202
203
) ;
203
- let policy = CachePolicy :: new_options ( & request, & response, now, options) ;
204
+ let policy = CachePolicy :: try_new_with_options ( & request, & response, now, options) . unwrap ( ) ;
204
205
205
206
assert ! ( policy. is_stale( now) ) ;
206
207
}
@@ -219,7 +220,8 @@ fn request_method_not_cached(method: &str) {
219
220
let response =
220
221
response_parts ( Response :: builder ( ) . header ( header:: EXPIRES , format_date ( 1 , 3600 ) ) ) ;
221
222
222
- let policy = CachePolicy :: new_options ( & request, & response, now, options) ;
223
+ let not_storable = CachePolicy :: try_new_with_options ( & request, & response, now, options) . unwrap_err ( ) ;
224
+ let policy = not_storable. 0 ;
223
225
224
226
assert ! ( policy. is_stale( now) ) ;
225
227
}
@@ -252,7 +254,7 @@ fn test_etag_and_expiration_date_in_the_future() {
252
254
..Default :: default ( )
253
255
} ;
254
256
255
- let policy = CachePolicy :: new_options (
257
+ let policy = CachePolicy :: try_new_with_options (
256
258
& request_parts ( Request :: builder ( ) ) ,
257
259
& response_parts (
258
260
Response :: builder ( )
@@ -262,7 +264,7 @@ fn test_etag_and_expiration_date_in_the_future() {
262
264
) ,
263
265
now,
264
266
options,
265
- ) ;
267
+ ) . unwrap ( ) ;
266
268
267
269
assert ! ( policy. time_to_live( now) . as_millis( ) > 0 ) ;
268
270
}
@@ -275,14 +277,12 @@ fn test_client_side_no_store() {
275
277
..Default :: default ( )
276
278
} ;
277
279
278
- let policy = CachePolicy :: new_options (
280
+ CachePolicy :: try_new_with_options (
279
281
& request_parts ( Request :: builder ( ) . header ( header:: CACHE_CONTROL , "no-store" ) ) ,
280
282
& response_parts ( Response :: builder ( ) . header ( header:: CACHE_CONTROL , "max-age=60" ) ) ,
281
283
now,
282
284
options,
283
- ) ;
284
-
285
- assert ! ( !policy. is_storable( ) ) ;
285
+ ) . unwrap_err ( ) ;
286
286
}
287
287
288
288
#[ test]
@@ -297,15 +297,15 @@ fn test_request_max_age() {
297
297
. header ( header:: EXPIRES , format_date ( 1 , 3600 ) ) ,
298
298
) ;
299
299
300
- let policy = CachePolicy :: new_options (
300
+ let policy = CachePolicy :: try_new_with_options (
301
301
& first_request,
302
302
& response,
303
303
now,
304
304
CacheOptions {
305
305
shared : false ,
306
306
..Default :: default ( )
307
307
} ,
308
- ) ;
308
+ ) . unwrap ( ) ;
309
309
310
310
assert_eq ! ( policy. age( now) . as_secs( ) , 60 ) ;
311
311
assert_eq ! ( policy. time_to_live( now) . as_secs( ) , 3000 ) ;
@@ -335,7 +335,7 @@ fn test_request_min_fresh() {
335
335
let response = response_parts ( Response :: builder ( ) . header ( header:: CACHE_CONTROL , "max-age=60" ) ) ;
336
336
337
337
let policy =
338
- CachePolicy :: new_options ( & request_parts ( Request :: builder ( ) ) , & response, now, options) ;
338
+ CachePolicy :: try_new_with_options ( & request_parts ( Request :: builder ( ) ) , & response, now, options) . unwrap ( ) ;
339
339
340
340
assert ! ( !policy. is_stale( now) ) ;
341
341
@@ -368,8 +368,12 @@ fn test_request_max_stale() {
368
368
. header ( header:: AGE , 4 * 60 ) ,
369
369
) ;
370
370
371
- let policy =
372
- CachePolicy :: new_options ( & request_parts ( Request :: builder ( ) ) , & response, now, options) ;
371
+ let policy = CachePolicy :: try_new_with_options (
372
+ & request_parts ( Request :: builder ( ) ) ,
373
+ & response,
374
+ now,
375
+ options,
376
+ ) . unwrap ( ) ;
373
377
374
378
assert ! ( policy. is_stale( now) ) ;
375
379
@@ -410,8 +414,12 @@ fn test_request_max_stale_not_honored_with_must_revalidate() {
410
414
. header ( header:: AGE , 4 * 60 ) ,
411
415
) ;
412
416
413
- let policy =
414
- CachePolicy :: new_options ( & request_parts ( Request :: builder ( ) ) , & response, now, options) ;
417
+ let policy = CachePolicy :: try_new_with_options (
418
+ & request_parts ( Request :: builder ( ) ) ,
419
+ & response,
420
+ now,
421
+ options,
422
+ ) . unwrap ( ) ;
415
423
416
424
assert ! ( policy. is_stale( now) ) ;
417
425
@@ -433,14 +441,14 @@ fn test_request_max_stale_not_honored_with_must_revalidate() {
433
441
#[ test]
434
442
fn test_get_headers_deletes_cached_100_level_warnings ( ) {
435
443
let now = SystemTime :: now ( ) ;
436
- let policy = CachePolicy :: new (
444
+ let policy = CachePolicy :: try_new (
437
445
& request_parts ( Request :: builder ( ) . header ( "cache-control" , "max-stale" ) ) ,
438
446
& response_parts (
439
447
Response :: builder ( )
440
448
. header ( "cache-control" , "immutable" )
441
449
. header ( header:: WARNING , "199 test danger, 200 ok ok" ) ,
442
450
) ,
443
- ) ;
451
+ ) . unwrap ( ) ;
444
452
445
453
assert_eq ! (
446
454
"200 ok ok" ,
@@ -451,15 +459,16 @@ fn test_get_headers_deletes_cached_100_level_warnings() {
451
459
452
460
#[ test]
453
461
fn test_do_not_cache_partial_response ( ) {
454
- let policy = CachePolicy :: new (
462
+ let not_storable = CachePolicy :: try_new (
455
463
& request_parts ( Request :: builder ( ) ) ,
456
464
& response_parts (
457
465
Response :: builder ( )
458
466
. status ( 206 )
459
467
. header ( header:: CONTENT_RANGE , "bytes 100-100/200" )
460
468
. header ( header:: CACHE_CONTROL , "max-age=60" ) ,
461
469
) ,
462
- ) ;
470
+ ) . unwrap_err ( ) ;
471
+ let policy = not_storable. 0 ;
463
472
464
473
assert ! ( !policy. is_storable( ) ) ;
465
474
}
0 commit comments