Skip to content

Commit

Permalink
Fix cookies on multiple page
Browse files Browse the repository at this point in the history
It was the case for golem.de, the cookie wasn't properly send to the next page (might be a bug in the cookie jar not properly retrieving previous defined cookies).
  • Loading branch information
j0k3r committed Mar 1, 2022
1 parent f8564e9 commit 786621f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
"rector": "vendor/bin/rector process -c maintenance/rector.config.php"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"phpstan/extension-installer": true
}
}
}
3 changes: 2 additions & 1 deletion src/HttpClient/Plugin/CookiePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function __construct(CookieJar $cookieJar)
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
{
$cookies = [];
// inject cookies previously defined (like when it's defined in site config)
$cookies = $request->getHeader('Cookie');
foreach ($this->cookieJar->getCookies() as $cookie) {
if ($cookie->isExpired()) {
continue;
Expand Down
31 changes: 31 additions & 0 deletions tests/GrabyFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,37 @@ public function testCookie(): void
$this->assertSame('Michael Flynn\'s Contradictory Line On Russia', $res['title']);
}

public function testCookieOnMultiplePages(): void
{
// Rector: do not add mock client – we are testing if the cookie is set.
$graby = new Graby([
'debug' => true,
'extractor' => [
'config_builder' => [
'site_config' => [__DIR__ . '/fixtures/site_config'],
],
],
]);
$res = $graby->fetchContent('https://www.golem.de/news/app-entwicklung-cross-platform-oder-nativ-programmieren-2202-162600.html');

$this->assertCount(11, $res);

$this->assertArrayHasKey('status', $res);
$this->assertArrayHasKey('html', $res);
$this->assertArrayHasKey('title', $res);
$this->assertArrayHasKey('language', $res);
$this->assertArrayHasKey('date', $res);
$this->assertArrayHasKey('authors', $res);
$this->assertArrayHasKey('url', $res);
$this->assertArrayHasKey('summary', $res);
$this->assertArrayHasKey('image', $res);
$this->assertArrayHasKey('native_ad', $res);
$this->assertArrayHasKey('headers', $res);

$this->assertSame(200, $res['status']);
$this->assertStringNotContainsString('Golem.de mit Cookies', $res['html']);
}

public function testSaveXmlUnknownEncoding(): void
{
$httpMockClient = new HttpMockClient();
Expand Down

0 comments on commit 786621f

Please sign in to comment.