The reason why the tests for HTTP 410 routes are slower compared to other "normal" routes is because Laravel's default behavior is to handle exceptions by rendering an error view. In this case, when the route returns a 410 status code, Laravel will render the error view, which adds extra processing time.
To improve the speed of the tests for HTTP 410 routes, you can modify the exception handler to return a response directly instead of rendering a view. Here's how you can do it:
-
Open the
app/Exceptions/Handler.phpfile. -
Locate the
rendermethod. -
Add the following code at the beginning of the method:
if ($exception instanceof \Symfony\Component\HttpKernel\Exception\GoneHttpException) {
return response('', 410);
}
This code checks if the exception is an instance of GoneHttpException (which is thrown when you use App::abort(410)), and if so, it returns a response with a 410 status code and an empty body.
- Save the file.
With this modification, the exception handler will return a response directly for HTTP 410 routes, eliminating the need to render an error view and improving the speed of the tests.
Please note that modifying the exception handler affects the behavior of your application globally. If you want to apply this change only for testing purposes, you can conditionally apply the modification based on the environment. For example:
if (app()->environment('testing')) {
if ($exception instanceof \Symfony\Component\HttpKernel\Exception\GoneHttpException) {
return response('', 410);
}
}
This way, the modification will only be applied when running tests.