Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow empty body #59

Merged
merged 3 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (!$request->isJson()) {
if (!empty($request->all()) && !$request->isJson()) {
return response()->json([
'message' => 'Only JSON requests are accepted'
], Response::HTTP_BAD_REQUEST);
Expand Down
19 changes: 19 additions & 0 deletions tests/Feature/Http/Middleware/JsonOnlyMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,23 @@ public function test_json_requests_are_accepted(): void

$user->delete();
}

/**
* Test that empty requests are accepted.
*/
public function test_empty_requests_are_accepted(): void
{

$response = $this->post(uri: '/api/v1/login', headers: ['Accept' => 'application/json']);

$response->assertStatus(422)
->assertJson([
'message' => 'The email field is required. (and 1 more error)',
'errors' => [
'email' => ['The email field is required.'],
'password' => ['The password field is required.']
]
]);

}
}