Skip to content

Commit 009cdc1

Browse files
author
Andrey Helldar
committed
Fixed middleware initialization
1 parent 17a6474 commit 009cdc1

File tree

5 files changed

+32
-37
lines changed

5 files changed

+32
-37
lines changed

src/Middlewares/SetHeaderMiddleware.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Closure;
66
use Illuminate\Http\Request;
77
use Lmc\HttpConstants\Header;
8-
use Symfony\Component\HttpFoundation\JsonResponse;
98

109
class SetHeaderMiddleware
1110
{
@@ -17,14 +16,20 @@ class SetHeaderMiddleware
1716
*/
1817
public function handle(Request $request, Closure $next)
1918
{
20-
/** @var \Illuminate\Http\Response $response */
2119
$response = $next($request);
2220

23-
return $this->hasJson($response) ? $response : $response->header(Header::ACCEPT, 'application/json');
21+
$this->set($response);
22+
23+
return $response;
2424
}
2525

26-
protected function hasJson($response): bool
26+
/**
27+
* @param \Illuminate\Http\JsonResponse|\Illuminate\Http\Response $response
28+
*
29+
* @return void
30+
*/
31+
protected function set($response)
2732
{
28-
return $response instanceof JsonResponse;
33+
$response->headers->set(Header::ACCEPT, 'application/json');
2934
}
3035
}

src/ServiceProvider.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class ServiceProvider extends BaseServiceProvider
1313
public function boot(): void
1414
{
1515
$this->resolve()->prependMiddleware($this->middleware);
16-
$this->resolve()->prependToMiddlewarePriority($this->middleware);
1716
}
1817

1918
protected function resolve(): Kernel

tests/Middlewares/DuplicateTest.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

tests/Middlewares/SetHeaderMiddlewareTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function testWeb(): void
1414
->assertSuccessful()
1515
->assertHeader(Header::ACCEPT, 'application/json')
1616
->assertJsonStructure(['data'])
17-
->assertJson(['data' => 'Hello, JSON!']);
17+
->assertJson(['data' => 'Hello, Web!']);
1818
}
1919

2020
public function testApi(): void
@@ -24,6 +24,16 @@ public function testApi(): void
2424
->assertSuccessful()
2525
->assertHeader(Header::ACCEPT, 'application/json')
2626
->assertJsonStructure(['data'])
27-
->assertJson(['data' => 'Hello, JSON!']);
27+
->assertJson(['data' => 'Hello, Api!']);
28+
}
29+
30+
public function testCustom(): void
31+
{
32+
$this
33+
->get('custom')
34+
->assertSuccessful()
35+
->assertHeader(Header::ACCEPT, 'application/json')
36+
->assertJsonStructure(['data'])
37+
->assertJson(['data' => 'Hello, Custom!']);
2838
}
2939
}

tests/TestCase.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Tests;
44

55
use DragonCode\LaravelJsonResponse\ServiceProvider;
6-
use Illuminate\Contracts\Http\Kernel;
76
use Orchestra\Testbench\TestCase as BaseTestCase;
87

98
abstract class TestCase extends BaseTestCase
@@ -20,17 +19,19 @@ protected function getEnvironmentSetUp($app)
2019

2120
protected function setRoutes($app): void
2221
{
23-
$app['router']->get('web', function () {
24-
return ['data' => 'Hello, JSON!'];
22+
/** @var \Illuminate\Routing\RouteRegistrar $router */
23+
$router = $app['router'];
24+
25+
$router->get('web', function () {
26+
return ['data' => 'Hello, Web!'];
2527
})->middleware('web');
2628

27-
$app['router']->get('api', function () {
28-
return ['data' => 'Hello, JSON!'];
29+
$router->get('api', function () {
30+
return ['data' => 'Hello, Api!'];
2931
})->middleware('api');
30-
}
3132

32-
protected function getMiddlewareGroups(): array
33-
{
34-
return $this->app->make(Kernel::class)->getMiddlewareGroups();
33+
$router->get('custom', function () {
34+
return ['data' => 'Hello, Custom!'];
35+
});
3536
}
3637
}

0 commit comments

Comments
 (0)