|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\HttpFoundation\Tests\RequestMatcher; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\HttpFoundation\Request; |
| 16 | +use Symfony\Component\HttpFoundation\RequestMatcher\HeaderRequestMatcher; |
| 17 | + |
| 18 | +class HeaderRequestMatcherTest extends TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @dataProvider getDataForArray |
| 22 | + */ |
| 23 | + public function testArray(array $headers, bool $matches) |
| 24 | + { |
| 25 | + $matcher = new HeaderRequestMatcher(['x-foo', 'bar']); |
| 26 | + |
| 27 | + $request = Request::create('https://example.com'); |
| 28 | + foreach ($headers as $k => $v) { |
| 29 | + $request->headers->set($k, $v); |
| 30 | + } |
| 31 | + |
| 32 | + $this->assertSame($matches, $matcher->matches($request)); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @dataProvider getDataForArray |
| 37 | + */ |
| 38 | + public function testCommaSeparatedString(array $headers, bool $matches) |
| 39 | + { |
| 40 | + $matcher = new HeaderRequestMatcher('x-foo, bar'); |
| 41 | + |
| 42 | + $request = Request::create('https://example.com'); |
| 43 | + foreach ($headers as $k => $v) { |
| 44 | + $request->headers->set($k, $v); |
| 45 | + } |
| 46 | + |
| 47 | + $this->assertSame($matches, $matcher->matches($request)); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @dataProvider getDataForSingleString |
| 52 | + */ |
| 53 | + public function testSingleString(array $headers, bool $matches) |
| 54 | + { |
| 55 | + $matcher = new HeaderRequestMatcher('x-foo'); |
| 56 | + |
| 57 | + $request = Request::create('https://example.com'); |
| 58 | + foreach ($headers as $k => $v) { |
| 59 | + $request->headers->set($k, $v); |
| 60 | + } |
| 61 | + |
| 62 | + $this->assertSame($matches, $matcher->matches($request)); |
| 63 | + } |
| 64 | + |
| 65 | + public static function getDataForArray(): \Generator |
| 66 | + { |
| 67 | + yield 'Superfluous header' => [['X-Foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], true]; |
| 68 | + yield 'Exact match' => [['X-Foo' => 'foo', 'bar' => 'bar'], true]; |
| 69 | + yield 'Case insensitivity' => [['x-foo' => 'foo', 'BAR' => 'bar'], true]; |
| 70 | + yield 'Only one header matching' => [['bar' => 'bar', 'baz' => 'baz'], false]; |
| 71 | + yield 'Only one header' => [['X-foo' => 'foo'], false]; |
| 72 | + yield 'Header name as a value' => [['X-foo'], false]; |
| 73 | + yield 'Empty headers' => [[], false]; |
| 74 | + } |
| 75 | + |
| 76 | + public static function getDataForSingleString(): \Generator |
| 77 | + { |
| 78 | + yield 'Superfluous header' => [['X-Foo' => 'foo', 'bar' => 'bar'], true]; |
| 79 | + yield 'Exact match' => [['X-foo' => 'foo'], true]; |
| 80 | + yield 'Case insensitivity' => [['x-foo' => 'foo'], true]; |
| 81 | + yield 'Header name as a value' => [['X-foo'], false]; |
| 82 | + yield 'Empty headers' => [[], false]; |
| 83 | + } |
| 84 | +} |
0 commit comments