Skip to content
Open
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
26 changes: 26 additions & 0 deletions src/Illuminate/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
*/
protected $routeResolver;

/**
* The cached Accept header value used to detect changes.
*
* @var string|null
*/
protected $cachedAcceptHeader;

/**
* Create a new Illuminate HTTP request from server variables.
*
Expand Down Expand Up @@ -812,4 +819,23 @@ public function __get($key)
{
return Arr::get($this->all(), $key, fn () => $this->route($key));
}

/**
* {@inheritdoc}
*
* Override to clear cache when Accept header changes.
*/
#[\Override]
public function getAcceptableContentTypes(): array
{
$currentAcceptHeader = $this->headers->get('Accept');

if ($this->cachedAcceptHeader !== $currentAcceptHeader) {
$this->acceptableContentTypes = null;
}

$this->cachedAcceptHeader = $currentAcceptHeader;

return parent::getAcceptableContentTypes();
}
}
73 changes: 73 additions & 0 deletions tests/Http/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,79 @@ public function testFormatReturnsAcceptsCharset()
$this->assertTrue($request->accepts('application/baz+json'));
}

public function testWantsJsonRespectsHeaderChanges()
{
$request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => '*/*']);

$this->assertFalse($request->wantsJson());

$this->assertTrue($request->acceptsAnyContentType());

$request->headers->set('Accept', 'application/json');

$this->assertTrue($request->wantsJson(), 'wantsJson() should return true after Accept header is changed to application/json');
}

public function testAcceptsJsonRespectsHeaderChanges()
{
$request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => '*/*']);

$this->assertTrue($request->acceptsAnyContentType());

$request->headers->set('Accept', 'application/json');

$this->assertTrue($request->acceptsJson(), 'acceptsJson() should return true after Accept header is changed to application/json');
}

public function testPrefersRespectsHeaderChanges()
{
$request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => '*/*']);

$this->assertTrue($request->acceptsAnyContentType());

$request->headers->set('Accept', 'application/json');

$this->assertSame('json', $request->prefers(['html', 'json']), 'prefers() should return json after Accept header is changed to application/json');
}

public function testWantsJsonWorksWhenHeaderSetBeforeFirstCall()
{
$request = Request::create('/', 'GET', [], [], [], []);

$request->headers->set('Accept', 'application/json');

$this->assertTrue($request->wantsJson(), 'wantsJson() should return true when Accept header is set to application/json');
}

public function testCacheClearedWhenTransitioningFromUnsetToSetHeader()
{
$request = Request::create('/', 'GET', [], [], [], []);

$request->getAcceptableContentTypes();

$request->headers->set('Accept', 'application/json');

$this->assertTrue($request->wantsJson(), 'wantsJson() should return true after Accept header is set from null to application/json');

$this->assertTrue($request->acceptsJson(), 'acceptsJson() should return true after Accept header is set from null to application/json');
}

public function testAcceptsJsonWorksWhenHeaderChangedMultipleTimes()
{
$request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'text/html']);

$this->assertFalse($request->acceptsJson());

$request->headers->set('Accept', 'application/json');
$this->assertTrue($request->acceptsJson());

$request->headers->set('Accept', 'text/html');
$this->assertFalse($request->acceptsJson());

$request->headers->set('Accept', 'application/json');
$this->assertTrue($request->acceptsJson());
}

public function testBadAcceptHeader()
{
$request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; pt-PT; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)']);
Expand Down