From d32f5e1aeac6dfeb39b94302588103f88c7531f1 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Tue, 19 Feb 2019 07:27:44 +0000 Subject: [PATCH 1/3] Add Cache Control middleware for #3479 --- app/Http/Kernel.php | 2 ++ app/Http/Middleware/CacheControl.php | 36 ++++++++++++++++++++++++++++ app/Http/Routes/ApiSystemRoutes.php | 2 +- 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 app/Http/Middleware/CacheControl.php diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index cc9e60b8e..df30b79eb 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -15,6 +15,7 @@ use Barryvdh\Cors\HandleCors; use CachetHQ\Cachet\Http\Middleware\Admin; use CachetHQ\Cachet\Http\Middleware\ApiAuthentication; use CachetHQ\Cachet\Http\Middleware\Authenticate; +use CachetHQ\Cachet\Http\Middleware\CacheControl; use CachetHQ\Cachet\Http\Middleware\Localize; use CachetHQ\Cachet\Http\Middleware\ReadyForUse; use CachetHQ\Cachet\Http\Middleware\RedirectIfAuthenticated; @@ -47,6 +48,7 @@ class Kernel extends HttpKernel 'admin' => Admin::class, 'can' => Authorize::class, 'cors' => HandleCors::class, + 'cache' => CacheControl::class, 'auth' => Authenticate::class, 'auth.api' => ApiAuthentication::class, 'guest' => RedirectIfAuthenticated::class, diff --git a/app/Http/Middleware/CacheControl.php b/app/Http/Middleware/CacheControl.php new file mode 100644 index 000000000..f6b6b3fac --- /dev/null +++ b/app/Http/Middleware/CacheControl.php @@ -0,0 +1,36 @@ +header('Cache-Control', 'public,max-age='.$maxAge); + + return $response; + } +} diff --git a/app/Http/Routes/ApiSystemRoutes.php b/app/Http/Routes/ApiSystemRoutes.php index 80899f71b..92a7aa2e3 100644 --- a/app/Http/Routes/ApiSystemRoutes.php +++ b/app/Http/Routes/ApiSystemRoutes.php @@ -43,7 +43,7 @@ class ApiSystemRoutes $router->group(['middleware' => ['auth.api']], function (Registrar $router) { $router->get('ping', 'GeneralController@ping'); $router->get('version', 'GeneralController@version'); - $router->get('status', 'GeneralController@status'); + $router->get('status', ['uses' => 'GeneralController@status', 'middleware' => ['cache']]); }); }); } From 5157e67f5048042b97c3118ce52f816c8cfa03e8 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Tue, 19 Feb 2019 07:30:28 +0000 Subject: [PATCH 2/3] Add status API tests --- tests/Api/GeneralTest.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/Api/GeneralTest.php b/tests/Api/GeneralTest.php index ef1538844..637d44c87 100644 --- a/tests/Api/GeneralTest.php +++ b/tests/Api/GeneralTest.php @@ -11,6 +11,8 @@ namespace CachetHQ\Tests\Cachet\Api; +use CachetHQ\Cachet\Models\Component; + /** * This is the general test class. * @@ -42,4 +44,36 @@ class GeneralTest extends AbstractApiTestCase $response->assertStatus(406); } + + public function test_can_get_system_status() + { + $response = $this->json('GET', '/api/v1/status'); + + $response->assertStatus(200) + ->assertHeader('Cache-Control') + ->assertJsonFragment([ + 'data' => [ + 'status' => 'success', + 'message' => 'System operational' + ] + ]); + } + + public function test_can_get_system_status_not_success() + { + factory(Component::class)->create([ + 'status' => 3, + ]); + + $response = $this->json('GET', '/api/v1/status'); + + $response->assertStatus(200) + ->assertHeader('Cache-Control') + ->assertJsonFragment([ + 'data' => [ + 'status' => 'info', + 'message' => 'The system is experiencing issues' + ] + ]); + } } From c6bb329ef8914b29be1beec21b4de633b8a491e7 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Tue, 19 Feb 2019 07:30:51 +0000 Subject: [PATCH 3/3] Apply fixes from StyleCI --- app/Http/Middleware/CacheControl.php | 5 +++-- tests/Api/GeneralTest.php | 8 ++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/app/Http/Middleware/CacheControl.php b/app/Http/Middleware/CacheControl.php index f6b6b3fac..fa54dfe09 100644 --- a/app/Http/Middleware/CacheControl.php +++ b/app/Http/Middleware/CacheControl.php @@ -19,8 +19,9 @@ class CacheControl /** * Handle an incoming request. * - * @param \Illuminate\Http\Request $request - * @param \Closure $next + * @param \Illuminate\Http\Request $request + * @param \Closure $next + * * @return mixed */ public function handle(Request $request, Closure $next) diff --git a/tests/Api/GeneralTest.php b/tests/Api/GeneralTest.php index 637d44c87..6d0d77cf3 100644 --- a/tests/Api/GeneralTest.php +++ b/tests/Api/GeneralTest.php @@ -54,8 +54,8 @@ class GeneralTest extends AbstractApiTestCase ->assertJsonFragment([ 'data' => [ 'status' => 'success', - 'message' => 'System operational' - ] + 'message' => 'System operational', + ], ]); } @@ -72,8 +72,8 @@ class GeneralTest extends AbstractApiTestCase ->assertJsonFragment([ 'data' => [ 'status' => 'info', - 'message' => 'The system is experiencing issues' - ] + 'message' => 'The system is experiencing issues', + ], ]); } }