[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/net/tests/cases/ -> lmbHttpCacheTest.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/net/src/lmbHttpResponse.class.php');
  10  lmb_require('limb/net/src/lmbHttpCache.class.php');
  11  
  12  Mock :: generate('lmbHttpResponse', 'MockHttpResponse');
  13  
  14  class lmbHttpCacheTest extends UnitTestCase
  15  {
  16    var $response;
  17    var $cache;
  18    var $server_vars;
  19  
  20    function setUp()
  21    {
  22      $this->response = new MockHttpResponse();
  23      $this->cache = new lmbHttpCache();
  24      $this->server_vars = $_SERVER;
  25    }
  26  
  27    function tearDown()
  28    {
  29      $_SERVER = $this->server_vars;
  30    }
  31  
  32    function testSetCacheSettings()
  33    {
  34      $this->cache->setLastModifiedTime($time = time());
  35      $this->assertEqual($this->cache->getLastModifiedTime(), $time);
  36      $this->assertEqual($this->cache->formatLastModifiedTime(), gmdate('D, d M Y H:i:s \G\M\T', $time));
  37  
  38      $this->cache->setEtag($etag = md5(time()));
  39      $this->assertEqual($this->cache->getEtag(), $etag);
  40  
  41      $this->cache->setCacheTime(10);
  42      $this->assertEqual($this->cache->getCacheTime(), 10);
  43  
  44      $this->cache->setCacheType('public');
  45      $this->assertEqual($this->cache->getCacheType(), 'public');
  46    }
  47  
  48    function testGetDefaultEtag1()
  49    {
  50      $script = 'test';
  51      $query = 'query';
  52  
  53      $_SERVER['QUERY_STRING'] = $query;
  54      $_SERVER['SCRIPT_FILENAME'] = $script;
  55  
  56      $this->cache->setLastModifiedTime($time = time());
  57      $etag = $this->cache->getEtag();
  58  
  59      $this->assertEqual($etag, '"' . md5($script . '?' . $query . '#' . $time ) . '"');
  60    }
  61  
  62    function testGetDefaultEtag2()
  63    {
  64      $script = 'test';
  65      $query = 'query';
  66  
  67      $_SERVER['QUERY_STRING'] = $query;
  68      unset($_SERVER['SCRIPT_FILENAME']);
  69      $_SERVER['PATH_TRANSLATED'] = $script;
  70  
  71      $this->cache->setLastModifiedTime($time = time());
  72      $etag = $this->cache->getEtag();
  73  
  74      $this->assertEqual($etag, '"' . md5($script . '?' . $query . '#' . $time ) . '"');
  75    }
  76  
  77    function testGetDefaultEtag3()
  78    {
  79      $script = 'test';
  80  
  81      unset($_SERVER['QUERY_STRING']);
  82      $_SERVER['SCRIPT_FILENAME'] = $script;
  83  
  84      $this->cache->setLastModifiedTime($time = time());
  85      $etag = $this->cache->getEtag();
  86  
  87      $this->assertEqual($etag, '"' . md5($script . '#' . $time ) . '"');
  88    }
  89  
  90    function testIs412False()
  91    {
  92      $this->assertFalse($this->cache->is412());
  93    }
  94  
  95    function testIs412FalsePartOfEtag()
  96    {
  97      $_SERVER['HTTP_IF_MATCH'] = 'big_etag';
  98  
  99      $this->cache->setEtag('etag');
 100  
 101      $this->assertFalse($this->cache->is412());
 102    }
 103  
 104    function testIs412FalseAsteric()
 105    {
 106      $_SERVER['HTTP_IF_MATCH'] = '*';
 107  
 108      $this->cache->setEtag('etag');
 109  
 110      $this->assertFalse($this->cache->is412());
 111    }
 112  
 113    function testIs412Etag()
 114    {
 115      $_SERVER['HTTP_IF_MATCH'] = 'wrong';
 116  
 117      $this->cache->setEtag('etag');
 118  
 119      $this->assertTrue($this->cache->is412());
 120    }
 121  
 122    function testIs412UnmodifiedSince()
 123    {
 124      $this->cache->setLastModifiedTime($time = time());
 125  
 126      $_SERVER['HTTP_IF_UNMODIFIED_SINCE'] = gmdate('D, d M Y H:i:s \G\M\T', $time - 100);
 127  
 128      $this->assertTrue($this->cache->is412());
 129    }
 130  
 131    function testIs304False()
 132    {
 133      $this->assertFalse($this->cache->is304());
 134    }
 135  
 136    function testIs304LastModifiedTime()
 137    {
 138      $this->cache->setLastModifiedTime($time = time());
 139  
 140      $_SERVER['HTTP_IF_MODIFIED_SINCE'] = $this->cache->formatLastModifiedTime();
 141  
 142      $this->assertTrue($this->cache->is304());
 143    }
 144  
 145    function testIs304Etag()
 146    {
 147      $etag = 'etag';
 148  
 149      unset($_SERVER['HTTP_IF_MODIFIED_SINCE']);
 150      $_SERVER['HTTP_IF_NONE_MATCH'] = $etag;
 151  
 152      $this->cache->setLastModifiedTime($time = time());
 153      $this->cache->setEtag($etag);
 154  
 155      $this->assertTrue($this->cache->is304());
 156    }
 157  
 158    function testIs304EtagAsteric()
 159    {
 160      $etag = 'etag';
 161  
 162      unset($_SERVER['HTTP_IF_MODIFIED_SINCE']);
 163      $_SERVER['HTTP_IF_NONE_MATCH'] = '*';
 164  
 165      $this->cache->setLastModifiedTime($time = time());
 166      $this->cache->setEtag($etag);
 167  
 168      $this->assertTrue($this->cache->is304());
 169    }
 170  
 171    function testCheckAndWrite412()
 172    {
 173      $_SERVER['HTTP_IF_MATCH'] = 'wrong';
 174  
 175      $this->cache->setEtag('etag');
 176  
 177      $this->response->expectCallCount('header', 3);
 178      $this->response->expectArgumentsAt(0, 'header', array('HTTP/1.1 412 Precondition Failed'));
 179      $this->response->expectArgumentsAt(1, 'header', array('Cache-Control: protected, max-age=0, must-revalidate'));
 180      $this->response->expectArgumentsAt(2, 'header', array('Content-Type: text/plain'));
 181  
 182      $this->response->expectOnce('write', array(new WantedPatternExpectation("~^HTTP/1.1 Error 412~")));
 183  
 184      $this->assertTrue($this->cache->checkAndWrite($this->response));
 185    }
 186  
 187    function testCheckAndWrite304()
 188    {
 189      $_SERVER['HTTP_IF_NONE_MATCH'] = 'etag';
 190  
 191      $this->cache->setEtag('etag');
 192  
 193      $this->response->expectCallCount('header', 6);
 194      $this->response->expectArgumentsAt(0, 'header', array('HTTP/1.0 304 Not Modified'));
 195      $this->response->expectArgumentsAt(1, 'header', array('Etag: etag'));
 196      $this->response->expectArgumentsAt(2, 'header', array('Pragma: '));
 197      $this->response->expectArgumentsAt(3, 'header', array('Cache-Control: '));
 198      $this->response->expectArgumentsAt(4, 'header', array('Last-Modified: '));
 199      $this->response->expectArgumentsAt(5, 'header', array('Expires: '));
 200  
 201      $this->assertTrue($this->cache->checkAndWrite($this->response));
 202    }
 203  
 204    function testCheckAndWriteFalseNotHead()
 205    {
 206      $_SERVER['REQUEST_METHOD'] = 'GET';
 207      $this->assertFalse($this->cache->checkAndWrite($this->response));
 208    }
 209  
 210    function testCheckAndWriteNoCacheTime()
 211    {
 212      $_SERVER['REQUEST_METHOD'] = 'HEAD';
 213  
 214      $this->cache->setLastModifiedTime($time = time());
 215  
 216      $this->response->expectCallCount('header', 5);
 217      $this->response->expectArgumentsAt(0, 'header', array('Cache-Control: protected, must-revalidate, max-age=0'));
 218      $this->response->expectArgumentsAt(1, 'header', array('Last-Modified: ' . $this->cache->formatLastModifiedTime()));
 219      $this->response->expectArgumentsAt(2, 'header', array('Etag: ' . $this->cache->getEtag()));
 220      $this->response->expectArgumentsAt(3, 'header', array('Pragma: '));
 221      $this->response->expectArgumentsAt(4, 'header', array('Expires: '));
 222  
 223      $this->assertTrue($this->cache->checkAndWrite($this->response));
 224    }
 225  
 226    function testCheckAndWriteWithCacheTime()
 227    {
 228      $_SERVER['REQUEST_METHOD'] = 'HEAD';
 229  
 230      $this->cache->setLastModifiedTime($time = time());
 231      $this->cache->setCacheTime(100);
 232  
 233      $this->response->expectCallCount('header', 5);
 234      $this->response->expectArgumentsAt(0, 'header', array('Cache-Control: protected, max-age=100'));
 235      $this->response->expectArgumentsAt(1, 'header', array('Last-Modified: ' . $this->cache->formatLastModifiedTime()));
 236      $this->response->expectArgumentsAt(2, 'header', array('Etag: ' . $this->cache->getEtag()));
 237      $this->response->expectArgumentsAt(3, 'header', array('Pragma: '));
 238      $this->response->expectArgumentsAt(4, 'header', array('Expires: '));
 239  
 240      $this->assertTrue($this->cache->checkAndWrite($this->response));
 241    }
 242  
 243    function testCheckAndWriteWithPrivacy()
 244    {
 245      $_SERVER['REQUEST_METHOD'] = 'HEAD';
 246  
 247      $this->cache->setLastModifiedTime($time = time());
 248      $this->cache->setCacheTime(100);
 249      $this->cache->setCacheType(LIMB_HTTP_CACHE_TYPE_PUBLIC);
 250  
 251      $this->response->expectCallCount('header', 5);
 252      $this->response->expectArgumentsAt(0, 'header', array('Cache-Control: public, max-age=100'));
 253      $this->response->expectArgumentsAt(1, 'header', array('Last-Modified: ' . $this->cache->formatLastModifiedTime()));
 254      $this->response->expectArgumentsAt(2, 'header', array('Etag: ' . $this->cache->getEtag()));
 255      $this->response->expectArgumentsAt(3, 'header', array('Pragma: '));
 256      $this->response->expectArgumentsAt(4, 'header', array('Expires: '));
 257  
 258      $this->assertTrue($this->cache->checkAndWrite($this->response));
 259    }
 260  }
 261  ?>


Generated: Sat Nov 22 03:48:54 2008 Cross-referenced by PHPXref 0.7