Skip to content

Commit

Permalink
Merge pull request #10229 from Turbo87/json-array-tests
Browse files Browse the repository at this point in the history
tests/util: Adjust helper methods to work for JSON arrays too
  • Loading branch information
Turbo87 authored Dec 17, 2024
2 parents 74fc51e + 56fa9ff commit f962796
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,10 @@ pub trait RequestHelper {
/// Issue a PUT request
async 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"}");

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

Expand All @@ -150,11 +149,10 @@ pub trait RequestHelper {
/// Issue a PATCH request
async fn patch<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::PATCH, path);
*request.body_mut() = body;
if is_json {
if is_json_body(request.body()) {
request.header(header::CONTENT_TYPE, "application/json");
}

Expand All @@ -170,11 +168,10 @@ pub trait RequestHelper {
/// Issue a DELETE request with a body... yes we do it, for crate owner removal
async 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;
if is_json {
if is_json_body(request.body()) {
request.header(header::CONTENT_TYPE, "application/json");
}

Expand Down Expand Up @@ -260,6 +257,11 @@ fn req(method: Method, path: &str) -> MockRequest {
.unwrap()
}

fn is_json_body(body: &Bytes) -> bool {
(body.starts_with(b"{") && body.ends_with(b"}"))
|| (body.starts_with(b"[") && body.ends_with(b"]"))
}

/// A type that can generate unauthenticated requests
pub struct MockAnonymousUser {
app: TestApp,
Expand Down

0 comments on commit f962796

Please sign in to comment.