Skip to content

Commit

Permalink
bug: fix custom content-type overidden by json method (#1833)
Browse files Browse the repository at this point in the history
* bug: fix custom content-type overided for json method

* fix format

---------

Co-authored-by: hulin <[email protected]>
  • Loading branch information
hulin32 and hulin32 authored May 15, 2023
1 parent eca2a2f commit b13ca4b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/async_impl/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
6 changes: 4 additions & 2 deletions src/blocking/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
34 changes: 34 additions & 0 deletions tests/blocking.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
mod support;

use http::header::CONTENT_TYPE;
use http::HeaderValue;
use std::collections::HashMap;
use support::*;

#[test]
Expand Down Expand Up @@ -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());
}
34 changes: 34 additions & 0 deletions tests/client.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
}

0 comments on commit b13ca4b

Please sign in to comment.