jeudi 16 février 2017

Testing redirect coming from a middleware

I have this test case:

public function testRedirectToAndSetDefaultLocale()
{
    $locale = config('app.locale');
    $this->visit('/')->assertRedirectedTo('/' . $locale);
    $this->assertEquals($locale, app()->getLocale());
}

And the following middleware:

class SetLocale
{
    public function __construct(Application $app, Redirector $redirector, Request $request)
    {
        $this->app = $app;
        $this->redirector = $redirector;
        $this->request = $request;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @param  string|null $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        $locale = $request->segment(1);

        if (!array_key_exists($locale, $this->app->config->get('app.locales'))) {
            $segments = $request->segments();
            $segments[0] = $this->app->config->get('app.fallback_locale');

            return $this->redirector->to(implode('/', $segments));
        }

        $this->app->setLocale($locale);

        return $next($request);
    }
}

The test case above fails with the following trace:

  A request to [http://localhost:8000/en] failed. Received status code [404]./Users/xxx/xxxxxxxxxx/xxxxxxxxx.project/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:196
    /Users/xxx/xxxxxxxxxx/xxxxxxxxx.project/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:80
    /Users/xxx/xxxxxxxxxx/xxxxxxxxx.project/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:138
    /Users/xxx/xxxxxxxxxx/xxxxxxxxx.project/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:80
    /Users/xxx/xxxxxxxxxx/xxxxxxxxx.project/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:61
    /Users/xxx/xxxxxxxxxx/xxxxxxxxx.project/tests/HomepageTests.php:17

I'm assuming that a server is not started or something like that. However I don't know what to do.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2kt6L0s
via IFTTT

Aucun commentaire:

Enregistrer un commentaire