diff options
author | tokiedokie <thetokiedokie@gmail.com> | 2020-09-15 14:18:48 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-15 07:18:48 +0200 |
commit | f5c84920c225579af9c249bdac4a59a046ef8683 (patch) | |
tree | 5d9290ff86362e71ffd135da93fb6d83abd7b751 /cli/http_util.rs | |
parent | 04d347225776ef81282740a55b55a45419644b7f (diff) |
fix(cli/http_utils): accept a single key-multiple values headers (#7375)
Diffstat (limited to 'cli/http_util.rs')
-rw-r--r-- | cli/http_util.rs | 84 |
1 files changed, 68 insertions, 16 deletions
diff --git a/cli/http_util.rs b/cli/http_util.rs index 015bfaa46..ebd84972d 100644 --- a/cli/http_util.rs +++ b/cli/http_util.rs @@ -76,10 +76,7 @@ fn resolve_url_from_location(base_url: &Url, location: &str) -> Url { } } -// TODO(ry) HTTP headers are not unique key, value pairs. There may be more than -// one header line with the same key. This should be changed to something like -// Vec<(String, String)> -pub type HeadersMap = HashMap<String, String>; +pub type HeadersMap = HashMap<String, Vec<String>>; #[derive(Debug, PartialEq)] pub enum FetchOnceResult { @@ -112,7 +109,7 @@ pub async fn fetch_once( return Ok(FetchOnceResult::NotModified); } - let mut headers_: HashMap<String, String> = HashMap::new(); + let mut headers_: HashMap<String, Vec<String>> = HashMap::new(); let headers = response.headers(); if let Some(warning) = headers.get("X-Deno-Warning") { @@ -131,7 +128,10 @@ pub async fn fetch_once( .map(|e| e.to_str().unwrap().to_string()) .collect::<Vec<String>>() .join(","); - headers_.insert(key_str, values_str); + headers_ + .entry(key_str) + .or_insert_with(Vec::new) + .push(values_str); } if response.status().is_redirection() { @@ -248,7 +248,15 @@ mod tests { let result = fetch_once(client, &url, None).await; if let Ok(FetchOnceResult::Code(body, headers)) = result { assert!(!body.is_empty()); - assert_eq!(headers.get("content-type").unwrap(), "application/json"); + assert_eq!( + headers + .get("content-type") + .unwrap() + .first() + .unwrap() + .as_str(), + "application/json" + ); assert_eq!(headers.get("etag"), None); assert_eq!(headers.get("x-typescript-types"), None); } else { @@ -269,7 +277,12 @@ mod tests { if let Ok(FetchOnceResult::Code(body, headers)) = result { assert_eq!(String::from_utf8(body).unwrap(), "console.log('gzip')"); assert_eq!( - headers.get("content-type").unwrap(), + headers + .get("content-type") + .unwrap() + .first() + .unwrap() + .as_str(), "application/javascript" ); assert_eq!(headers.get("etag"), None); @@ -289,10 +302,18 @@ mod tests { assert!(!body.is_empty()); assert_eq!(String::from_utf8(body).unwrap(), "console.log('etag')"); assert_eq!( - headers.get("content-type").unwrap(), + headers + .get("content-type") + .unwrap() + .first() + .unwrap() + .as_str(), "application/typescript" ); - assert_eq!(headers.get("etag").unwrap(), "33a64df551425fcc55e"); + assert_eq!( + headers.get("etag").unwrap().first().unwrap().as_str(), + "33a64df551425fcc55e" + ); } else { panic!(); } @@ -316,7 +337,12 @@ mod tests { assert!(!body.is_empty()); assert_eq!(String::from_utf8(body).unwrap(), "console.log('brotli');"); assert_eq!( - headers.get("content-type").unwrap(), + headers + .get("content-type") + .unwrap() + .first() + .unwrap() + .as_str(), "application/javascript" ); assert_eq!(headers.get("etag"), None); @@ -399,7 +425,15 @@ mod tests { let result = fetch_once(client, &url, None).await; if let Ok(FetchOnceResult::Code(body, headers)) = result { assert!(!body.is_empty()); - assert_eq!(headers.get("content-type").unwrap(), "application/json"); + assert_eq!( + headers + .get("content-type") + .unwrap() + .first() + .unwrap() + .as_str(), + "application/json" + ); assert_eq!(headers.get("etag"), None); assert_eq!(headers.get("x-typescript-types"), None); } else { @@ -426,7 +460,12 @@ mod tests { if let Ok(FetchOnceResult::Code(body, headers)) = result { assert_eq!(String::from_utf8(body).unwrap(), "console.log('gzip')"); assert_eq!( - headers.get("content-type").unwrap(), + headers + .get("content-type") + .unwrap() + .first() + .unwrap() + .as_str(), "application/javascript" ); assert_eq!(headers.get("etag"), None); @@ -452,10 +491,18 @@ mod tests { assert!(!body.is_empty()); assert_eq!(String::from_utf8(body).unwrap(), "console.log('etag')"); assert_eq!( - headers.get("content-type").unwrap(), + headers + .get("content-type") + .unwrap() + .first() + .unwrap() + .as_str(), "application/typescript" ); - assert_eq!(headers.get("etag").unwrap(), "33a64df551425fcc55e"); + assert_eq!( + headers.get("etag").unwrap().first().unwrap().as_str(), + "33a64df551425fcc55e" + ); assert_eq!(headers.get("x-typescript-types"), None); } else { panic!(); @@ -486,7 +533,12 @@ mod tests { assert!(!body.is_empty()); assert_eq!(String::from_utf8(body).unwrap(), "console.log('brotli');"); assert_eq!( - headers.get("content-type").unwrap(), + headers + .get("content-type") + .unwrap() + .first() + .unwrap() + .as_str(), "application/javascript" ); assert_eq!(headers.get("etag"), None); |