diff --git a/Controller/Annotations/HeaderParam.php b/Controller/Annotations/HeaderParam.php new file mode 100644 index 000000000..c384fbc67 --- /dev/null +++ b/Controller/Annotations/HeaderParam.php @@ -0,0 +1,36 @@ + + * + * 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 + */ +class HeaderParam extends AbstractScalarParam +{ + /** @var bool */ + public $strict = true; + + /** + * {@inheritdoc} + */ + public function getValue(Request $request, $default = null) + { + return $request->headers->get($this->getKey(), $default); + } +} diff --git a/Tests/Controller/Annotations/HeaderParamTest.php b/Tests/Controller/Annotations/HeaderParamTest.php new file mode 100644 index 000000000..af1bf477c --- /dev/null +++ b/Tests/Controller/Annotations/HeaderParamTest.php @@ -0,0 +1,53 @@ + + * + * 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 + */ +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')); + } +}