I'am trying to test my login API with TestCase.
The dump of response of PHPunit is:
R{#1553 +"timestamp": {#72 +"date": "2021-06-12 09:50:01.210655" +"timezone_type": 3 +"timezone": "UTC" } +"status": 500 +"error": "Internal Server Error" +"message": "No message available" }
But in the log file i see:
testing.INFO: Report: Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: The POST method is not supported for this route. Supported methods: GET, HEAD. in \laravel_code\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php:258
I'm tring to login and my route is configured ad follow:
`Route::group([
'middleware' => 'api',
'prefix' => 'v1/auth',
'namespace' => 'Api\v1'
], function ($router) {
Route::post('login', 'AuthController@login');
});I use json method in my TestCase:
$payload = ['email' => 'XXX', 'password' => XXX'];
$headers = [
'Accept' => 'application/json',
'Cache-Control' => 'no-cache',
'Content-Type' => 'application/json',
'Host' => 'fullservice.local',
'Content-Length' => strlen(json_encode($payload))
];`
$response = $this->json('POST', 'api/v1/auth/login', $payload, $headers); With UI and Postman it works.
How do you see the route if you run php artisan route:list
in your terminal? And are you sending extra headers with postman that could result in a different request?
Hi,
i see method POST in my terminal. So i don't undenstad why phpunit not see it.
| | POST | api/v1/auth/login | | App\Http\Controllers\Api\v1\AuthController@login
I tried to send same headers used in postman, but the result don't change.
Can you do a $response->dump();
Or do you get the error on the json() call?
I have no idea why the route doesn't work in your test so it is difficult to help.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community