romalops's avatar

Phpunit test always return 302 status

Hello guys !

I'm running into weird issues. When running any test that make a request, it gives me back an 302 redirect, no matters what the URL is.

public function test_login_screen_can_be_rendered()
{
    $response = $this->get('/login')->dump();
    $response->assertStatus(200);
}

When I dump the reponse data I get :

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

        <title>Redirecting to https://daily-drum.test/login</title>
    </head>
    <body>
        Redirecting to <a href="https://daily-drum.test/login">https://daily-drum.test/login</a>.
    </body>
</html>

I don't really understand why it's happening. Any suggestions ? I think it's maybe a problem of the created server or I don't know what's really happen. Btw, I'm using Laragon, but I think it's not maybe related ?

0 likes
9 replies
Sinnbeck's avatar

Why do you have this meta tag?

 <meta http-equiv="refresh" content="0;url='https://daily-drum.test/login'" />
romalops's avatar

@Sinnbeck I don't know, that what the answer of the request send me back. This code isn't generated from me

martinbean's avatar

@romalops EDIT: In fact, I see the problem. You seem to have a redirect to HTTPS. So you either need to set the correct application URL, or only redirect to HTTPS in environments that need to.

romalops's avatar

@martinbean I guess so,

So I tried to edit the phpunit.xml file by adding this line :

<server name="APP_URL" value="http://daily-drum.test" />

But it doesn' change anything. Is there any HTTPS redirection made by default by Laravel ?

martinbean's avatar

Is there any HTTPS redirection made by default by Laravel ?

@romalops No. That will be something you’ve added.

martinbean's avatar

@romalops Well, you are somewhere, because like I say, Laravel doesn’t do it by default. It would break so many people’s development environments if it tried to redirect HTTP URLs to HTTPS ones.

romalops's avatar

Okay found it, I created a Middleware that was always redirecting by trying to put a cookie

class LocalizationMiddleware
{
    public function handle(Request $request, Closure $next)
    {
        if ($request->cookie('LANG_SELECTED')) {
            App::setLocale($request->cookie('LANG_SELECTED'));
        } else {
            // First visit on the website
            try {
                $locale = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
                return redirect($request->fullUrl())->withCookie(cookie()->forever('LANG_SELECTED', $locale));
            } catch (\Exception $e) {
                return redirect($request->fullUrl())->withCookie(cookie()->forever('LANG_SELECTED', config('app.locale', 'en')));
            }
        }

        if (Auth::user()) {
            App::setLocale(Auth::user()->lang);
        }

        return $next($request);
    }
}

1 like

Please or to participate in this conversation.