Testing redirect links to external sites Testing redirect links to external sites laravel laravel

Testing redirect links to external sites


I am struggling with a similar problem right now, and I have just about given up on testing the endpoint directly. Opting for a solution like this...

Test that the link contains proper information in the view:

$this->visit('/my/example/page')    ->seeElement('a', ['href' => route('my.route')]);

Moving the logic in the controller to something you can test directly. The Laravel\Socialite package has some interesting tests that might be helpful if you do this...

class ExternalLinkRedirect {    public function __construct($request){        $this->request = $request;    }    public function redirect()    {        return redirect()->away('exteranlsite.com');    }}

Then test it directly

    $route = route('my.route');    $request = \Illuminate\Http\Request::create($route);    $redirector = new ExternalLinkRedirect($request);    $response = $redirector->redirect();    $this->assertEquals('www.externalsite.com', $response->getTargetUrl());


A simple way is to use the get method instead of visit.

An example:$this->get('/facebook') ->assertRedirectedTo('https://www.facebook.com/Les-Teachers-du-NET-516952535045054');