Skip to content

Commit d49c8d1

Browse files
committed
Merge branch 'feature/39' into develop
Close #39
2 parents 587dd9b + 746af0c commit d49c8d1

File tree

8 files changed

+16
-6
lines changed

8 files changed

+16
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ All notable changes to this project will be documented in this file, in reverse
99
- [#45](https://github.com/zfcampus/zf-api-problem/pull/45) adds support for PHP 7.
1010
- [#45](https://github.com/zfcampus/zf-api-problem/pull/45) adds support for
1111
version 3 components of Zend Framework.
12+
- [#39](https://github.com/zfcampus/zf-api-problem/pull/39) adds the constant
13+
`ApiProblem::CONTENT_TYPE` for specifying the Content-Type of API Problem
14+
responses.
1215

1316
### Deprecated
1417

src/ApiProblem.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
*/
1212
class ApiProblem
1313
{
14+
/**
15+
* Content type for api problem response
16+
*/
17+
const CONTENT_TYPE = 'application/problem+json';
18+
1419
/**
1520
* Additional details to include in report
1621
*

src/ApiProblemResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function getHeaders()
6969
{
7070
$headers = parent::getHeaders();
7171
if (!$headers->has('content-type')) {
72-
$headers->addHeaderLine('content-type', 'application/problem+json');
72+
$headers->addHeaderLine('content-type', ApiProblem::CONTENT_TYPE);
7373
}
7474
return $headers;
7575
}

src/Listener/RenderErrorListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Zend\EventManager\AbstractListenerAggregate;
1111
use Zend\Mvc\MvcEvent;
1212
use Zend\View\Exception\ExceptionInterface as ViewExceptionInterface;
13+
use ZF\ApiProblem\ApiProblem;
1314

1415
/**
1516
* RenderErrorListener
@@ -89,7 +90,7 @@ public function onRenderError(MvcEvent $e)
8990
$payload['details'] = $details;
9091
}
9192

92-
$response->getHeaders()->addHeaderLine('content-type', 'application/problem+json');
93+
$response->getHeaders()->addHeaderLine('content-type', ApiProblem::CONTENT_TYPE);
9394
$response->setStatusCode($status);
9495
$response->setContent(json_encode($payload));
9596

src/View/ApiProblemStrategy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function injectResponse(ViewEvent $e)
7272

7373
$problem = $model->getApiProblem();
7474
$statusCode = $this->getStatusCodeFromApiProblem($problem);
75-
$contentType = 'application/problem+json';
75+
$contentType = ApiProblem::CONTENT_TYPE;
7676

7777
// Populate response
7878
$response = $e->getResponse();

test/ApiProblemResponseTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function testApiProblemResponseSetsContentTypeHeader()
5959
$this->assertTrue($headers->has('content-type'));
6060
$header = $headers->get('content-type');
6161
$this->assertInstanceOf('Zend\Http\Header\ContentType', $header);
62-
$this->assertEquals('application/problem+json', $header->getFieldValue());
62+
$this->assertEquals(ApiProblem::CONTENT_TYPE, $header->getFieldValue());
6363
}
6464

6565
public function testComposeApiProblemIsAccessible()

test/Listener/RenderErrorListenerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Zend\Http\Response;
1212
use Zend\Mvc\Application;
1313
use Zend\Mvc\MvcEvent;
14+
use ZF\ApiProblem\ApiProblem;
1415
use ZF\ApiProblem\Listener\RenderErrorListener;
1516

1617
class RenderErrorListenerTest extends TestCase
@@ -43,7 +44,7 @@ public function testOnRenderErrorCreatesAnApiProblemResponse()
4344
$this->assertEquals(406, $response->getStatusCode());
4445
$headers = $response->getHeaders();
4546
$this->assertTrue($headers->has('Content-Type'));
46-
$this->assertEquals('application/problem+json', $headers->get('content-type')->getFieldValue());
47+
$this->assertEquals(ApiProblem::CONTENT_TYPE, $headers->get('content-type')->getFieldValue());
4748
$content = json_decode($response->getContent(), true);
4849
$this->assertArrayHasKey('status', $content);
4950
$this->assertArrayHasKey('title', $content);

test/View/ApiProblemStrategyTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function testInjectResponseSetsContentTypeHeaderToApiProblemForApiProblem
7878
$headers = $this->response->getHeaders();
7979
$this->assertTrue($headers->has('Content-Type'));
8080
$header = $headers->get('Content-Type');
81-
$this->assertEquals('application/problem+json', $header->getFieldValue());
81+
$this->assertEquals(ApiProblem::CONTENT_TYPE, $header->getFieldValue());
8282
}
8383

8484
public function invalidStatusCodes()

0 commit comments

Comments
 (0)