Skip to content

[HttpFoundation] Add HeaderRequestMatcher #51343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2024
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
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 <fabien@symfony.com>
*
* 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 <alex.daubois@gmail.com>
*/
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 <fabien@symfony.com>
*
* 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];
}
}