Skip to content

Commit

Permalink
Setting Expires updates max-age
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Feb 10, 2017
1 parent a71d95d commit 748e215
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace ICanBoogie\HTTP;

use ICanBoogie\Accessor\AccessorTrait;
use ICanBoogie\DateTime;

/**
* A response to a HTTP request.
Expand Down Expand Up @@ -454,13 +455,16 @@ protected function get_last_modified()
/**
* Sets the value of the `Expires` header field.
*
* The method also calls the {@link session_cache_expire()} function.
* The method updates the `max-age` the Cache Control directive accordingly.
*
* @param mixed $time
*/
protected function set_expires($time)
{
$this->headers['Expires'] = $time;
/* @var DateTime $expires */
$expires = $this->headers['Expires'];
$this->cache_control->max_age = $expires->is_empty ? null : $expires->timestamp - time();
}

/**
Expand Down
4 changes: 3 additions & 1 deletion tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,14 @@ public function test_expires()
$this->assertInstanceOf(Date::class, $response->expires);
$this->assertEmpty((string) $response->expires);

$value = DateTime::now();
$value = new DateTime('+1 days');
$response->expires = $value;
$this->assertEquals($value, $response->expires);
$this->assertSame(86400, $response->cache_control->max_age);

$response->expires = null;
$this->assertEmpty((string) $response->expires);
$this->assertNull($response->cache_control->max_age);
}

/**
Expand Down

0 comments on commit 748e215

Please sign in to comment.