Skip to content

Added ability to assign middleware to specific route groups #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 15, 2022
Merged
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ Or manually update `require` block of `composer.json` and run `composer update`.

After you've installed the package via composer, you're done. There's no step two.

This package will automatically register the `DragonCode\LaravelJsonResponse\Middlewares\SetHeaderMiddleware` middleware in the `web` and `api` groups, if they exist. The middleware will add a header `Accept` that will effectively convert all responses to JSON format. This header will apply to all responses.
This package will automatically register the `DragonCode\LaravelJsonResponse\Middlewares\SetHeaderMiddleware` middleware in the `web` and `api` groups, if they exist. The
middleware will add a header `Accept` that will effectively convert all responses to JSON format. This header will apply to all responses.

> If you need to redefine the header for specific groups of routes, you can do this by changing the [`settings`](config/http.php).


[badge_build]: https://img.shields.io/github/workflow/status/TheDragonCode/laravel-json-response/phpunit?style=flat-square
Expand Down
21 changes: 21 additions & 0 deletions config/http.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

return [
'response' => [
/*
* This setting is responsible for applying middleware to routes.
*
* You can specify route group names to apply the `SetHeaderMiddleware`
* middleware to them, or specify null to apply it to all routes.
*
* For example,
*
* json => null
* json => 'api'
* json => ['api', 'web']
*/
'json' => null,
],
];
51 changes: 50 additions & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DragonCode\LaravelJsonResponse\Middlewares\SetHeaderMiddleware;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Support\Arr;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;

class ServiceProvider extends BaseServiceProvider
Expand All @@ -12,7 +13,55 @@ class ServiceProvider extends BaseServiceProvider

public function boot(): void
{
$this->resolve()->prependMiddleware($this->middleware);
$this->publishConfig();

$this->registerMiddleware($this->middlewareGroups());
}

public function register(): void
{
$this->registerConfig();
}

protected function registerMiddleware(array $groups): void
{
$this->toAll($groups)
? $this->resolve()->prependMiddleware($this->middleware)
: $this->prependMiddlewareToGroups($this->middleware, $groups);
}

protected function prependMiddlewareToGroups(string $middleware, array $groups): void
{
foreach ($groups as $group) {
$this->resolve()->prependMiddlewareToGroup($group, $middleware);
}
}

protected function toAll(array $groups): bool
{
return empty($groups);
}

protected function middlewareGroups(): array
{
return array_filter(Arr::wrap(
$this->app['config']->get('http.response.json')
));
}

protected function publishConfig(): void
{
$this->publishes([
__DIR__ . '/../config/http.php' => $this->app->configPath('http.php'),
]);
}

protected function registerConfig(): void
{
$this->mergeConfigFrom(
__DIR__ . '/../config/http.php',
'http'
);
}

protected function resolve(): Kernel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
use Lmc\HttpConstants\Header;
use Tests\TestCase;

class SetHeaderMiddlewareTest extends TestCase
class AllTest extends TestCase
{
public function testWeb(): void
public function testApi(): void
{
$this
->get('web')
->get('api')
->assertSuccessful()
->assertHeader(Header::ACCEPT, 'application/json')
->assertJsonStructure(['data'])
->assertJson(['data' => 'Hello, Web!']);
->assertJson(['data' => 'Hello, Api!']);
}

public function testApi(): void
public function testWeb(): void
{
$this
->get('api')
->get('web')
->assertSuccessful()
->assertHeader(Header::ACCEPT, 'application/json')
->assertJsonStructure(['data'])
->assertJson(['data' => 'Hello, Api!']);
->assertJson(['data' => 'Hello, Web!']);
}

public function testCustom(): void
Expand Down
61 changes: 61 additions & 0 deletions tests/Middlewares/ApiAndWebTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Tests\Middlewares;

use Lmc\HttpConstants\Header;
use Tests\TestCase;

class ApiAndWebTest extends TestCase
{
protected $groups = ['api', 'web'];

public function testApi(): void
{
$this
->get('api')
->assertSuccessful()
->assertHeader(Header::ACCEPT, 'application/json')
->assertJsonStructure(['data'])
->assertJson(['data' => 'Hello, Api!']);
}

public function testWeb(): void
{
$this
->get('web')
->assertSuccessful()
->assertHeader(Header::ACCEPT, 'application/json')
->assertJsonStructure(['data'])
->assertJson(['data' => 'Hello, Web!']);
}

public function testCustom(): void
{
$this
->get('custom')
->assertSuccessful()
->assertHeaderMissing(Header::ACCEPT)
->assertJsonStructure(['data'])
->assertJson(['data' => 'Hello, Custom!']);
}

public function testCustomHeader(): void
{
$this
->get('custom', [Header::ACCEPT => 'application/xml'])
->assertSuccessful()
->assertHeaderMissing(Header::ACCEPT)
->assertJsonStructure(['data'])
->assertJson(['data' => 'Hello, Custom!']);
}

public function testAsterisk(): void
{
$this
->get('custom', [Header::ACCEPT => '*/*'])
->assertSuccessful()
->assertHeaderMissing(Header::ACCEPT)
->assertJsonStructure(['data'])
->assertJson(['data' => 'Hello, Custom!']);
}
}
61 changes: 61 additions & 0 deletions tests/Middlewares/OnlyApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Tests\Middlewares;

use Lmc\HttpConstants\Header;
use Tests\TestCase;

class OnlyApiTest extends TestCase
{
protected $groups = 'api';

public function testApi(): void
{
$this
->get('api')
->assertSuccessful()
->assertHeader(Header::ACCEPT, 'application/json')
->assertJsonStructure(['data'])
->assertJson(['data' => 'Hello, Api!']);
}

public function testWeb(): void
{
$this
->get('web')
->assertSuccessful()
->assertHeaderMissing(Header::ACCEPT)
->assertJsonStructure(['data'])
->assertJson(['data' => 'Hello, Web!']);
}

public function testCustom(): void
{
$this
->get('custom')
->assertSuccessful()
->assertHeaderMissing(Header::ACCEPT)
->assertJsonStructure(['data'])
->assertJson(['data' => 'Hello, Custom!']);
}

public function testCustomHeader(): void
{
$this
->get('custom', [Header::ACCEPT => 'application/xml'])
->assertSuccessful()
->assertHeaderMissing(Header::ACCEPT)
->assertJsonStructure(['data'])
->assertJson(['data' => 'Hello, Custom!']);
}

public function testAsterisk(): void
{
$this
->get('custom', [Header::ACCEPT => '*/*'])
->assertSuccessful()
->assertHeaderMissing(Header::ACCEPT)
->assertJsonStructure(['data'])
->assertJson(['data' => 'Hello, Custom!']);
}
}
61 changes: 61 additions & 0 deletions tests/Middlewares/OnlyWebTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Tests\Middlewares;

use Lmc\HttpConstants\Header;
use Tests\TestCase;

class OnlyWebTest extends TestCase
{
protected $groups = 'web';

public function testApi(): void
{
$this
->get('api')
->assertSuccessful()
->assertHeaderMissing(Header::ACCEPT)
->assertJsonStructure(['data'])
->assertJson(['data' => 'Hello, Api!']);
}

public function testWeb(): void
{
$this
->get('web')
->assertSuccessful()
->assertHeader(Header::ACCEPT, 'application/json')
->assertJsonStructure(['data'])
->assertJson(['data' => 'Hello, Web!']);
}

public function testCustom(): void
{
$this
->get('custom')
->assertSuccessful()
->assertHeaderMissing(Header::ACCEPT)
->assertJsonStructure(['data'])
->assertJson(['data' => 'Hello, Custom!']);
}

public function testCustomHeader(): void
{
$this
->get('custom', [Header::ACCEPT => 'application/xml'])
->assertSuccessful()
->assertHeaderMissing(Header::ACCEPT)
->assertJsonStructure(['data'])
->assertJson(['data' => 'Hello, Custom!']);
}

public function testAsterisk(): void
{
$this
->get('custom', [Header::ACCEPT => '*/*'])
->assertSuccessful()
->assertHeaderMissing(Header::ACCEPT)
->assertJsonStructure(['data'])
->assertJson(['data' => 'Hello, Custom!']);
}
}
17 changes: 12 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@
namespace Tests;

use DragonCode\LaravelJsonResponse\ServiceProvider;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Routing\Router;
use Orchestra\Testbench\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
protected $groups;

protected function getPackageProviders($app): array
{
return [ServiceProvider::class];
}

protected function getEnvironmentSetUp($app)
{
$this->setRoutes($app);
$this->setRoutes($app['router']);
$this->setConfig($app['config']);
}

protected function setRoutes($app): void
protected function setRoutes(Router $router): void
{
/** @var \Illuminate\Routing\RouteRegistrar $router */
$router = $app['router'];

$router->get('web', function () {
return ['data' => 'Hello, Web!'];
})->middleware('web');
Expand All @@ -34,4 +36,9 @@ protected function setRoutes($app): void
return ['data' => 'Hello, Custom!'];
});
}

protected function setConfig(Repository $config): void
{
$config->set('http.response.json', $this->groups);
}
}