From 07b8a8c476cc05001d88d6c173cdf9512c12aa26 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Fri, 19 Apr 2024 12:06:31 +0100 Subject: [PATCH] Fix crawler not being updated when manually following redirect If you manually follow a redirect the crawler contains the response for the request *before*. --- src/Codeception/Lib/InnerBrowser.php | 4 +++- tests/data/app/view/index.php | 1 + tests/unit/Codeception/Module/TestsForWeb.php | 23 +++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Codeception/Lib/InnerBrowser.php b/src/Codeception/Lib/InnerBrowser.php index 7d80e37..a6d7896 100644 --- a/src/Codeception/Lib/InnerBrowser.php +++ b/src/Codeception/Lib/InnerBrowser.php @@ -2127,7 +2127,9 @@ public function startFollowingRedirects(): void */ public function followRedirect(): void { - $this->client->followRedirect(); + $this->crawler = $this->client->followRedirect(); + $this->baseUrl = $this->retrieveBaseUrl(); + $this->forms = []; } /** diff --git a/tests/data/app/view/index.php b/tests/data/app/view/index.php index 3890759..f797d32 100755 --- a/tests/data/app/view/index.php +++ b/tests/data/app/view/index.php @@ -30,6 +30,7 @@ Link Text +Test redirect A wise man said: "debug!" diff --git a/tests/unit/Codeception/Module/TestsForWeb.php b/tests/unit/Codeception/Module/TestsForWeb.php index 10a3868..69a73cb 100644 --- a/tests/unit/Codeception/Module/TestsForWeb.php +++ b/tests/unit/Codeception/Module/TestsForWeb.php @@ -8,6 +8,7 @@ use Codeception\Test\Unit; use PHPUnit\Framework\AssertionFailedError; use PHPUnit\Framework\ExpectationFailedException; +use Symfony\Component\BrowserKit\Response; /** * Author: davert @@ -1866,4 +1867,26 @@ public function testUncheckHidden() $this->assertFalse(isset($form['butter'])); } + + public function testManualRedirect() + { + $this->module->amOnPage('/info'); + $this->module->see('Information', 'h1'); + + $this->module->stopFollowingRedirects(); + $this->module->amOnPage('/'); + $this->module->seeResponseCodeIsBetween(200, 299); + $this->module->see('Welcome to test app!', 'h1'); + + // Workaround https://github.com/Codeception/util-universalframework/issues/3 + $this->module->client->mockResponse(new Response('', 301, [ + 'Location' => '/info', + ])); + + $this->module->click('Test redirect'); + $this->module->seeResponseCodeIs(301); + $this->module->followRedirect(); + $this->module->seeCurrentUrlEquals('/info'); + $this->module->see('Information', 'h1'); + } }