Skip to content

Commit 4723fbc

Browse files
committed
Fix order of calls in HttpBase step
Fixes an issue when using `Http::get()->useBrowser()->postBrowserNavigateHook()`. Previously in this case, when the loader is configured to use the HTTP client, the post browser navigate hook was actually not set because of an issue with the order, things happened internally.
1 parent e08b494 commit 4723fbc

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [3.4.2] - 2025-03-08
10+
### Fixed
11+
* Issue when using `Http::get()->useBrowser()->postBrowserNavigateHook()`. Previously in this case, when the loader is configured to use the HTTP client, the post browser navigate hook was actually not set because of an issue with the order, things happened internally.
12+
913
## [3.4.1] - 2025-03-08
1014
### Fixed
1115
* Since, when using the Chrome browser for loading, we can only execute GET requests:

src/Steps/Loading/HttpBase.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ protected function getRequestFromInputUri(UriInterface $uri): RequestInterface
243243

244244
/**
245245
* @throws LoadingException
246+
* @throws Exception
246247
*/
247248
protected function getResponseFromRequest(RequestInterface $request): ?RespondedRequest
248249
{
@@ -273,10 +274,6 @@ private function applyTempLoaderCustomizations(): array
273274

274275
$resetConfig = ['resetToHttpClient' => false, 'resetToBrowser' => false];
275276

276-
if (!empty($this->postBrowserNavigateHooks) && $loader->usesHeadlessBrowser()) {
277-
$loader->browser()->setTempPostNavigateHooks($this->postBrowserNavigateHooks);
278-
}
279-
280277
if ($this->skipCache) {
281278
$loader->skipCacheForNextRequest();
282279
}
@@ -298,6 +295,10 @@ private function applyTempLoaderCustomizations(): array
298295
$loader->useHeadlessBrowser();
299296
}
300297

298+
if (!empty($this->postBrowserNavigateHooks) && $loader->usesHeadlessBrowser()) {
299+
$loader->browser()->setTempPostNavigateHooks($this->postBrowserNavigateHooks);
300+
}
301+
301302
return $resetConfig;
302303
}
303304

tests/Steps/Loading/HttpTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace tests\Steps\Loading;
44

5+
use Closure;
56
use Crwlr\Crawler\Input;
67
use Crwlr\Crawler\Loader\Http\Exceptions\LoadingException;
8+
use Crwlr\Crawler\Loader\Http\HeadlessBrowserLoaderHelper;
79
use Crwlr\Crawler\Loader\Http\HttpLoader;
810
use Crwlr\Crawler\Loader\Http\Messages\RespondedRequest;
911
use Crwlr\Crawler\Steps\Loading\Http;
@@ -723,3 +725,33 @@ function () {
723725
helper_invokeStepWithInput($step);
724726
},
725727
);
728+
729+
it(
730+
'sets post browser navigate hooks, when useBrowser() was called and the loader is configured to use the HTTP ' .
731+
'client',
732+
function () {
733+
$loader = Mockery::mock(HttpLoader::class)->makePartial();
734+
735+
$browserHelperMock = Mockery::mock(HeadlessBrowserLoaderHelper::class);
736+
737+
$loader->shouldReceive('browser')->andReturn($browserHelperMock);
738+
739+
$browserHelperMock
740+
->shouldReceive('setTempPostNavigateHooks')
741+
->once()
742+
->withArgs(function (array $hooks) {
743+
return $hooks[0] instanceof Closure;
744+
});
745+
746+
$respondedRequest = new RespondedRequest(
747+
new Request('GET', 'https://www.example.com/woop'),
748+
new Response(200, body: Utils::streamFor('Woop')),
749+
);
750+
751+
$loader->shouldReceive('load')->once()->andReturn($respondedRequest);
752+
753+
$step = Http::get()->setLoader($loader)->useBrowser()->postBrowserNavigateHook(BrowserAction::wait(1.0));
754+
755+
helper_invokeStepWithInput($step);
756+
},
757+
);

0 commit comments

Comments
 (0)