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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[HttpFoundation] Add HeaderRequestMatcher
  • Loading branch information
alexandre-daubois authored and fabpot committed Feb 3, 2024
commit 62b5a34a69f624e668fe1b46fe6dbdf253f3f235
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Add `UploadedFile::getClientOriginalPath()`
* Add `QueryParameterRequestMatcher`
* Add `HeaderRequestMatcher`

7.0
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpFoundation\RequestMatcher;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcherInterface;

/**
* Checks the presence of HTTP headers in a Request.
*
* @author Alexandre Daubois <[email protected]>
*/
class HeaderRequestMatcher implements RequestMatcherInterface
{
/**
* @var string[]
*/
private array $headers;

/**
* @param string[]|string $headers A header or a list of headers
* Strings can contain a comma-delimited list of headers
*/
public function __construct(array|string $headers)
{
$this->headers = array_reduce((array) $headers, static fn (array $headers, string $header) => array_merge($headers, preg_split('/\s*,\s*/', $header)), []);
}

public function matches(Request $request): bool
{
if (!$this->headers) {
return true;
}

foreach ($this->headers as $header) {
if (!$request->headers->has($header)) {
return false;
}
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpFoundation\Tests\RequestMatcher;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcher\HeaderRequestMatcher;

class HeaderRequestMatcherTest extends TestCase
{
/**
* @dataProvider getDataForArray
*/
public function testArray(array $headers, bool $matches)
{
$matcher = new HeaderRequestMatcher(['x-foo', 'bar']);

$request = Request::create('https://example.com');
foreach ($headers as $k => $v) {
$request->headers->set($k, $v);
}

$this->assertSame($matches, $matcher->matches($request));
}

/**
* @dataProvider getDataForArray
*/
public function testCommaSeparatedString(array $headers, bool $matches)
{
$matcher = new HeaderRequestMatcher('x-foo, bar');

$request = Request::create('https://example.com');
foreach ($headers as $k => $v) {
$request->headers->set($k, $v);
}

$this->assertSame($matches, $matcher->matches($request));
}

/**
* @dataProvider getDataForSingleString
*/
public function testSingleString(array $headers, bool $matches)
{
$matcher = new HeaderRequestMatcher('x-foo');

$request = Request::create('https://example.com');
foreach ($headers as $k => $v) {
$request->headers->set($k, $v);
}

$this->assertSame($matches, $matcher->matches($request));
}

public static function getDataForArray(): \Generator
{
yield 'Superfluous header' => [['X-Foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], true];
yield 'Exact match' => [['X-Foo' => 'foo', 'bar' => 'bar'], true];
yield 'Case insensitivity' => [['x-foo' => 'foo', 'BAR' => 'bar'], true];
yield 'Only one header matching' => [['bar' => 'bar', 'baz' => 'baz'], false];
yield 'Only one header' => [['X-foo' => 'foo'], false];
yield 'Header name as a value' => [['X-foo'], false];
yield 'Empty headers' => [[], false];
}

public static function getDataForSingleString(): \Generator
{
yield 'Superfluous header' => [['X-Foo' => 'foo', 'bar' => 'bar'], true];
yield 'Exact match' => [['X-foo' => 'foo'], true];
yield 'Case insensitivity' => [['x-foo' => 'foo'], true];
yield 'Header name as a value' => [['X-foo'], false];
yield 'Empty headers' => [[], false];
}
}