[英]Simulate HTTP_REFERER in a Laravel Dusk test?
Is there a straightforward way to simulate HTTP_REFERER
in a Dusk test? 在Dusk测试中是否有直接的方法来模拟HTTP_REFERER
? I'm using Laravel\\Dusk\\Browser
and calling the visit()
method on various pages. 我正在使用Laravel\\Dusk\\Browser
并在各种页面上调用visit()
方法。 This is using the Selenium driver. 这是使用Selenium驱动程序。
Something like setReferer
below: 像下面的setReferer
:
namespace Example\Tests\Browser\Processes\PublicSite;
class SampleBrowser extends Browser {
use MakesAssertions, ProvidesAdditionalBrowserActions, WaitsForElements;
public function __construct(RemoteWebDriver $driver, $resolver = null)
{
parent::__construct($driver, new ElementResolver($driver,
$resolver->prefix ?? 'body'));
}
}
class SampleTestCase extends BrowserTestCase
{
/**
* Test that the deal builder modal shows up.
*/
public function testRefererRendering()
{
$this->browse(function (SampleBrowser $browser) {
$browser
// this is the bit that I want
->setReferer('https://example.org/')
->visit('/')
->waitForLocation('/?came_via=example.org')
->assertCookieValue('came_via', 'example.org');
});
}
}
Dusk or any Browser/Selenium testing solution doesn't implement this kind of feature, and it doesn't seem they ever will. 黄昏或任何浏览器/ Selenium测试解决方案都没有实现这种功能,它似乎永远不会实现。
See https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/2047 请参阅https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/2047
Navigate to the referer URL, then inject a script that makes the page navigate to the one you want to test. 导航到referer URL,然后注入一个脚本,使页面导航到您要测试的页面。
Use a proxy server that intercepts the requests and sets the referer to the one you want 使用代理服务器拦截请求并将引用者设置为您想要的引用
https://github.com/j-bennet/selenium-referer https://github.com/j-bennet/selenium-referer
If you're trying to test the behaviour of the server-side code, you can always test it without fully emulating the client side with a browser. 如果您正在尝试测试服务器端代码的行为,则可以始终对其进行测试,而无需使用浏览器完全模拟客户端。
For example: 例如:
<?php
namespace App\Http\Controllers;
use \Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class HomeController extends Controller
{
public function home(Request $request)
{
$referer = $request->header('Referer');
if ($referer) {
return redirect()->route('home', ['came_via' => $referer]);
}
return view('welcome');
}
}
and the TestCase 和TestCase
<?php
namespace Tests\Feature;
use Tests\TestCase;
class RedirectTest extends TestCase
{
/**
* A basic redirect example.
*
* @return void
*/
public function testRedirection()
{
// Normal request
$response = $this->get('/');
$response->assertStatus(200);
// Referer check redirection
$referer = 'http://google.com';
$response = $this->call('GET', '/', [], [], [], ['HTTP_REFERER' => $referer]);
$response->assertRedirect('/?came_via=' . urlencode($referer));
}
}
Result 结果
OK (2 tests, 4 assertions)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.