How to get redirect route from laravel redirectResponse?

Viewed 1656

So I'm trying to test my laravel’s middleware which will return redirection.

If the request did not satisfy the middleware’s requirements, the request will be redirect using redirect()->route('name')

I tried getting the return value from the middleware like so

$response = $middleware->handle($request, function ($req) { return $req; }, 'server:summary:view:edit');
\Log::info($response);

the $response is instance of Illuminate\Http\RedirectResponse. When I do \Log::info($response); I get this result.

HTTP/1.0 302 Found
Cache-Control: no-cache, private
Date:          Mon, 13 Jul 2020 08:12:27 GMT
Location:      https://myrandomurl.test/name

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="refresh" content="0;url='https://myrandomurl.test/name" />

        <title>Redirecting to https://myrandomurl.test/name</title>
    </head>
    <body>
        Redirecting to <a href="https://myrandomurl.test/name">https://myrandomurl.test/named</a>.
    </body>
</html>

So how do I get the url from the Location tag ? or How do I get the redirect url from laravel redirectResponse ?

I tried browsing the doc at https://laravel.com/api/5.8/Illuminate/Http/RedirectResponse.html#method_content but there's no method that shows redirect route.

I need the route to assert it for successful test result.

1 Answers

The Illuminate\Http\RedirectResponse inherits Symfony\Component\HttpFoundation\RedirectResponse class that has getTargetUrl method:

$response->getTargetUrl();

Instead you may use assertRedirect for testing purposes.

Related