From 0a111bb2a81356b15a56b16b57f8a440ba8a65be Mon Sep 17 00:00:00 2001 From: Ivan Djurdjevac Date: Thu, 6 Oct 2016 13:32:30 +0000 Subject: [PATCH] Request Validator functional test for negotiate method --- src/JsonApi/Negotiation/MessageValidator.php | 4 +- src/JsonApi/Request/Request.php | 2 +- .../Negotiation/RequestValidatorTest.php | 124 ++++++++++++++++++ 3 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 tests/JsonApi/Negotiation/RequestValidatorTest.php diff --git a/src/JsonApi/Negotiation/MessageValidator.php b/src/JsonApi/Negotiation/MessageValidator.php index 07b077de..111a8e74 100644 --- a/src/JsonApi/Negotiation/MessageValidator.php +++ b/src/JsonApi/Negotiation/MessageValidator.php @@ -35,10 +35,10 @@ public function __construct(ExceptionFactoryInterface $exceptionFactory, $includ * @param string $message * @return string */ - public function lintMessage($message) + protected function lintMessage($message) { if (empty($message) === true) { - return null; + return ""; } try { diff --git a/src/JsonApi/Request/Request.php b/src/JsonApi/Request/Request.php index 8e737f54..a86c49d6 100644 --- a/src/JsonApi/Request/Request.php +++ b/src/JsonApi/Request/Request.php @@ -105,7 +105,7 @@ public function validateQueryParams() protected function isValidMediaTypeHeader($headerName) { $header = $this->getHeaderLine($headerName); - return (strpos($header, "application/vnd.api+json") === false || $header === "application/vnd.api+json"); + return strpos($header, "application/vnd.api+json") !== false; } protected function setIncludedFields() diff --git a/tests/JsonApi/Negotiation/RequestValidatorTest.php b/tests/JsonApi/Negotiation/RequestValidatorTest.php new file mode 100644 index 00000000..cffda793 --- /dev/null +++ b/tests/JsonApi/Negotiation/RequestValidatorTest.php @@ -0,0 +1,124 @@ +getMockForAbstractClass('\Psr\Http\Message\ServerRequestInterface'); + $exceptionFactory = $this->getMockForAbstractClass('\WoohooLabs\Yin\JsonApi\Exception\ExceptionFactoryInterface'); + + $request = $this->createRequestMock($server, $exceptionFactory); + + $request->expects($this->once()) + ->method('validateContentTypeHeader') + ->will($this->returnValue(true)); + ; + + $request->expects($this->once()) + ->method('validateAcceptHeader') + ->will($this->returnValue(true)); + ; + + $validator = $this->createRequestValidator($server); + + $validator->negotiate($request); + } + + + /** + * @test + * @dataProvider getInvalidContentTypes + * @expectedException \WoohooLabs\Yin\JsonApi\Exception\MediaTypeUnsupported + */ + public function negotiateTrowMediaTypeUnsupported($contentType) + { + // Content type is invalid Accept is valid + $server = $this->createServerRequest($contentType, 'application/vnd.api+json'); + + $request = $this->createRequest($server, $contentType); + $validator = $this->createRequestValidator($server); + + $validator->negotiate($request); + } + + /** + * @test + * @dataProvider getInvalidContentTypes + * @expectedException \WoohooLabs\Yin\JsonApi\Exception\MediaTypeUnacceptable + */ + public function negotiateThrowTypeUnacceptable($accept) + { + // Content Type is valid, Accept is invalid + $server = $this->createServerRequest('application/vnd.api+json', $accept); + + $request = $this->createRequest($server, 'application/vnd.api+json'); + $validator = $this->createRequestValidator($server); + + $validator->negotiate($request); + } + + public function createServerRequest($contentType, $accept = '') + { + $server = $this->getMockForAbstractClass('\Psr\Http\Message\ServerRequestInterface'); + + $map = array( + array('Content-Type', $contentType), + array('Accept', $accept) + ); + $server->expects($this->any()) + ->method('getHeaderLine') + ->will($this->returnValueMap($map)); + + + return $server; + } + + + public function createRequest($server, $contentType) + { + $exceptionInterface = new DefaultExceptionFactory($server); + + $request = new Request($server, $exceptionInterface); + + return $request; + } + + protected function createRequestMock($server, $exceptionFactory) + { + return $this->getMockForAbstractClass('\WoohooLabs\Yin\JsonApi\Request\RequestInterface', [$server, $exceptionFactory]); + } + + + private function createRequestValidator($server, $includeOriginalMessageResponse = true) + { + $exceptionInterface = new DefaultExceptionFactory($server); + return new RequestValidator($exceptionInterface, $includeOriginalMessageResponse); + } + + public function getInvalidContentTypes() + { + return [ + ['application/zip'], + ['application/octet-stream'], + ['application/ms-word'], + ['application/json'], + ['application/x-javascript'], + ]; + } + + public function getValidContentTypes() + { + return [ + ['application/vnd.api+json'] + ]; + } +}