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

Commit105f0c8

Browse files
[HttpFoundation] AddHeaderRequestMatcher
1 parentc005258 commit105f0c8

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-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 HTTP headers of 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_map(strtolower(...), (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: 69 additions & 0 deletions
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 <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+
publicfunctiontest(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 getDataForSingleString
37+
*/
38+
publicfunctiontestSingleString(array$headers,bool$matches)
39+
{
40+
$matcher =newHeaderRequestMatcher('x-foo');
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+
publicstaticfunctiongetDataForArray():\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+
publicstaticfunctiongetDataForSingleString():\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)

[8]ページ先頭

©2009-2025 Movatter.jp