Skip to content

Commit

Permalink
fix(requests): update request handling and non-json request testing (#74
Browse files Browse the repository at this point in the history
)

Modify `JsonOnlyMiddleware` to use `$request->getContent()` for request
handling. Update the corresponding test by replacing the usage of
`$this->post()` with `$this->call()` to ensure consistency in testing
non-json request failures.

---------

Signed-off-by: Valentin Sickert <[email protected]>
  • Loading branch information
Lapotor authored Dec 13, 2023
1 parent a5a8ae7 commit 0fc0bc8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/Http/Middleware/JsonOnlyMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 9 additions & 3 deletions tests/Feature/Http/Middleware/JsonOnlyMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ public function test_non_json_requests_are_rejected(): void
'password' => bcrypt('ValidPassword'),
]);

$response = $this->post('/api/v1/login', [
'body' => '<?xml version="1.0" encoding="UTF-8"?>
$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: '<?xml version="1.0" encoding="UTF-8"?>
<root>
<email>[email protected]</email>
<password>ValidPassword</password>
</root>'
], ['Content-Type' => 'text/xml']);
);

$response->assertStatus(400)
->assertJson([
Expand Down

0 comments on commit 0fc0bc8

Please sign in to comment.