diff --git a/src/Illuminate/Http/Concerns/InteractsWithContentTypes.php b/src/Illuminate/Http/Concerns/InteractsWithContentTypes.php index 492c131b26f0..9ddb3c3b5cb2 100644 --- a/src/Illuminate/Http/Concerns/InteractsWithContentTypes.php +++ b/src/Illuminate/Http/Concerns/InteractsWithContentTypes.php @@ -41,7 +41,19 @@ public function isJson() */ public function expectsJson() { - return ($this->ajax() && ! $this->pjax()) || $this->wantsJson(); + return ($this->ajax() && ! $this->pjax() && $this->wantsAnyContentType()) || $this->wantsJson(); + } + + /** + * Determine if the current request is asking for any content type in return. + * + * @return bool + */ + public function wantsAnyContentType() + { + $acceptable = $this->getAcceptableContentTypes(); + + return isset($acceptable[0]) && ($acceptable[0] === '*/*' || $acceptable[0] === '*'); } /** diff --git a/tests/Http/HttpRequestTest.php b/tests/Http/HttpRequestTest.php index 01f03453f8e2..ec21b937ae35 100644 --- a/tests/Http/HttpRequestTest.php +++ b/tests/Http/HttpRequestTest.php @@ -583,6 +583,30 @@ public function testFlushMethodCallsSession() $request->flush(); } + public function testExpectsJson() + { + $request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'application/json']); + $this->assertTrue($request->expectsJson()); + + $request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => '*/*']); + $this->assertFalse($request->expectsJson()); + + $request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => '*/*', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); + $this->assertTrue($request->expectsJson()); + + $request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => '*/*', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest', 'HTTP_X_PJAX' => 'true']); + $this->assertFalse($request->expectsJson()); + + $request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'text/html']); + $this->assertFalse($request->expectsJson()); + + $request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'text/html', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); + $this->assertFalse($request->expectsJson()); + + $request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'text/html', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest', 'HTTP_X_PJAX' => 'true']); + $this->assertFalse($request->expectsJson()); + } + public function testFormatReturnsAcceptableFormat() { $request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'application/json']);