From 1f6b32e960dfa5941d9978f1ca5752903e04128b Mon Sep 17 00:00:00 2001 From: Jairo Correa Date: Thu, 21 Dec 2017 12:08:46 -0200 Subject: [PATCH 1/2] Fix expectsJson returning true when explicitly expecting other content type --- .../Concerns/InteractsWithContentTypes.php | 14 ++++++++++- tests/Http/HttpRequestTest.php | 24 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) 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']); From 3dc1dfec80761f71989ec6c34460593d956b410c Mon Sep 17 00:00:00 2001 From: Jairo Correa Date: Tue, 26 Dec 2017 16:26:59 -0200 Subject: [PATCH 2/2] Treat empty Accept header like any content type --- src/Illuminate/Http/Concerns/InteractsWithContentTypes.php | 4 ++++ tests/Http/HttpRequestTest.php | 3 +++ 2 files changed, 7 insertions(+) diff --git a/src/Illuminate/Http/Concerns/InteractsWithContentTypes.php b/src/Illuminate/Http/Concerns/InteractsWithContentTypes.php index 9ddb3c3b5cb2..8037bd55ff6f 100644 --- a/src/Illuminate/Http/Concerns/InteractsWithContentTypes.php +++ b/src/Illuminate/Http/Concerns/InteractsWithContentTypes.php @@ -53,6 +53,10 @@ public function wantsAnyContentType() { $acceptable = $this->getAcceptableContentTypes(); + if (count($acceptable) === 0) { + return true; + } + return isset($acceptable[0]) && ($acceptable[0] === '*/*' || $acceptable[0] === '*'); } diff --git a/tests/Http/HttpRequestTest.php b/tests/Http/HttpRequestTest.php index ec21b937ae35..7642c2754322 100644 --- a/tests/Http/HttpRequestTest.php +++ b/tests/Http/HttpRequestTest.php @@ -594,6 +594,9 @@ public function testExpectsJson() $request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => '*/*', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); $this->assertTrue($request->expectsJson()); + $request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => null, '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());