Skip to content
Open
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
36 changes: 36 additions & 0 deletions Controller/Annotations/HeaderParam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\RestBundle\Controller\Annotations;

use Symfony\Component\HttpFoundation\Request;

/**
* Represents a parameter that must be present in header.
*
* @Annotation
* @Target("METHOD")
*
* @author Ilia Shcheglov <ilia.sheglov@gmail.com>
*/
class HeaderParam extends AbstractScalarParam
{
/** @var bool */
public $strict = true;

/**
* {@inheritdoc}
*/
public function getValue(Request $request, $default = null)
{
return $request->headers->get($this->getKey(), $default);
}
}
53 changes: 53 additions & 0 deletions Tests/Controller/Annotations/HeaderParamTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\RestBundle\Tests\Controller\Annotations;

use FOS\RestBundle\Controller\Annotations\AbstractScalarParam;
use FOS\RestBundle\Controller\Annotations\HeaderParam;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\HeaderBag;
use Symfony\Component\HttpFoundation\Request;

/**
* HeaderParamTest.
*
* @author Ilia Shcheglov <ilia.sheglov@gmail.com>
*/
final class HeaderParamTest extends TestCase
{
protected function setUp(): void
{
$this->param = $this->getMockBuilder(HeaderParam::class)
->setMethods(['getKey'])
->getMock();
}

public function testInterface()
{
self::assertInstanceOf(AbstractScalarParam::class, $this->param);
}

public function testValueGetter()
{
$this->param
->expects(self::once())
->method('getKey')
->willReturn('foo');

$request = $this->createMock(Request::class);
$headerBag = new HeaderBag();
$headerBag->set('foo', 'foobar');
$request->headers = $headerBag;

self::assertEquals('foobar', $this->param->getValue($request, 'bar'));
}
}