fix(graphql): handle past HTTP dates in retry-after header gracefully

Extract parse_retry_after_value(header, now) as a pure function to enable
deterministic testing of Retry-After header parsing. The previous
implementation used let-chains with SystemTime::now() inline, which made
it untestable and would panic on negative durations when the server
clock was behind or the header contained a date in the past.

Changes:
- Extract parse_retry_after_value() taking an explicit `now` parameter
- Handle past HTTP dates by returning 1 second instead of panicking on
  negative Duration (date.duration_since(now) returns Err for past dates)
- Trim whitespace from header values before parsing
- Add test for past HTTP date returning 1 second minimum
- Add test for delta-seconds with surrounding whitespace

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
teernisse
2026-02-14 10:20:01 -05:00
parent 361757568f
commit 64e73b1cab
2 changed files with 26 additions and 4 deletions

View File

@@ -244,6 +244,21 @@ async fn test_retry_after_invalid_falls_back_to_60() {
}
}
#[test]
fn test_retry_after_http_date_in_past_returns_one_second() {
let now = SystemTime::now();
let past = now - Duration::from_secs(120);
let date_str = httpdate::fmt_http_date(past);
assert_eq!(parse_retry_after_value(&date_str, now), 1);
}
#[test]
fn test_retry_after_delta_seconds_trims_whitespace() {
let now = SystemTime::now();
assert_eq!(parse_retry_after_value(" 120 ", now), 120);
}
#[tokio::test]
async fn test_graphql_network_error() {
let client = GraphqlClient::new("http://127.0.0.1:1", "token");