Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Fix expectsJson returning true when expecting html #22459

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/Illuminate/Http/Concerns/InteractsWithContentTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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] === '*');
}

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/Http/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down