Im writing phpunit tests. I write this test:
public function testValidationFailure(){
$new_item_data = ['bad_param' => 'fancy_name'];
$response =
$this->withHeaders(['identifier' => "identifier"])
->post("localhost/item", $new_item_data);
$response->assertStatus(Response::HTTP_FOUND);
echo "status:" . $response->status();
}
And I have this in api.php
Route::middleware('ensure.token')->resource('item', ItemController::class)->only(['store', 'destroy', 'update']);
The controller looks like this:
class ItemController extends Controller{
public function store(Request $request): JsonResponse{
try{
$request->validate(['name' => 'required']);
//code to insert new row in table
} catch(Exception $e){
return response()->json(['error' => "could_not_save_item"], Response::HTTP_INTERNAL_SERVER_ERROR);
}
Now, why is the status returned equals to "302"?
i would expect another code, more like "validation-failed"-like.
302 is "HTTP_FOUND", which to me does not sound like an error.
How can I take over when validation fails and send an http code that makes more sense? Or why is laravel returning this strange http code?