From b13ca4b3399b42e7bbdafc374a129ea09bf33b17 Mon Sep 17 00:00:00 2001 From: Hulin Date: Mon, 15 May 2023 23:26:29 +0200 Subject: [PATCH] bug: fix custom content-type overidden by json method (#1833) * bug: fix custom content-type overided for json method * fix format --------- Co-authored-by: hulin --- src/async_impl/request.rs | 6 ++++-- src/blocking/request.rs | 6 ++++-- tests/blocking.rs | 34 ++++++++++++++++++++++++++++++++++ tests/client.rs | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 4 deletions(-) diff --git a/src/async_impl/request.rs b/src/async_impl/request.rs index aaf97e940..a5e22e060 100644 --- a/src/async_impl/request.rs +++ b/src/async_impl/request.rs @@ -440,8 +440,10 @@ impl RequestBuilder { if let Ok(ref mut req) = self.request { match serde_json::to_vec(json) { Ok(body) => { - req.headers_mut() - .insert(CONTENT_TYPE, HeaderValue::from_static("application/json")); + if !req.headers().contains_key(CONTENT_TYPE) { + req.headers_mut() + .insert(CONTENT_TYPE, HeaderValue::from_static("application/json")); + } *req.body_mut() = Some(body.into()); } Err(err) => error = Some(crate::error::builder(err)), diff --git a/src/blocking/request.rs b/src/blocking/request.rs index 6dd7935e7..71bbdc932 100644 --- a/src/blocking/request.rs +++ b/src/blocking/request.rs @@ -494,8 +494,10 @@ impl RequestBuilder { if let Ok(ref mut req) = self.request { match serde_json::to_vec(json) { Ok(body) => { - req.headers_mut() - .insert(CONTENT_TYPE, HeaderValue::from_static("application/json")); + if !req.headers().contains_key(CONTENT_TYPE) { + req.headers_mut() + .insert(CONTENT_TYPE, HeaderValue::from_static("application/json")); + } *req.body_mut() = Some(body.into()); } Err(err) => error = Some(crate::error::builder(err)), diff --git a/tests/blocking.rs b/tests/blocking.rs index 93f204ac8..28412b6c3 100644 --- a/tests/blocking.rs +++ b/tests/blocking.rs @@ -1,4 +1,8 @@ mod support; + +use http::header::CONTENT_TYPE; +use http::HeaderValue; +use std::collections::HashMap; use support::*; #[test] @@ -328,3 +332,33 @@ fn test_body_from_bytes() { assert_eq!(request.body().unwrap().as_bytes(), Some(body.as_bytes())); } + +#[test] +#[cfg(feature = "json")] +fn blocking_add_json_default_content_type_if_not_set_manually() { + let mut map = HashMap::new(); + map.insert("body", "json"); + let content_type = HeaderValue::from_static("application/vnd.api+json"); + let req = reqwest::blocking::Client::new() + .post("https://google.com/") + .header(CONTENT_TYPE, &content_type) + .json(&map) + .build() + .expect("request is not valid"); + + assert_eq!(content_type, req.headers().get(CONTENT_TYPE).unwrap()); +} + +#[test] +#[cfg(feature = "json")] +fn blocking_update_json_content_type_if_set_manually() { + let mut map = HashMap::new(); + map.insert("body", "json"); + let req = reqwest::blocking::Client::new() + .post("https://google.com/") + .json(&map) + .build() + .expect("request is not valid"); + + assert_eq!("application/json", req.headers().get(CONTENT_TYPE).unwrap()); +} diff --git a/tests/client.rs b/tests/client.rs index 56267e639..015da127d 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -1,6 +1,10 @@ #![cfg(not(target_arch = "wasm32"))] mod support; + use futures_util::stream::StreamExt; +use http::header::CONTENT_TYPE; +use http::HeaderValue; +use std::collections::HashMap; use support::*; use reqwest::Client; @@ -372,3 +376,33 @@ async fn test_allowed_methods() { assert!(resp.is_err()); } + +#[test] +#[cfg(feature = "json")] +fn add_json_default_content_type_if_not_set_manually() { + let mut map = HashMap::new(); + map.insert("body", "json"); + let content_type = HeaderValue::from_static("application/vnd.api+json"); + let req = Client::new() + .post("https://google.com/") + .header(CONTENT_TYPE, &content_type) + .json(&map) + .build() + .expect("request is not valid"); + + assert_eq!(content_type, req.headers().get(CONTENT_TYPE).unwrap()); +} + +#[test] +#[cfg(feature = "json")] +fn update_json_content_type_if_set_manually() { + let mut map = HashMap::new(); + map.insert("body", "json"); + let req = Client::new() + .post("https://google.com/") + .json(&map) + .build() + .expect("request is not valid"); + + assert_eq!("application/json", req.headers().get(CONTENT_TYPE).unwrap()); +}