Skip to content

Commit

Permalink
Merge branch 'hotfix/5'
Browse files Browse the repository at this point in the history
Close #5
  • Loading branch information
weierophinney committed Jun 13, 2017
2 parents 4f2375d + 7b61657 commit 9aaa282
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 0.2.1 - 2017-06-13

### Added

- Nothing.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- [#5](https://github.com/weierophinney/problem-details/pull/5) updates the
response factory and middleware to treat lack of/empty `Accept` header values
as `*/*`, per RFC-7231 section 5.3.2.

## 0.2.0 - 2017-05-30

### Added
Expand Down
2 changes: 1 addition & 1 deletion src/ProblemDetailsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function process(ServerRequestInterface $request, DelegateInterface $dele

private function canActAsErrorHandler(ServerRequestInterface $request) : bool
{
$accept = $request->getHeaderLine('Accept');
$accept = $request->getHeaderLine('Accept') ?: '*/*';
if (empty($accept)) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ProblemDetailsResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ private function generateStream() : StreamInterface

private function getResponseGenerator(ServerRequestInterface $request) : callable
{
$accept = $request->getHeaderLine('Accept', 'application/xhtml+xml');
$accept = $request->getHeaderLine('Accept') ?: '*/*';
$mediaType = (new Negotiator())->getBest($accept, self::NEGOTIATION_PRIORITIES);

if (! $mediaType) {
Expand Down
1 change: 1 addition & 0 deletions test/ProblemDetailsMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ protected function setUp()
public function acceptHeaders()
{
return [
'empty' => [''],
'application/xml' => ['application/xml'],
'application/vnd.api+xml' => ['application/vnd.api+xml'],
'application/json' => ['application/json'],
Expand Down
15 changes: 8 additions & 7 deletions test/ProblemDetailsResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ protected function setUp()
public function acceptHeaders()
{
return [
'empty' => ['', 'application/problem+json'],
'application/xml' => ['application/xml', 'application/problem+xml'],
'application/vnd.api+xml' => ['application/vnd.api+xml', 'application/problem+xml'],
'application/json' => ['application/json', 'application/problem+json'],
Expand All @@ -36,7 +37,7 @@ public function acceptHeaders()
*/
public function testCreateResponseCreatesExpectedType(string $header, string $expectedType)
{
$this->request->getHeaderLine('Accept', 'application/xhtml+xml')->willReturn($header);
$this->request->getHeaderLine('Accept')->willReturn($header);

$response = $this->factory->createResponse(
$this->request->reveal(),
Expand All @@ -53,7 +54,7 @@ public function testCreateResponseCreatesExpectedType(string $header, string $ex
*/
public function testCreateResponseFromThrowableCreatesExpectedType(string $header, string $expectedType)
{
$this->request->getHeaderLine('Accept', 'application/xhtml+xml')->willReturn($header);
$this->request->getHeaderLine('Accept')->willReturn($header);

$exception = new RuntimeException();
$response = $this->factory->createResponseFromThrowable(
Expand All @@ -72,7 +73,7 @@ public function testCreateResponseFromThrowableCreatesExpectedTypeWithExtraInfor
string $header,
string $expectedType
) {
$this->request->getHeaderLine('Accept', 'application/xhtml+xml')->willReturn($header);
$this->request->getHeaderLine('Accept')->willReturn($header);

$factory = new ProblemDetailsResponseFactory(ProblemDetailsResponseFactory::INCLUDE_THROWABLE_DETAILS);

Expand All @@ -98,7 +99,7 @@ public function testCreateResponseFromThrowableWillPullDetailsFromProblemDetails
$e->getType()->willReturn('https://example.com/api/doc/invalid-client-request');
$e->getAdditionalData()->willReturn(['foo' => 'bar']);

$this->request->getHeaderLine('Accept', 'application/xhtml+xml')->willReturn('application/json');
$this->request->getHeaderLine('Accept')->willReturn('application/json');

$factory = new ProblemDetailsResponseFactory();

Expand All @@ -121,7 +122,7 @@ public function testCreateResponseFromThrowableWillPullDetailsFromProblemDetails

public function testFactoryRaisesExceptionIfBodyFactoryDoesNotReturnStream()
{
$this->request->getHeaderLine('Accept', 'application/xhtml+xml')->willReturn('application/json');
$this->request->getHeaderLine('Accept')->willReturn('application/json');

$factory = new ProblemDetailsResponseFactory(false, null, null, function () {
return null;
Expand All @@ -133,7 +134,7 @@ public function testFactoryRaisesExceptionIfBodyFactoryDoesNotReturnStream()

public function testFactoryGeneratesXmlResponseIfNegotiationFails()
{
$this->request->getHeaderLine('Accept', 'application/xhtml+xml')->willReturn('text/plain');
$this->request->getHeaderLine('Accept')->willReturn('text/plain');

$response = $this->factory->createResponse(
$this->request->reveal(),
Expand All @@ -147,7 +148,7 @@ public function testFactoryGeneratesXmlResponseIfNegotiationFails()

public function testFactoryRendersPreviousExceptionsInDebugMode()
{
$this->request->getHeaderLine('Accept', 'application/xhtml+xml')->willReturn('application/json');
$this->request->getHeaderLine('Accept')->willReturn('application/json');

$first = new RuntimeException('first', 101010);
$second = new RuntimeException('second', 101011, $first);
Expand Down

0 comments on commit 9aaa282

Please sign in to comment.