3 namespace Drupal\Tests\Core\Cache\Context;
5 use Drupal\Core\Cache\Context\HeadersCacheContext;
6 use Drupal\Tests\UnitTestCase;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpFoundation\RequestStack;
11 * @coversDefaultClass \Drupal\Core\Cache\Context\HeadersCacheContext
14 class HeadersCacheContextTest extends UnitTestCase {
17 * @covers ::getContext
19 * @dataProvider providerTestGetContext
21 public function testGetContext($headers, $header_name, $context) {
22 $request_stack = new RequestStack();
23 $request = Request::create('/', 'GET');
24 // Request defaults could change, so compare with default values instead of
25 // passed in context value.
26 $request->headers->replace($headers);
27 $request_stack->push($request);
28 $cache_context = new HeadersCacheContext($request_stack);
29 $this->assertSame($cache_context->getContext($header_name), $context);
33 * Provides a list of headers and expected cache contexts.
35 public function providerTestGetContext() {
40 [['llama' => 'rocks', 'alpaca' => '', 'panda' => 'drools', 'z' => '0'], NULL, 'alpaca=&llama=rocks&panda=drools&z=0'],
41 [['llama' => 'rocks', 'alpaca' => '', 'panda' => 'drools', 'z' => '0'], 'llama', 'rocks'],
42 [['llama' => 'rocks', 'alpaca' => '', 'panda' => 'drools', 'z' => '0'], 'alpaca', '?valueless?'],
43 [['llama' => 'rocks', 'alpaca' => '', 'panda' => 'drools', 'z' => '0'], 'panda', 'drools'],
44 [['llama' => 'rocks', 'alpaca' => '', 'panda' => 'drools', 'z' => '0'], 'z', '0'],
45 [['llama' => 'rocks', 'alpaca' => '', 'panda' => 'drools', 'z' => '0'], 'chicken', ''],
46 // Header value could be an array.
47 [['z' => ['0', '1']], NULL, 'z=0,1'],
48 // Values are sorted to minimize cache variations.
49 [['z' => ['1', '0'], 'a' => []], NULL, 'a=&z=0,1'],
50 [['a' => [], 'z' => ['1', '0']], NULL, 'a=&z=0,1'],