测试重定向到外部站点的链接 [英] Testing redirect links to external sites

查看:342
本文介绍了测试重定向到外部站点的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面/my/example/page上有一个链接,该链接链接到laravel路线my.route.

I have a link on a page /my/example/page which links to a laravel route my.route.

路线

Route::group(['middleware' => ['auth']], function () {
    Route::group(['middleware' => ['Some\Custom\Auth\Middleware:1']], function () {
        Route::match(['GET', 'POST'], '/my/route/to/external/controller', 'exampleController@externalLink')->name('my.route');
    }
}    

/my/example/page

<a href="/my/route/to/external/controller">Link</a>

此路由/my/route/to/external/controller指向exampleController控制器中的该控制器方法externalLink,该方法返回供href使用的URL

This route /my/route/to/external/controller points to this controller method externalLink in the exampleController controller, which returns the url for the href to use

public function externalLink()
{
    return $this->redirect->away('www.externalsite.com');
}

我的测试是

$this->visit(/my/example/page)
     ->click('Link')
     ->assertRedirectedToRoute('my.route');

我不断收到错误消息

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException

Symfony\Component\HttpKernel\Exception\NotFoundHttpException

当我使用click()测试方法时.

我可以使用@expectedException来捕获此错误,但是他没有帮助,因为我希望看到其他页面.

I can catch this using @expectedException but his doesn't help as I am expecting to see a different page.

我也尝试过(不一起);

I have also tried (not together);

->assertResponseStatus(200);
->seePageIs('www.externalsite.com');
->assertRedirect();
->followRedirects();

从浏览器检查中,单击URL后,我得到

From browser inspection, when the url is clicked I get

http://www.example.com/my/route/to/external      302
http://www.externalsite.com                      200

如何在功能上测试按钮被点击并重定向到外部站点?

How can I functionally test the button being clicked and redirecting to an external site?

推荐答案

我现在正在努力解决类似的问题,而我已经放弃了直接测试端点的方法.选择这样的解决方案...

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')]);

将控制器中的逻辑移至可以直接测试的地方. Laravel \ Socialite软件包有一些有趣的测试,可能会有所帮助如果您这样做...

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');
    }
}

然后直接测试

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

这篇关于测试重定向到外部站点的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆