Skip to content

Commit de6165a

Browse files
committed
fix: only add json content type if no content type was explicitly set by user #5
1 parent 9cb390d commit de6165a

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "drupal_kit"
33
description = "Flexible Drupal HTTP client supporting a variety of popular contrib modules"
4-
version = "0.3.2"
4+
version = "0.3.3"
55
edition = "2021"
66
license = "Apache-2.0 OR MIT"
77

src/http_client.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,17 @@ pub trait HttpClient {
153153
{
154154
async move {
155155
// Add Content-Type: application/json header by default
156+
// Only add Content-Type if not already set
156157
let mut options = options;
157-
options.push(HttpRequestOption::Header(
158-
HeaderName::from_static("content-type"),
159-
HeaderValue::from_static("application/json"),
160-
));
158+
if !options.iter().any(|opt| match opt {
159+
HttpRequestOption::Header(name, _) => name == "content-type",
160+
_ => false,
161+
}) {
162+
options.push(HttpRequestOption::Header(
163+
HeaderName::from_static("content-type"),
164+
HeaderValue::from_static("application/json"),
165+
));
166+
}
161167

162168
match self.request(method, path, body, options).await {
163169
Ok(response) => {

tests/http_client.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use drupal_kit::http::Method;
2-
use drupal_kit::http_client::HttpClient;
2+
use drupal_kit::http_client::{HttpClient, HttpRequestOption};
33
use reqwest::Client;
4+
use http;
5+
use http::header;
46

57
struct TestHttpClient {
68
client: Client,
@@ -49,3 +51,36 @@ async fn test_request_json_adds_content_type_header() {
4951
// The request should succeed because the mock expects the Content-Type header
5052
assert!(result.is_ok());
5153
}
54+
55+
#[tokio::test]
56+
async fn test_request_json_custom_content_type() {
57+
let mut server = mockito::Server::new_async().await;
58+
let base_url = server.url();
59+
60+
// Create a mock that expects a custom Content-Type header
61+
let _mock = server
62+
.mock("GET", "/test")
63+
.match_header("content-type", "application/xml")
64+
.with_status(200)
65+
.with_body("{}")
66+
.create_async()
67+
.await;
68+
69+
let client = TestHttpClient::new(base_url);
70+
71+
// Make a request with a custom Content-Type header
72+
let result: Result<serde_json::Value, _> = client
73+
.request_json(
74+
Method::GET,
75+
"/test",
76+
"",
77+
vec![HttpRequestOption::Header(
78+
http::header::CONTENT_TYPE,
79+
http::HeaderValue::from_static("application/xml"),
80+
)],
81+
)
82+
.await;
83+
84+
// The request should succeed because the mock expects the custom Content-Type header
85+
assert!(result.is_ok());
86+
}

0 commit comments

Comments
 (0)