Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit65e3323

Browse files
[HttpFoundation] AddHeaderRequestMatcher
1 parentc005258 commit65e3323

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

‎src/Symfony/Component/HttpFoundation/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
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
---
Lines changed: 52 additions & 0 deletions
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 <fabien@symfony.com>
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+
namespaceSymfony\Component\HttpFoundation\RequestMatcher;
13+
14+
useSymfony\Component\HttpFoundation\Request;
15+
useSymfony\Component\HttpFoundation\RequestMatcherInterface;
16+
17+
/**
18+
* Checks the presence of HTTP headers in a Request.
19+
*
20+
* @author Alexandre Daubois <alex.daubois@gmail.com>
21+
*/
22+
class HeaderRequestMatcherimplements RequestMatcherInterface
23+
{
24+
/**
25+
* @var string[]
26+
*/
27+
privatearray$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+
publicfunction__construct(array|string$headers)
34+
{
35+
$this->headers =array_reduce((array)$headers,staticfn (array$headers,string$header) =>array_merge($headers,preg_split('/\s*,\s*/',$header)), []);
36+
}
37+
38+
publicfunctionmatches(Request$request):bool
39+
{
40+
if (!$this->headers) {
41+
returntrue;
42+
}
43+
44+
foreach ($this->headersas$header) {
45+
if (!$request->headers->has($header)) {
46+
returnfalse;
47+
}
48+
}
49+
50+
returntrue;
51+
}
52+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
namespaceSymfony\Component\HttpFoundation\Tests\RequestMatcher;
13+
14+
usePHPUnit\Framework\TestCase;
15+
useSymfony\Component\HttpFoundation\Request;
16+
useSymfony\Component\HttpFoundation\RequestMatcher\HeaderRequestMatcher;
17+
18+
class HeaderRequestMatcherTestextends TestCase
19+
{
20+
/**
21+
* @dataProvider getDataForArray
22+
*/
23+
publicfunctiontestArray(array$headers,bool$matches)
24+
{
25+
$matcher =newHeaderRequestMatcher(['x-foo','bar']);
26+
27+
$request = Request::create('https://example.com');
28+
foreach ($headersas$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+
publicfunctiontestCommaSeparatedString(array$headers,bool$matches)
39+
{
40+
$matcher =newHeaderRequestMatcher('x-foo, bar');
41+
42+
$request = Request::create('https://example.com');
43+
foreach ($headersas$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+
publicfunctiontestSingleString(array$headers,bool$matches)
54+
{
55+
$matcher =newHeaderRequestMatcher('x-foo');
56+
57+
$request = Request::create('https://example.com');
58+
foreach ($headersas$k =>$v) {
59+
$request->headers->set($k,$v);
60+
}
61+
62+
$this->assertSame($matches,$matcher->matches($request));
63+
}
64+
65+
publicstaticfunctiongetDataForArray():\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+
publicstaticfunctiongetDataForSingleString():\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

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp