Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 0706a85

Browse files
committed
Treat empty/missing Accept header as "*/*"
Per [the Accept header specification](https://tools.ietf.org/html/rfc7231#section-5.3.2), a missing `Accept` header is equivalent to `*/*`.
1 parent 4f2375d commit 0706a85

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

src/ProblemDetailsMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function process(ServerRequestInterface $request, DelegateInterface $dele
5454

5555
private function canActAsErrorHandler(ServerRequestInterface $request) : bool
5656
{
57-
$accept = $request->getHeaderLine('Accept');
57+
$accept = $request->getHeaderLine('Accept') ?: '*/*';
5858
if (empty($accept)) {
5959
return false;
6060
}

src/ProblemDetailsResponseFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ private function generateStream() : StreamInterface
285285

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

291291
if (! $mediaType) {

test/ProblemDetailsMiddlewareTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ protected function setUp()
2626
public function acceptHeaders()
2727
{
2828
return [
29+
'empty' => [''],
2930
'application/xml' => ['application/xml'],
3031
'application/vnd.api+xml' => ['application/vnd.api+xml'],
3132
'application/json' => ['application/json'],

test/ProblemDetailsResponseFactoryTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ protected function setUp()
2424
public function acceptHeaders()
2525
{
2626
return [
27+
'empty' => ['', 'application/problem+json'],
2728
'application/xml' => ['application/xml', 'application/problem+xml'],
2829
'application/vnd.api+xml' => ['application/vnd.api+xml', 'application/problem+xml'],
2930
'application/json' => ['application/json', 'application/problem+json'],
@@ -36,7 +37,7 @@ public function acceptHeaders()
3637
*/
3738
public function testCreateResponseCreatesExpectedType(string $header, string $expectedType)
3839
{
39-
$this->request->getHeaderLine('Accept', 'application/xhtml+xml')->willReturn($header);
40+
$this->request->getHeaderLine('Accept')->willReturn($header);
4041

4142
$response = $this->factory->createResponse(
4243
$this->request->reveal(),
@@ -53,7 +54,7 @@ public function testCreateResponseCreatesExpectedType(string $header, string $ex
5354
*/
5455
public function testCreateResponseFromThrowableCreatesExpectedType(string $header, string $expectedType)
5556
{
56-
$this->request->getHeaderLine('Accept', 'application/xhtml+xml')->willReturn($header);
57+
$this->request->getHeaderLine('Accept')->willReturn($header);
5758

5859
$exception = new RuntimeException();
5960
$response = $this->factory->createResponseFromThrowable(
@@ -72,7 +73,7 @@ public function testCreateResponseFromThrowableCreatesExpectedTypeWithExtraInfor
7273
string $header,
7374
string $expectedType
7475
) {
75-
$this->request->getHeaderLine('Accept', 'application/xhtml+xml')->willReturn($header);
76+
$this->request->getHeaderLine('Accept')->willReturn($header);
7677

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

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

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

103104
$factory = new ProblemDetailsResponseFactory();
104105

@@ -121,7 +122,7 @@ public function testCreateResponseFromThrowableWillPullDetailsFromProblemDetails
121122

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

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

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

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

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

152153
$first = new RuntimeException('first', 101010);
153154
$second = new RuntimeException('second', 101011, $first);

0 commit comments

Comments
 (0)