use std::time::Duration;
pub(crate) use daimon_core::stream_util::backoff_delay;
pub(crate) fn parse_retry_after(headers: &reqwest::header::HeaderMap) -> Option<Duration> {
headers
.get(reqwest::header::RETRY_AFTER)
.and_then(|v| v.to_str().ok())
.and_then(daimon_core::stream_util::parse_retry_after_secs)
.map(Duration::from_secs)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_retry_after_seconds() {
let mut headers = reqwest::header::HeaderMap::new();
headers.insert(reqwest::header::RETRY_AFTER, "12".parse().unwrap());
assert_eq!(parse_retry_after(&headers), Some(Duration::from_secs(12)));
}
#[test]
fn test_parse_retry_after_absent() {
let headers = reqwest::header::HeaderMap::new();
assert_eq!(parse_retry_after(&headers), None);
}
#[test]
fn test_parse_retry_after_http_date_ignored() {
let mut headers = reqwest::header::HeaderMap::new();
headers.insert(
reqwest::header::RETRY_AFTER,
"Wed, 21 Oct 2015 07:28:00 GMT".parse().unwrap(),
);
assert_eq!(parse_retry_after(&headers), None);
}
}