Skip to content

Commit f962796

Browse files
authored
Merge pull request #10229 from Turbo87/json-array-tests
tests/util: Adjust helper methods to work for JSON arrays too
2 parents 74fc51e + 56fa9ff commit f962796

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/tests/util.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,10 @@ pub trait RequestHelper {
136136
/// Issue a PUT request
137137
async fn put<T>(&self, path: &str, body: impl Into<Bytes>) -> Response<T> {
138138
let body = body.into();
139-
let is_json = body.starts_with(b"{") && body.ends_with(b"}");
140139

141140
let mut request = self.request_builder(Method::PUT, path);
142141
*request.body_mut() = body;
143-
if is_json {
142+
if is_json_body(request.body()) {
144143
request.header(header::CONTENT_TYPE, "application/json");
145144
}
146145

@@ -150,11 +149,10 @@ pub trait RequestHelper {
150149
/// Issue a PATCH request
151150
async fn patch<T>(&self, path: &str, body: impl Into<Bytes>) -> Response<T> {
152151
let body = body.into();
153-
let is_json = body.starts_with(b"{") && body.ends_with(b"}");
154152

155153
let mut request = self.request_builder(Method::PATCH, path);
156154
*request.body_mut() = body;
157-
if is_json {
155+
if is_json_body(request.body()) {
158156
request.header(header::CONTENT_TYPE, "application/json");
159157
}
160158

@@ -170,11 +168,10 @@ pub trait RequestHelper {
170168
/// Issue a DELETE request with a body... yes we do it, for crate owner removal
171169
async fn delete_with_body<T>(&self, path: &str, body: impl Into<Bytes>) -> Response<T> {
172170
let body = body.into();
173-
let is_json = body.starts_with(b"{") && body.ends_with(b"}");
174171

175172
let mut request = self.request_builder(Method::DELETE, path);
176173
*request.body_mut() = body;
177-
if is_json {
174+
if is_json_body(request.body()) {
178175
request.header(header::CONTENT_TYPE, "application/json");
179176
}
180177

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

260+
fn is_json_body(body: &Bytes) -> bool {
261+
(body.starts_with(b"{") && body.ends_with(b"}"))
262+
|| (body.starts_with(b"[") && body.ends_with(b"]"))
263+
}
264+
263265
/// A type that can generate unauthenticated requests
264266
pub struct MockAnonymousUser {
265267
app: TestApp,

0 commit comments

Comments
 (0)