Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2366707

Browse files
[HttpFoundation] Add HeaderRequestMatcher
1 parent c005258 commit 2366707

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

src/Symfony/Component/HttpFoundation/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Make `HeaderBag::getDate()`, `Response::getDate()`, `getExpires()` and `getLastModified()` return a `DateTimeImmutable`
8+
* Add `HeaderRequestMatcher`
89

910
6.3
1011
---
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\RequestMatcher;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
16+
17+
/**
18+
* Checks the presence of HTTP headers in a Request.
19+
*
20+
* @author Alexandre Daubois <[email protected]>
21+
*/
22+
class HeaderRequestMatcher implements RequestMatcherInterface
23+
{
24+
/**
25+
* @var string[]
26+
*/
27+
private array $headers;
28+
29+
/**
30+
* @param string[]|string $headers A header or a list of headers
31+
* Strings can contain a comma-delimited list of headers
32+
*/
33+
public function __construct(array|string $headers)
34+
{
35+
$this->headers = array_reduce(array_map(strtolower(...), (array) $headers), static fn (array $headers, string $header) => array_merge($headers, preg_split('/\s*,\s*/', $header)), []);
36+
}
37+
38+
public function matches(Request $request): bool
39+
{
40+
if (!$this->headers) {
41+
return true;
42+
}
43+
44+
foreach ($this->headers as $header) {
45+
if (!$request->headers->has($header)) {
46+
return false;
47+
}
48+
}
49+
50+
return true;
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 test(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 getDataForSingleString
37+
*/
38+
public function testSingleString(array $headers, bool $matches)
39+
{
40+
$matcher = new HeaderRequestMatcher('x-foo');
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+
public static function getDataForArray(): \Generator
51+
{
52+
yield 'Superfluous header' => [['X-Foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], true];
53+
yield 'Exact match' => [['X-Foo' => 'foo', 'bar' => 'bar'], true];
54+
yield 'Case insensitivity' => [['x-foo' => 'foo', 'BAR' => 'bar'], true];
55+
yield 'Only one header matching' => [['bar' => 'bar', 'baz' => 'baz'], false];
56+
yield 'Only one header' => [['X-foo' => 'foo'], false];
57+
yield 'Header name as a value' => [['X-foo'], false];
58+
yield 'Empty headers' => [[], false];
59+
}
60+
61+
public static function getDataForSingleString(): \Generator
62+
{
63+
yield 'Superfluous header' => [['X-Foo' => 'foo', 'bar' => 'bar'], true];
64+
yield 'Exact match' => [['X-foo' => 'foo'], true];
65+
yield 'Case insensitivity' => [['x-foo' => 'foo'], true];
66+
yield 'Header name as a value' => [['X-foo'], false];
67+
yield 'Empty headers' => [[], false];
68+
}
69+
}

0 commit comments

Comments
 (0)