Permalink
Browse files

Merge branch 'feature-expires' into release-1.6.0

  • Loading branch information...
2 parents 0e0b794 + 44efd62 commit 9ede10cd1af4f86a8dca8ea0f8eb500afb65d517 Josh Lockhart committed Jan 12, 2012
Showing with 78 additions and 0 deletions.
  1. +21 −0 Slim/Slim.php
  2. +57 −0 tests/SlimTest.php
View
@@ -679,6 +679,27 @@ public function etag( $value, $type = 'strong' ) {
}
}
+ /**
+ * Set Expires HTTP response header
+ *
+ * The `Expires` header tells the HTTP client the time at which
+ * the current resource should be considered stale. At that time the HTTP
+ * client will send a conditional GET request to the server; the server
+ * may return a 200 OK if the resource has changed, else a 304 Not Modified
+ * if the resource has not changed. The `Expires` header should be used in
+ * conjunction with the `etag()` or `lastModified()` methods above.
+ *
+ * @param string|int $time If string, a time to be parsed by `strtotime()`;
+ * If int, a UNIX timestamp;
+ * @return void
+ */
+ public function expires( $time ) {
+ if ( is_string($time) ) {
+ $time = strtotime($time);
+ }
+ $this->response['Expires'] = gmdate(DATE_RFC1123, $time);
+ }
+
/***** COOKIES *****/
/**
View
@@ -742,6 +742,63 @@ public function testETagWithInvalidType(){
list($status, $header, $body) = $s->call($env);
}
+ /**
+ * Test Expires
+ */
+ public function testExpiresAsString() {
+ Slim_Environment::mock(array(
+ 'REQUEST_METHOD' => 'GET',
+ 'REMOTE_ADDR' => '127.0.0.1',
+ 'SCRIPT_NAME' => '/foo', //<-- Physical
+ 'PATH_INFO' => '/bar', //<-- Virtual
+ 'QUERY_STRING' => 'one=foo&two=bar',
+ 'SERVER_NAME' => 'slimframework.com',
+ 'SERVER_PORT' => 80,
+ 'HTTP_IF_NONE_MATCH' => '"abc1234"',
+ 'slim.url_scheme' => 'http',
+ 'slim.input' => '',
+ 'slim.errors' => @fopen('php://stderr', 'w')
+ ));
+ $expectedDate = gmdate('D, d M Y', strtotime('5 days')); //Just the day, month, and year
+ $s = new Slim();
+ $s->get('/bar', function () use ($s) {
+ $s->expires('5 days');
+ });
+ $env = $s->environment();
+ list($status, $header, $body) = $s->call($env);
+ $this->assertTrue(isset($header['Expires']));
+ $this->assertEquals(0, strpos($header['Expires'], $expectedDate));
+ }
+
+ /**
+ * Test Expires
+ */
+ public function testExpiresAsInteger() {
+ Slim_Environment::mock(array(
+ 'REQUEST_METHOD' => 'GET',
+ 'REMOTE_ADDR' => '127.0.0.1',
+ 'SCRIPT_NAME' => '/foo', //<-- Physical
+ 'PATH_INFO' => '/bar', //<-- Virtual
+ 'QUERY_STRING' => 'one=foo&two=bar',
+ 'SERVER_NAME' => 'slimframework.com',
+ 'SERVER_PORT' => 80,
+ 'HTTP_IF_NONE_MATCH' => '"abc1234"',
+ 'slim.url_scheme' => 'http',
+ 'slim.input' => '',
+ 'slim.errors' => @fopen('php://stderr', 'w')
+ ));
+ $fiveDaysFromNow = time() + (60 * 60 * 24 * 5);
+ $expectedDate = gmdate('D, d M Y', $fiveDaysFromNow); //Just the day, month, and year
+ $s = new Slim();
+ $s->get('/bar', function () use ($s, $fiveDaysFromNow) {
+ $s->expires($fiveDaysFromNow);
+ });
+ $env = $s->environment();
+ list($status, $header, $body) = $s->call($env);
+ $this->assertTrue(isset($header['Expires']));
+ $this->assertEquals(0, strpos($header['Expires'], $expectedDate));
+ }
+
/************************************************
* COOKIES
************************************************/

0 comments on commit 9ede10c

Please sign in to comment.