Skip to content

Commit

Permalink
Add UncacheableByVarnish middleware (#3)
Browse files Browse the repository at this point in the history
[ci skip] [skip ci]
  • Loading branch information
richan-fongdasen authored Jun 25, 2018
1 parent f1077ab commit 85b1792
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Concerns/ManipulateHttpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ protected function addCacheableHeader(Response $response, $cacheDuration)
->header('Cache-Control', 'public, max-age='.$duration);
}

/**
* Add uncacheable header so varnish can recognize
* the response as an uncacheable content.
*
* @param \Illuminate\Http\Response $response
*/
public function addUncacheableHeader(Response $response)
{
return $response->header($this->getConfig('uncacheable_header'), '1');
}

/**
* Add an ETag header to the current response.
*
Expand Down
25 changes: 25 additions & 0 deletions src/Middleware/UncacheableByVarnish.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace RichanFongdasen\Varnishable\Middleware;

use Closure;

class UncacheableByVarnish
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);

\Varnishable::addUncacheableHeader($response);

return $response;
}
}
10 changes: 10 additions & 0 deletions tests/Concerns/ManipulateHttpResponseTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ public function it_can_add_an_etag_header_to_the_current_response_object()
$this->assertEquals('"'. md5($content) .'"', $actual);
}

/** @test */
public function it_can_add_uncacheable_header_to_the_current_response_object()
{
$this->service->addUncacheableHeader($this->response);

$uncacheable = $this->response->headers->get(\Varnishable::getConfig('uncacheable_header'));

$this->assertEquals('1', $uncacheable);
}

/** @test */
public function it_can_calculate_total_cache_duration_in_seconds()
{
Expand Down
43 changes: 43 additions & 0 deletions tests/Middleware/UncacheableByVarnishTests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace RichanFongdasen\Varnishable\Tests\Middleware;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use RichanFongdasen\Varnishable\Middleware\UncacheableByVarnish;
use RichanFongdasen\Varnishable\Tests\TestCase;

class UncacheableByVarnishTests extends TestCase
{
/**
* Cacheable by varnish middleware object.
*
* @var \RichanFongdasen\Varnishable\Middleware\UncacheableByVarnish
*/
protected $middleware;

/**
* Setup the test environment
*/
public function setUp()
{
parent::setUp();

$this->middleware = new UncacheableByVarnish;
}

/** @test */
public function it_can_handle_the_incoming_request_and_manipulate_the_response_headers()
{
$request = new Request;
$response = new Response;

$this->middleware->handle(
$request,
function (Request $request) use ($response) { return $response; }
);

$actual = $response->headers->get(\Varnishable::getConfig('uncacheable_header'));
$this->assertEquals('1', $actual);
}
}

0 comments on commit 85b1792

Please sign in to comment.