Skip to content

Commit

Permalink
Merge branch 'patch-expects-json' of https://github.com/jn-jairo/fram…
Browse files Browse the repository at this point in the history
…ework into jn-jairo-patch-expects-json
  • Loading branch information
taylorotwell committed Dec 29, 2017
2 parents 6439a38 + 3dc1dfe commit 7c523f1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Illuminate/Http/Concerns/InteractsWithContentTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,23 @@ 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();

if (count($acceptable) === 0) {
return true;
}

return isset($acceptable[0]) && ($acceptable[0] === '*/*' || $acceptable[0] === '*');
}

/**
Expand Down
27 changes: 27 additions & 0 deletions tests/Http/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,33 @@ 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' => 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());

$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

0 comments on commit 7c523f1

Please sign in to comment.