diff --git a/app/Http/Middleware/JsonOnlyMiddleware.php b/app/Http/Middleware/JsonOnlyMiddleware.php index 1188a226..2535c71b 100644 --- a/app/Http/Middleware/JsonOnlyMiddleware.php +++ b/app/Http/Middleware/JsonOnlyMiddleware.php @@ -15,7 +15,7 @@ class JsonOnlyMiddleware */ public function handle(Request $request, Closure $next): Response { - if (!empty($request->all()) && !$request->isJson()) { + if (!empty($request->getContent()) && !$request->isJson()) { return response()->json([ 'message' => 'Only JSON requests are accepted' ], Response::HTTP_BAD_REQUEST); diff --git a/tests/Feature/Http/Middleware/JsonOnlyMiddlewareTest.php b/tests/Feature/Http/Middleware/JsonOnlyMiddlewareTest.php index 1aee383b..f29dfa1a 100644 --- a/tests/Feature/Http/Middleware/JsonOnlyMiddlewareTest.php +++ b/tests/Feature/Http/Middleware/JsonOnlyMiddlewareTest.php @@ -18,13 +18,19 @@ public function test_non_json_requests_are_rejected(): void 'password' => bcrypt('ValidPassword'), ]); - $response = $this->post('/api/v1/login', [ - 'body' => ' + $response = $this->call( + 'POST', + '/api/v1/login', + server: $this->transformHeadersToServerVars([ + 'Accept' => 'text/xml, application/xml, text/plain', + 'Content-Type' => 'text/xml, application/xml, text/plain', + ]), + content: ' valid@example.com ValidPassword ' - ], ['Content-Type' => 'text/xml']); + ); $response->assertStatus(400) ->assertJson([