File tree Expand file tree Collapse file tree 5 files changed +32
-37
lines changed Expand file tree Collapse file tree 5 files changed +32
-37
lines changed Original file line number Diff line number Diff line change 5
5
use Closure ;
6
6
use Illuminate \Http \Request ;
7
7
use Lmc \HttpConstants \Header ;
8
- use Symfony \Component \HttpFoundation \JsonResponse ;
9
8
10
9
class SetHeaderMiddleware
11
10
{
@@ -17,14 +16,20 @@ class SetHeaderMiddleware
17
16
*/
18
17
public function handle (Request $ request , Closure $ next )
19
18
{
20
- /** @var \Illuminate\Http\Response $response */
21
19
$ response = $ next ($ request );
22
20
23
- return $ this ->hasJson ($ response ) ? $ response : $ response ->header (Header::ACCEPT , 'application/json ' );
21
+ $ this ->set ($ response );
22
+
23
+ return $ response ;
24
24
}
25
25
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 )
27
32
{
28
- return $ response instanceof JsonResponse ;
33
+ $ response-> headers -> set (Header:: ACCEPT , ' application/json ' ) ;
29
34
}
30
35
}
Original file line number Diff line number Diff line change @@ -13,7 +13,6 @@ class ServiceProvider extends BaseServiceProvider
13
13
public function boot (): void
14
14
{
15
15
$ this ->resolve ()->prependMiddleware ($ this ->middleware );
16
- $ this ->resolve ()->prependToMiddlewarePriority ($ this ->middleware );
17
16
}
18
17
19
18
protected function resolve (): Kernel
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ public function testWeb(): void
14
14
->assertSuccessful ()
15
15
->assertHeader (Header::ACCEPT , 'application/json ' )
16
16
->assertJsonStructure (['data ' ])
17
- ->assertJson (['data ' => 'Hello, JSON ! ' ]);
17
+ ->assertJson (['data ' => 'Hello, Web ! ' ]);
18
18
}
19
19
20
20
public function testApi (): void
@@ -24,6 +24,16 @@ public function testApi(): void
24
24
->assertSuccessful ()
25
25
->assertHeader (Header::ACCEPT , 'application/json ' )
26
26
->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! ' ]);
28
38
}
29
39
}
Original file line number Diff line number Diff line change 3
3
namespace Tests ;
4
4
5
5
use DragonCode \LaravelJsonResponse \ServiceProvider ;
6
- use Illuminate \Contracts \Http \Kernel ;
7
6
use Orchestra \Testbench \TestCase as BaseTestCase ;
8
7
9
8
abstract class TestCase extends BaseTestCase
@@ -20,17 +19,19 @@ protected function getEnvironmentSetUp($app)
20
19
21
20
protected function setRoutes ($ app ): void
22
21
{
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! ' ];
25
27
})->middleware ('web ' );
26
28
27
- $ app [ ' router ' ] ->get ('api ' , function () {
28
- return ['data ' => 'Hello, JSON ! ' ];
29
+ $ router ->get ('api ' , function () {
30
+ return ['data ' => 'Hello, Api ! ' ];
29
31
})->middleware ('api ' );
30
- }
31
32
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
+ } );
35
36
}
36
37
}
You can’t perform that action at this time.
0 commit comments