Skip to content

Commit

Permalink
Merge pull request #3481 from CachetHQ/feature/api-cache-control
Browse files Browse the repository at this point in the history
API cache control
  • Loading branch information
jbrooksuk committed Jun 23, 2019
2 parents 76e9f41 + c6bb329 commit ea8c945
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand Down Expand Up @@ -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,
Expand Down
37 changes: 37 additions & 0 deletions app/Http/Middleware/CacheControl.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 1 addition & 1 deletion app/Http/Routes/ApiSystemRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function map(Registrar $router)
$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']]);
});
});
}
Expand Down
34 changes: 34 additions & 0 deletions tests/Api/GeneralTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace CachetHQ\Tests\Cachet\Api;

use CachetHQ\Cachet\Models\Component;

/**
* This is the general test class.
*
Expand Down Expand Up @@ -42,4 +44,36 @@ public function test_non_acceptable_content_type()

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

0 comments on commit ea8c945

Please sign in to comment.