Merge pull request #3481 from CachetHQ/feature/api-cache-control

API cache control
This commit is contained in:
James Brooks 2019-06-23 08:47:12 +01:00 committed by GitHub
commit ea8c9454e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 1 deletions

View File

@ -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,

View File

@ -0,0 +1,37 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class CacheControl
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$response = $next($request);
$maxAge = time() + 30;
$response->header('Cache-Control', 'public,max-age='.$maxAge);
return $response;
}
}

View File

@ -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']]);
});
});
}

View File

@ -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',
],
]);
}
}