Skip to content

Commit d639f0c

Browse files
authored
tests/util: Add Content-Type: application/json request headers (#7895)
The `axum::Json` extractor requires this header to be present and rejects requests that don't have it. This change ensures we can move our code over from custom JSON parsing to the `axum` extractor eventually.
1 parent 84230a8 commit d639f0c

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/tests/util.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,15 @@ pub trait RequestHelper {
139139
/// Issue a PUT request
140140
#[track_caller]
141141
fn put<T>(&self, path: &str, body: impl Into<Bytes>) -> Response<T> {
142+
let body = body.into();
143+
let is_json = body.starts_with(b"{") && body.ends_with(b"}");
144+
142145
let mut request = self.request_builder(Method::PUT, path);
143-
*request.body_mut() = body.into();
146+
*request.body_mut() = body;
147+
if is_json {
148+
request.header(header::CONTENT_TYPE, "application/json");
149+
}
150+
144151
self.run(request)
145152
}
146153

@@ -154,8 +161,15 @@ pub trait RequestHelper {
154161
/// Issue a DELETE request with a body... yes we do it, for crate owner removal
155162
#[track_caller]
156163
fn delete_with_body<T>(&self, path: &str, body: impl Into<Bytes>) -> Response<T> {
164+
let body = body.into();
165+
let is_json = body.starts_with(b"{") && body.ends_with(b"}");
166+
157167
let mut request = self.request_builder(Method::DELETE, path);
158-
*request.body_mut() = body.into();
168+
*request.body_mut() = body;
169+
if is_json {
170+
request.header(header::CONTENT_TYPE, "application/json");
171+
}
172+
159173
self.run(request)
160174
}
161175

0 commit comments

Comments
 (0)