From eccf3ddd7cddde7e0d00841bb15f66db4adf1feb Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Tue, 17 Dec 2024 13:53:56 +0100 Subject: [PATCH 1/2] tests/util: Extract `is_json_body()` fn --- src/tests/util.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/tests/util.rs b/src/tests/util.rs index d7655a912e9..44f19c13e79 100644 --- a/src/tests/util.rs +++ b/src/tests/util.rs @@ -136,11 +136,10 @@ pub trait RequestHelper { /// Issue a PUT request async fn put(&self, path: &str, body: impl Into) -> Response { 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"); } @@ -150,11 +149,10 @@ pub trait RequestHelper { /// Issue a PATCH request async fn patch(&self, path: &str, body: impl Into) -> Response { 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"); } @@ -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(&self, path: &str, body: impl Into) -> Response { 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"); } @@ -260,6 +257,10 @@ fn req(method: Method, path: &str) -> MockRequest { .unwrap() } +fn is_json_body(body: &Bytes) -> bool { + body.starts_with(b"{") && body.ends_with(b"}") +} + /// A type that can generate unauthenticated requests pub struct MockAnonymousUser { app: TestApp, From 56fa9ff6b0548f028242d00a6c8d820d593d8d91 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Tue, 17 Dec 2024 13:54:28 +0100 Subject: [PATCH 2/2] tests/util: Adjust `is_json_body()` fn to work for JSON arrays --- src/tests/util.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/tests/util.rs b/src/tests/util.rs index 44f19c13e79..f699cd5fe2e 100644 --- a/src/tests/util.rs +++ b/src/tests/util.rs @@ -258,7 +258,8 @@ fn req(method: Method, path: &str) -> MockRequest { } fn is_json_body(body: &Bytes) -> bool { - body.starts_with(b"{") && body.ends_with(b"}") + (body.starts_with(b"{") && body.ends_with(b"}")) + || (body.starts_with(b"[") && body.ends_with(b"]")) } /// A type that can generate unauthenticated requests