oxedyne/fe2o3/fe2o3_net/tests/retry_after.rs
3.4 KiB, 10 runs
created by r683211544:79534, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! `HeaderFields::retry_after` and `retry_after_at` in both forms of RFC 9110 §10.2.3, and |
| 2 | //! `Time::parse_http` as the inverse of `Time::fmt_http`. |
| 3 | |
| 4 | use oxedyne_fe2o3_core::prelude::*; |
| 5 | use oxedyne_fe2o3_net::{ |
| 6 | http::fields::{ |
| 7 | retry_after_at, |
| 8 | HeaderFields, |
| 9 | HeaderFieldValue, |
| 10 | HeaderName, |
| 11 | }, |
| 12 | time::Time, |
| 13 | }; |
| 14 | |
| 15 | use std::time::{ |
| 16 | Duration, |
| 17 | SystemTime, |
| 18 | UNIX_EPOCH, |
| 19 | }; |
| 20 | |
| 21 | fn with(val: &str) -> HeaderFields { |
| 22 | let mut f = HeaderFields::default(); |
| 23 | f.insert(HeaderName::RetryAfter, HeaderFieldValue::Generic(val.to_string()), None); |
| 24 | f |
| 25 | } |
| 26 | |
| 27 | fn now() -> Outcome<Duration> { |
| 28 | Ok(res!(SystemTime::now().duration_since(UNIX_EPOCH))) |
| 29 | } |
| 30 | |
| 31 | #[test] |
| 32 | fn test_retry_after_delta_seconds_00() -> Outcome<()> { |
| 33 | for (val, want) in [ |
| 34 | ("7", Some(Duration::from_secs(7))), |
| 35 | (" 120 ", Some(Duration::from_secs(120))), |
| 36 | ("0", Some(Duration::ZERO)), |
| 37 | ("-3", None), |
| 38 | ("soon", None), |
| 39 | ("", None), |
| 40 | ] { |
| 41 | let got = with(val).retry_after(); |
| 42 | if got != want { |
| 43 | return Err(err!("Retry-After '{}' gave {:?}, not {:?}.", val, got, want; Test, Mismatch)); |
| 44 | } |
| 45 | } |
| 46 | let got = HeaderFields::default().retry_after(); |
| 47 | if got.is_some() { |
| 48 | return Err(err!("No Retry-After gave {:?}.", got; Test, Mismatch)); |
| 49 | } |
| 50 | Ok(()) |
| 51 | } |
| 52 | |
| 53 | #[test] |
| 54 | fn test_retry_after_http_date_00() -> Outcome<()> { |
| 55 | let date = res!(Time::fmt_http(&(res!(now()) + Duration::from_secs(30)))); |
| 56 | let got = with(&date).retry_after(); |
| 57 | match got { |
| 58 | Some(d) if d >= Duration::from_secs(28) && d <= Duration::from_secs(30) => (), |
| 59 | _ => return Err(err!("Retry-After '{}' gave {:?}, not about 30 s.", date, got; Test, Mismatch)), |
| 60 | } |
| 61 | let past = res!(Time::fmt_http(&(res!(now()) - Duration::from_secs(30)))); |
| 62 | let got = with(&past).retry_after(); |
| 63 | if got != Some(Duration::ZERO) { |
| 64 | return Err(err!("A past Retry-After '{}' gave {:?}, not no wait.", past, got; Test, Mismatch)); |
| 65 | } |
| 66 | Ok(()) |
| 67 | } |
| 68 | |
| 69 | // The value alone at a clock the caller gives, as a browser client reads it. |
| 70 | #[test] |
| 71 | fn test_retry_after_at_00() -> Outcome<()> { |
| 72 | let now = Duration::from_secs(784_111_777); // fixed: RFC 9110's example date |
| 73 | for (val, want) in [ |
| 74 | ("7", Some(Duration::from_secs(7))), |
| 75 | ("Sun, 06 Nov 1994 08:50:07 GMT", Some(Duration::from_secs(30))), |
| 76 | ("Sun, 06 Nov 1994 08:49:07 GMT", Some(Duration::ZERO)), |
| 77 | ("-3", None), |
| 78 | ("soon", None), |
| 79 | ] { |
| 80 | let got = retry_after_at(val, now); |
| 81 | if got != want { |
| 82 | return Err(err!("Retry-After '{}' at {:?} gave {:?}, not {:?}.", val, now, got, want; Test, Mismatch)); |
| 83 | } |
| 84 | } |
| 85 | Ok(()) |
| 86 | } |
| 87 | |
| 88 | #[test] |
| 89 | fn test_parse_http_inverts_fmt_http_00() -> Outcome<()> { |
| 90 | let at = Duration::from_secs(784_111_777); // fixed: RFC 9110's example, Sun, 06 Nov 1994 08:49:37 GMT |
| 91 | let s = res!(Time::fmt_http(&at)); |
| 92 | if s != "Sun, 06 Nov 1994 08:49:37 GMT" { |
| 93 | return Err(err!("fmt_http gave '{}'.", s; Test, Mismatch)); |
| 94 | } |
| 95 | let back = res!(Time::parse_http(&s)); |
| 96 | if back != at { |
| 97 | return Err(err!("parse_http('{}') gave {:?}, not {:?}.", s, back, at; Test, Mismatch)); |
| 98 | } |
| 99 | if Time::parse_http("Sunday, 06-Nov-94 08:49:37 GMT").is_ok() { |
| 100 | return Err(err!("The obsolete RFC 850 form was read."; Test, Unexpected)); |
| 101 | } |
| 102 | Ok(()) |
| 103 | } |