Skip to content
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

HTTP cache methods #129

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
82 changes: 80 additions & 2 deletions src/Klein/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@
class Response
{

/**
* Class constants
*/

/** Public HTTP cache flag */
const PUBLIC_CACHE = 'public_cache';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new constants should probably just be integers. They're values don't really have much significance, since they're meant to be compared as constants, so following the PHP (Java inspired) convention of simply assigning an integer value would be better suited here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String constant significance lies in debugging. Where string can suggest flags meaning without having to look at constant list. My mistake, I overlooked that DISPATCH constants use integers. Will cahnge to integers.


/** Private HTTP cache flag */
const PRIVATE_CACHE = 'private_cache';

/** No HTTP cache flag */
const NO_CACHE = 'no_cache';

/**
* Class properties
*/
Expand Down Expand Up @@ -103,6 +116,13 @@ class Response
*/
public $chunked = false;

/**
* Cache control flag
*
* @var string
* @access public
*/
public $cache_control = self::NO_CACHE;

/**
* Methods
Expand Down Expand Up @@ -341,6 +361,36 @@ protected function httpStatusLine()
return sprintf('HTTP/%s %s', $this->protocol_version, $this->status);
}

/**
* Send HTTP cache headers
*
* @param boolean $override
* @access public
* @return Response
*/
public function sendCacheHeaders($override = false)
{

if (headers_sent() && !$override) {
return $this;
}

switch ($this->cache_control) {
case self::PUBLIC_CACHE:
header('Cache-Control: public');
break;
case self::PRIVATE_CACHE:
header('Cache-Control: private');
break;
case self::NO_CACHE:
header('Pragma: no-cache');
header('Cache-Control: no-store, no-cache');
break;
}

return $this;
}

/**
* Send our HTTP headers
*
Expand All @@ -358,6 +408,9 @@ public function sendHeaders($cookies_also = true, $override = false)
// Send our HTTP status line
header($this->httpStatusLine());

//Send HTTP cache headers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try and keep the code conventions here. A space after the //characters is the convention in this project.

$this->sendCacheHeaders($override);

// Iterate through our Headers data collection and send each header
foreach ($this->headers as $key => $value) {
header($key .': '. $value, false);
Expand Down Expand Up @@ -535,6 +588,32 @@ public function cookie(
return $this;
}

/**
* Tell the browser and network caches (e.g. Varnish) to publicly cache the response
*
* @access public
* @return Response
*/
public function publicCache()
{
$this->cache_control = self::PUBLIC_CACHE;

return $this;
}

/**
* Tell the browser to privately cache the response
*
* @access public
* @return Response
*/
public function privateCache()
{
$this->cache_control = self::PRIVATE_CACHE;

return $this;
}

/**
* Tell the browser not to cache the response
*
Expand All @@ -543,8 +622,7 @@ public function cookie(
*/
public function noCache()
{
$this->header('Pragma', 'no-cache');
$this->header('Cache-Control', 'no-store, no-cache');
$this->cache_control = self::NO_CACHE;

return $this;
}
Expand Down
59 changes: 48 additions & 11 deletions tests/Klein/Tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,21 @@ public function testSendHeaders()
$this->expectOutputString(null);
}

/**
* Yes, testing headers really is a pain in the ass. ;)
*
* Technically... we can't. So, yea.
*/
public function testSendCacheHeaders()
{
$response = new Response('beep bloop blop beep boop');
$response->noCache();

$response->sendCacheHeaders();

$this->expectOutputString(null);
}

/**
* Testing cookies is exactly like testing headers
* ... So, yea.
Expand Down Expand Up @@ -283,16 +298,40 @@ public function testHeader()
}
}

public function testPublicCache()
{
$response = new Response();

// Make sure cache control is not initially publicCache
$response->cache_control = null;

$response->publicCache();

$this->assertEquals(Response::PUBLIC_CACHE, $response->cache_control);
}

public function testPrivateCache()
{
$response = new Response();

// Make sure cache control is not initially privateCache
$response->cache_control = null;

$response->privateCache();

$this->assertEquals(Response::PRIVATE_CACHE, $response->cache_control);
}

public function testNoCache()
{
$response = new Response();

// Make sure the headers are initially empty
$this->assertEmpty($response->headers()->all());
// Make sure cache control is not initially noCache
$response->cache_control = null;

$response->noCache();

$this->assertContains('no-cache', $response->headers()->all());
$this->assertEquals(Response::NO_CACHE, $response->cache_control);
}

public function testRedirect()
Expand Down Expand Up @@ -337,6 +376,9 @@ function ($request, $response, $service) use ($file_name, $file_mime) {
file_get_contents(__FILE__)
);

//Assert noCache was set
$this->assertEquals(Response::NO_CACHE, $this->klein_app->response()->cache_control);

// Assert headers were passed
$this->assertEquals(
$file_mime,
Expand Down Expand Up @@ -378,15 +420,10 @@ function ($request, $response, $service) use ($test_object) {
json_encode($test_object)
);

//Assert noCache was set
$this->assertEquals(Response::NO_CACHE, $this->klein_app->response()->cache_control);

// Assert headers were passed
$this->assertEquals(
'no-cache',
$this->klein_app->response()->headers()->get('Pragma')
);
$this->assertEquals(
'no-store, no-cache',
$this->klein_app->response()->headers()->get('Cache-Control')
);
$this->assertEquals(
'application/json',
$this->klein_app->response()->headers()->get('Content-Type')
Expand Down