diff --git a/src/ApiProblem.php b/src/ApiProblem.php index 428101a..40126e2 100644 --- a/src/ApiProblem.php +++ b/src/ApiProblem.php @@ -11,6 +11,11 @@ */ class ApiProblem { + /** + * Content type for api problem response + */ + const CONTENT_TYPE = 'application/problem+json'; + /** * Additional details to include in report * diff --git a/src/ApiProblemResponse.php b/src/ApiProblemResponse.php index d6a04c9..8932d97 100644 --- a/src/ApiProblemResponse.php +++ b/src/ApiProblemResponse.php @@ -71,7 +71,7 @@ public function getHeaders() { $headers = parent::getHeaders(); if (!$headers->has('content-type')) { - $headers->addHeaderLine('content-type', 'application/problem+json'); + $headers->addHeaderLine('content-type', ApiProblem::CONTENT_TYPE); } return $headers; } diff --git a/src/Listener/RenderErrorListener.php b/src/Listener/RenderErrorListener.php index 577e613..cc459b3 100644 --- a/src/Listener/RenderErrorListener.php +++ b/src/Listener/RenderErrorListener.php @@ -10,6 +10,7 @@ use Zend\EventManager\AbstractListenerAggregate; use Zend\Mvc\MvcEvent; use Zend\View\Exception\ExceptionInterface as ViewExceptionInterface; +use ZF\ApiProblem\ApiProblem; /** * RenderErrorListener @@ -89,7 +90,7 @@ public function onRenderError(MvcEvent $e) $payload['details'] = $details; } - $response->getHeaders()->addHeaderLine('content-type', 'application/problem+json'); + $response->getHeaders()->addHeaderLine('content-type', ApiProblem::CONTENT_TYPE); $response->setStatusCode($status); $response->setContent(json_encode($payload)); diff --git a/src/View/ApiProblemStrategy.php b/src/View/ApiProblemStrategy.php index c1cfa6d..6550e30 100644 --- a/src/View/ApiProblemStrategy.php +++ b/src/View/ApiProblemStrategy.php @@ -72,7 +72,7 @@ public function injectResponse(ViewEvent $e) $problem = $model->getApiProblem(); $statusCode = $this->getStatusCodeFromApiProblem($problem); - $contentType = 'application/problem+json'; + $contentType = ApiProblem::CONTENT_TYPE; // Populate response $response = $e->getResponse(); diff --git a/test/ApiProblemResponseTest.php b/test/ApiProblemResponseTest.php index b587fb3..397c67e 100644 --- a/test/ApiProblemResponseTest.php +++ b/test/ApiProblemResponseTest.php @@ -47,7 +47,7 @@ public function testApiProblemResponseSetsContentTypeHeader() $this->assertTrue($headers->has('content-type')); $header = $headers->get('content-type'); $this->assertInstanceOf('Zend\Http\Header\ContentType', $header); - $this->assertEquals('application/problem+json', $header->getFieldValue()); + $this->assertEquals(ApiProblem::CONTENT_TYPE, $header->getFieldValue()); } public function testComposeApiProblemIsAccessible() diff --git a/test/Listener/RenderErrorListenerTest.php b/test/Listener/RenderErrorListenerTest.php index 23119d6..f689188 100644 --- a/test/Listener/RenderErrorListenerTest.php +++ b/test/Listener/RenderErrorListenerTest.php @@ -11,6 +11,7 @@ use Zend\Http\Response; use Zend\Mvc\Application; use Zend\Mvc\MvcEvent; +use ZF\ApiProblem\ApiProblem; use ZF\ApiProblem\Listener\RenderErrorListener; class RenderErrorListenerTest extends TestCase @@ -43,7 +44,7 @@ public function testOnRenderErrorCreatesAnApiProblemResponse() $this->assertEquals(406, $response->getStatusCode()); $headers = $response->getHeaders(); $this->assertTrue($headers->has('Content-Type')); - $this->assertEquals('application/problem+json', $headers->get('content-type')->getFieldValue()); + $this->assertEquals(ApiProblem::CONTENT_TYPE, $headers->get('content-type')->getFieldValue()); $content = json_decode($response->getContent(), true); $this->assertArrayHasKey('status', $content); $this->assertArrayHasKey('title', $content); diff --git a/test/View/ApiProblemStrategyTest.php b/test/View/ApiProblemStrategyTest.php index e927f09..9692c6f 100644 --- a/test/View/ApiProblemStrategyTest.php +++ b/test/View/ApiProblemStrategyTest.php @@ -78,7 +78,7 @@ public function testInjectResponseSetsContentTypeHeaderToApiProblemForApiProblem $headers = $this->response->getHeaders(); $this->assertTrue($headers->has('Content-Type')); $header = $headers->get('Content-Type'); - $this->assertEquals('application/problem+json', $header->getFieldValue()); + $this->assertEquals(ApiProblem::CONTENT_TYPE, $header->getFieldValue()); } public function invalidStatusCodes()