Skip to content

tests/util: Add Content-Type: application/json request headers #7895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,15 @@ pub trait RequestHelper {
/// Issue a PUT request
#[track_caller]
fn put<T>(&self, path: &str, body: impl Into<Bytes>) -> Response<T> {
let body = body.into();
let is_json = body.starts_with(b"{") && body.ends_with(b"}");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this will eventually cause someone to have an annoying day writing tests if we ever add an endpoint that doesn't expect an object, but presumably that's not an issue right now.

I guess the other option would be to add a parallel put_json method that sets the header. I don't feel very strongly about this. 🤷

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, though arguably APIs shouldn't return raw arrays anyway and that extra friction might be good 😄

the only PUT that does not deal with Json currently is the publish endpoint. it felt not worth it to have a dedicated method just for that.


let mut request = self.request_builder(Method::PUT, path);
*request.body_mut() = body.into();
*request.body_mut() = body;
if is_json {
request.header(header::CONTENT_TYPE, "application/json");
}

self.run(request)
}

Expand All @@ -154,8 +161,15 @@ pub trait RequestHelper {
/// Issue a DELETE request with a body... yes we do it, for crate owner removal
#[track_caller]
fn delete_with_body<T>(&self, path: &str, body: impl Into<Bytes>) -> Response<T> {
let body = body.into();
let is_json = body.starts_with(b"{") && body.ends_with(b"}");

let mut request = self.request_builder(Method::DELETE, path);
*request.body_mut() = body.into();
*request.body_mut() = body;
if is_json {
request.header(header::CONTENT_TYPE, "application/json");
}

self.run(request)
}

Expand Down