Skip to content

Commit

Permalink
[8.x] Make accept header comparison case-insensitive (laravel#39413)
Browse files Browse the repository at this point in the history
* Make accept header comparison case-insensitive

* Update comment and ref link

* Update InteractsWithContentTypes.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
2 people authored and chu121su12 committed Oct 30, 2021
1 parent a28aa9c commit 516b824
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Illuminate/Http/Concerns/InteractsWithContentTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function wantsJson()
{
$acceptable = $this->getAcceptableContentTypes();

return isset($acceptable[0]) && Str::contains($acceptable[0], ['/json', '+json']);
return isset($acceptable[0]) && Str::contains(strtolower($acceptable[0]), ['/json', '+json']);
}

/**
Expand All @@ -62,6 +62,10 @@ public function accepts($contentTypes)
}

foreach ($types as $type) {
$accept = strtolower($accept);

$type = strtolower($type);

if ($this->matchesType($accept, $type) || $accept === strtok($type, '/').'/*') {
return true;
}
Expand Down Expand Up @@ -95,6 +99,10 @@ public function prefers($contentTypes)
$type = $mimeType;
}

$accept = strtolower($accept);

$type = strtolower($type);

if ($this->matchesType($type, $accept) || $accept === strtok($type, '/').'/*') {
return $contentType;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Http/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,21 @@ public function testBadAcceptHeader()
$this->assertFalse($request->accepts('text/html'));
}

public function testCaseInsensitiveAcceptHeader()
{
$request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'APPLICATION/JSON']);
$this->assertTrue($request->accepts(['text/html', 'application/json']));

$request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'AppLiCaTion/JsOn']);
$this->assertTrue($request->accepts(['text/html', 'application/json']));

$request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'APPLICATION/*']);
$this->assertTrue($request->accepts(['text/html', 'application/json']));

$request = Request::create('/', 'GET', [], [], [], ['HTTP_ACCEPT' => 'APPLICATION/JSON']);
$this->assertTrue($request->expectsJson());
}

public function testSessionMethod()
{
$this->expectException(RuntimeException::class);
Expand Down

0 comments on commit 516b824

Please sign in to comment.