简体   繁体   中英

Simulate HTTP_REFERER in a Laravel Dusk test?

Is there a straightforward way to simulate HTTP_REFERER in a Dusk test? I'm using Laravel\\Dusk\\Browser and calling the visit() method on various pages. This is using the Selenium driver.


Something like setReferer below:

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.

See https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/2047

Option 1

Navigate to the referer URL, then inject a script that makes the page navigate to the one you want to test.

Option 2

Use a proxy server that intercepts the requests and sets the referer to the one you want

https://github.com/j-bennet/selenium-referer

Option 3

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

<?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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM