Skip to content

Commit 56c42a8

Browse files
authored
LYNX-445: Magento\TestFramework\TestCase\GraphQl\Client::processResponseHeaders() does not allow to process multiple Set-Cookie headers (#261)
1 parent 46b644a commit 56c42a8

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

dev/tests/api-functional/framework/Magento/TestFramework/TestCase/GraphQl/Client.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class Client
2222
public const GRAPHQL_METHOD_POST = 'POST';
2323
/**#@-*/
2424

25+
private const SET_COOKIE_HEADER_NAME = 'Set-Cookie';
26+
2527
/** @var CurlClient */
2628
private $curlClient;
2729

@@ -264,7 +266,15 @@ private function processResponseHeaders(string $headers): array
264266
foreach ($headerLines as $headerLine) {
265267
$headerParts = preg_split('/: /', $headerLine, 2);
266268
if (count($headerParts) == 2) {
267-
$headersArray[trim($headerParts[0])] = trim($headerParts[1]);
269+
$headerName = trim($headerParts[0]);
270+
if ($headerName === self::SET_COOKIE_HEADER_NAME) {
271+
if (!isset($headersArray[self::SET_COOKIE_HEADER_NAME])) {
272+
$headersArray[self::SET_COOKIE_HEADER_NAME] = [];
273+
}
274+
$headersArray[self::SET_COOKIE_HEADER_NAME][] = trim($headerParts[1]);
275+
} else {
276+
$headersArray[$headerName] = trim($headerParts[1]);
277+
}
268278
} elseif (preg_match('/HTTP\/[\.0-9]+/', $headerLine)) {
269279
$headersArray[trim('Status-Line')] = trim($headerLine);
270280
}

dev/tests/Magento/GraphQl/PageCache/DisableSessionTest.php renamed to dev/tests/api-functional/testsuite/Magento/GraphQl/PageCache/DisableSessionTest.php

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@
1616

1717
namespace Magento\GraphQl\PageCache;
1818

19-
use Magento\TestFramework\TestCase\GraphQlAbstract;
20-
use Magento\TestFramework\Fixture\Config;
2119
use Magento\Framework\App\PageCache\Version;
20+
use Magento\TestFramework\Fixture\Config;
21+
use Magento\TestFramework\TestCase\GraphQlAbstract;
2222

2323
/**
2424
* Test absence/presence of private_content_version cookie in GraphQl POST HTTP responses
2525
*/
2626
class DisableSessionTest extends GraphQlAbstract
2727
{
28+
private const PHPSESSID_COOKIE_NAME = 'PHPSESSID';
29+
2830
#[
2931
Config('graphql/session/disable', '1')
3032
]
@@ -33,10 +35,14 @@ public function testPrivateSessionContentCookieNotPresentWhenSessionDisabled()
3335
$result = $this->graphQlMutationWithResponseHeaders($this->getMutation());
3436
$this->assertArrayHasKey('headers', $result);
3537
if (!empty($result['headers']['Set-Cookie'])) {
36-
$this->assertStringNotContainsString(
37-
Version::COOKIE_NAME,
38-
$result['headers']['Set-Cookie'],
39-
Version::COOKIE_NAME . ' should not be present in Set-Cookie header'
38+
$this->assertFalse(
39+
$this->isCookieSet($result['headers']['Set-Cookie'], self::PHPSESSID_COOKIE_NAME),
40+
self::PHPSESSID_COOKIE_NAME . ' should not be present in HTTP response'
41+
);
42+
43+
$this->assertFalse(
44+
$this->isCookieSet($result['headers']['Set-Cookie'], Version::COOKIE_NAME),
45+
Version::COOKIE_NAME . ' should not be present in HTTP response'
4046
);
4147
}
4248
}
@@ -49,11 +55,30 @@ public function testPrivateSessionContentCookiePresentWhenSessionEnabled()
4955
$result = $this->graphQlMutationWithResponseHeaders($this->getMutation());
5056
$this->assertArrayHasKey('headers', $result);
5157
$this->assertArrayHasKey('Set-Cookie', $result['headers'], 'Set-Cookie HTTP response header should be present');
52-
$this->assertStringContainsString(
53-
Version::COOKIE_NAME,
54-
$result['headers']['Set-Cookie'],
55-
Version::COOKIE_NAME . ' should be set by the server'
58+
59+
$this->assertTrue(
60+
$this->isCookieSet($result['headers']['Set-Cookie'], self::PHPSESSID_COOKIE_NAME),
61+
self::PHPSESSID_COOKIE_NAME . ' should be present in HTTP response'
5662
);
63+
64+
$this->assertTrue(
65+
$this->isCookieSet($result['headers']['Set-Cookie'], Version::COOKIE_NAME),
66+
Version::COOKIE_NAME . ' should be present in HTTP response'
67+
);
68+
}
69+
70+
/**
71+
* Checks if $cookieName was set by server in any of Set-Cookie header(s)
72+
*
73+
* @param array $setCookieHeader
74+
* @param string $cookieName
75+
* @return bool
76+
*/
77+
private function isCookieSet(array $setCookieHeader, string $cookieName): bool
78+
{
79+
return count(array_filter($setCookieHeader, function ($cookie) use ($cookieName) {
80+
return str_starts_with($cookie, $cookieName);
81+
})) > 0;
5782
}
5883

5984
/**

0 commit comments

Comments
 (0)