Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.
Closed
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
5 changes: 5 additions & 0 deletions src/ApiProblem.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
*/
class ApiProblem
{
/**
* Content type for api problem response
*/
const CONTENT_TYPE = 'application/problem+json';

/**
* Additional details to include in report
*
Expand Down
2 changes: 1 addition & 1 deletion src/ApiProblemResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Listener/RenderErrorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Zend\EventManager\AbstractListenerAggregate;
use Zend\Mvc\MvcEvent;
use Zend\View\Exception\ExceptionInterface as ViewExceptionInterface;
use ZF\ApiProblem\ApiProblem;

/**
* RenderErrorListener
Expand Down Expand Up @@ -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));

Expand Down
2 changes: 1 addition & 1 deletion src/View/ApiProblemStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion test/ApiProblemResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion test/Listener/RenderErrorListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/View/ApiProblemStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down