Skip to content

Commit 1969995

Browse files
Allow change accept header when wildcard accept header is requested (#8)
Co-authored-by: Jose Manuel Cardona <[email protected]>
1 parent a2c5fa9 commit 1969995

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
},
1111
"require": {
1212
"php": ">=7.2",
13-
"illuminate/http": "^7.0 || ^8.0 || ^9.0",
14-
"illuminate/contracts": "^7.0 || ^8.0 || ^9.0"
13+
"illuminate/http": "^8.0 || ^9.0 || ^10.0",
14+
"illuminate/contracts": "^8.0 || ^9.0 || ^10.0"
1515
},
1616
"require-dev": {
1717
"friendsofphp/php-cs-fixer": "^2.16",

src/Middleware/RequestAcceptJson.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ class RequestAcceptJson
99
{
1010
public function handle(Request $request, Closure $next)
1111
{
12-
if (!$request->headers->has('Accept')) {
12+
$accept = $request->headers->get('Accept');
13+
14+
if ($accept === null || $accept === '*/*' || $accept === 'application/*') {
1315
$request->headers->set('Accept', 'application/json');
1416
}
1517

tests/Middleware/RequestAcceptJsonTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,40 @@ function ($request) {
2525
$this->assertSame('application/json', $result->header('Accept'));
2626
}
2727

28+
/**
29+
* @test
30+
*/
31+
public function whenItReceivesWildcardAcceptHeaderItShouldSetToApplicationJson()
32+
{
33+
$mockRequest = $this->getRequest(['Accept' => '*/*']);
34+
$middleware = new RequestAcceptJson();
35+
36+
$result = $middleware->handle(
37+
$mockRequest,
38+
function ($request) {
39+
return $request;
40+
}
41+
);
42+
$this->assertSame('application/json', $result->header('Accept'));
43+
}
44+
45+
/**
46+
* @test
47+
*/
48+
public function whenItReceivesApplicationWildcardAcceptHeaderItShouldSetToApplicationJson()
49+
{
50+
$mockRequest = $this->getRequest(['Accept' => 'application/*']);
51+
$middleware = new RequestAcceptJson();
52+
53+
$result = $middleware->handle(
54+
$mockRequest,
55+
function ($request) {
56+
return $request;
57+
}
58+
);
59+
$this->assertSame('application/json', $result->header('Accept'));
60+
}
61+
2862
/**
2963
* @test
3064
*/

0 commit comments

Comments
 (0)