diff --git a/core/lib/Drupal/Core/Cache/Context/HeadersCacheContext.php b/core/lib/Drupal/Core/Cache/Context/HeadersCacheContext.php index 0a9ac0a..7e4d009 100644 --- a/core/lib/Drupal/Core/Cache/Context/HeadersCacheContext.php +++ b/core/lib/Drupal/Core/Cache/Context/HeadersCacheContext.php @@ -25,11 +25,29 @@ public static function getLabel() { */ public function getContext($header = NULL) { if ($header === NULL) { - return $this->requestStack->getCurrentRequest()->headers->all(); + $headers = $this->requestStack->getCurrentRequest()->headers->all(); + // Order headers by name to have less cache variations. + ksort($headers); + $result = NULL; + foreach ($headers as $name => $value) { + if ($result) { + $result .= '&'; + } + else { + $result = ''; + } + $result .= $name . '=' . implode(',', $value); + } + return $result; } - else { - return $this->requestStack->getCurrentRequest()->headers->get($header); + elseif ($this->requestStack->getCurrentRequest()->headers->has($header)) { + $value = $this->requestStack->getCurrentRequest()->headers->get($header); + if ($value !== '') { + return $value; + } + return '?valueless?'; } + return NULL; } /** diff --git a/core/tests/Drupal/Tests/Core/Cache/Context/HeadersCacheContextTest.php b/core/tests/Drupal/Tests/Core/Cache/Context/HeadersCacheContextTest.php index a457695..24ecb2b 100644 --- a/core/tests/Drupal/Tests/Core/Cache/Context/HeadersCacheContextTest.php +++ b/core/tests/Drupal/Tests/Core/Cache/Context/HeadersCacheContextTest.php @@ -18,20 +18,17 @@ class HeadersCacheContextTest extends UnitTestCase { * * @dataProvider providerTestGetContext */ - public function testGetContext($header_name, $header_value, $context) { + public function testGetContext($headers, $header_name, $context) { $request_stack = new RequestStack(); $request = Request::create('/', 'GET'); - if ($header_name) { - $request->headers->set($header_name, $header_value); - } + // Request defaults could change, so compare with default values instead of + // passed in context value. + $request->headers->replace($headers); $request_stack->push($request); $cache_context = new HeadersCacheContext($request_stack); - if (!$header_name) { - // Request defaults could change, so compare with default values instead - // of passed in context value. - $context = $request->headers->all(); - } - $this->assertSame($cache_context->getContext($header_name), $context); + $result = $cache_context->getContext($header_name); + file_put_contents('/home/andypost/www/core8/d.log', "result:\n" .var_export($result, TRUE) . "\nexpected:\n" . var_export($context, TRUE) . "\n", FILE_APPEND); + $this->assertSame($result, $context); } /** @@ -39,10 +36,18 @@ public function testGetContext($header_name, $header_value, $context) { */ public function providerTestGetContext() { return [ - [NULL, NULL, NULL], - ['x-custom-header', NULL, NULL], - ['x-custom-header', '123', '123'], - ['x-custom-header', ['123'], '123'], + [[], NULL, NULL], + [[], 'foo', NULL], + // Non-empty headers. + [['llama' => 'rocks', 'alpaca' => '', 'panda' => 'drools', 'z' => '0'], NULL, 'alpaca=&llama=rocks&panda=drools&z=0'], + [['llama' => 'rocks', 'alpaca' => '', 'panda' => 'drools', 'z' => '0'], 'llama', 'rocks'], + [['llama' => 'rocks', 'alpaca' => '', 'panda' => 'drools', 'z' => '0'], 'alpaca', '?valueless?'], + [['llama' => 'rocks', 'alpaca' => '', 'panda' => 'drools', 'z' => '0'], 'panda', 'drools'], + [['llama' => 'rocks', 'alpaca' => '', 'panda' => 'drools', 'z' => '0'], 'z', '0'], + [['llama' => 'rocks', 'alpaca' => '', 'panda' => 'drools', 'z' => '0'], 'chicken', NULL], + // Header value could be an array. + [['z' => ['0', '1']], NULL, 'z=0,1'], + [['z' => ['0', '1'], 'a' => []], NULL, 'a=&z=0,1'], ]; }