PageRenderTime 24ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/testapp/tests-jelix/jelix/cache/httpcacheTest.php

https://github.com/gmarrot/jelix
PHP | 149 lines | 88 code | 37 blank | 24 comment | 2 complexity | 5f7757c58661ab1d6b70691b69d0be93 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. require_once(JELIX_LIB_CORE_PATH.'/request/jClassicRequest.class.php');
  3. class httpcacheTest extends jUnitTestCase
  4. {
  5. protected $_server;
  6. function setUp(){
  7. jApp::saveContext();
  8. self::initClassicRequest(TESTAPP_URL.'index.php');
  9. }
  10. function tearDown() {
  11. jApp::restoreContext();
  12. }
  13. /**
  14. * @covers jResponse::isValidCache
  15. */
  16. public function testIsValideCacheWithLastModified() {
  17. $good_date= gmdate('D, d M Y H:i:s \G\M\T', time());
  18. $wrong_date = gmdate('D, d M Y H:i:s \G\M\T', time() + 100);
  19. $_SERVER['HTTP_If_Modified_Since'] = $good_date;
  20. $rep = jApp::coord()->request->getResponse('html');
  21. $this->assertNotNull(jApp::coord()->request);
  22. $this->assertInstanceOf('jResponse', $rep);
  23. $this->assertEquals($good_date, jApp::coord()->request->header('If-Modified-Since'));
  24. $unusedHeaderValue = 'test123456';
  25. $rep->addHttpHeader('Content-Language', $unusedHeaderValue);
  26. $this->assertTrue($rep->isValidCache($good_date));
  27. $this->assertFalse($rep->isValidCache($wrong_date));
  28. //test the suppresion of unused headers
  29. $this->assertAttributeNotContains($unusedHeaderValue, '_httpHeaders', $rep);
  30. }
  31. /**
  32. * @covers jResponse::isValidCache
  33. */
  34. public function testIsValideCacheWithEtag() {
  35. $good_etag = 'abcdef';
  36. $wrong_etag = 'vwxyz';
  37. $_SERVER['HTTP_If_None_Match'] = $good_etag;
  38. $rep = jApp::coord()->request->getResponse('html');
  39. $this->assertNotNull(jApp::coord()->request);
  40. $this->assertInstanceOf('jResponse', $rep);
  41. $this->assertEquals($good_etag, jApp::coord()->request->header('If-None-Match'));
  42. $this->assertTrue($rep->isValidCache(null, $good_etag));
  43. $this->assertFalse($rep->isValidCache(null, $wrong_etag));
  44. }
  45. /**
  46. * @covers jResponse::setLifeTime
  47. */
  48. public function testSetLifeTime(){
  49. $rep = jApp::coord()->request->getResponse('html');
  50. $rep->setLifeTime(30);
  51. $value = 'private, maxage=30';
  52. $expected_headers = array('Cache-Control' => $value, 'Expires' => '', 'Pragma' => '');
  53. $this->assertAttributeEquals($expected_headers, '_httpHeaders', $rep);
  54. $expected_headers = null;
  55. $rep->setLifeTime(10, true);
  56. $value = 'public, s-maxage=10';
  57. $expected_headers = array('Cache-Control' => $value, 'Expires' => '', 'Pragma' => '');
  58. $this->assertAttributeEquals($expected_headers, '_httpHeaders', $rep);
  59. }
  60. /**
  61. * @covers jResponse::setExpires
  62. */
  63. public function testSetExpires(){
  64. $rep = jApp::coord()->request->getResponse('html');
  65. $good_date= gmdate('D, d M Y H:i:s \G\M\T', time());
  66. $rep->setExpires($good_date);
  67. $expected_headers = array('Cache-Control' => '', 'Expires' => $good_date, 'Pragma' => '');
  68. $this->assertAttributeEquals($expected_headers, '_httpHeaders', $rep);
  69. }
  70. /**
  71. * @covers jResponse::_normalizeDate
  72. */
  73. public function testNormalizeDate(){
  74. if(class_exists('ReflectionMethod')){
  75. $rep = jApp::coord()->request->getResponse('html');
  76. $method = new ReflectionMethod('jResponse', '_normalizeDate');
  77. $method->setAccessible(TRUE);
  78. //case jDateTime
  79. $date1 = "2011-10-26 13:00:00";
  80. $dt = new jDateTime();
  81. $dt->setFromString($date1, jDateTime::DB_DTFORMAT);
  82. $this->assertEquals( gmdate('D, d M Y H:i:s \G\M\T', strtotime($date1)), $method->invoke($rep, $dt));
  83. //case DateTime
  84. $date2 = '2011-10-26 10:00:00';
  85. $dt = new DateTime($date2);
  86. $this->assertEquals( gmdate('D, d M Y H:i:s \G\M\T', strtotime($date2)), $method->invoke($rep, $dt));
  87. //case strtotime
  88. $date3 = '2011-10-26 05:02:02';
  89. $this->assertEquals(gmdate('D, d M Y H:i:s \G\M\T', strtotime($date3)), $method->invoke($rep, $date3));
  90. }
  91. }
  92. /**
  93. * @covers jResponse::_checkRequestType
  94. * @expectedException PHPUnit_Framework_Error
  95. */
  96. public function testCheckRequestType(){
  97. if(class_exists('ReflectionMethod')){
  98. //prepare
  99. $rep = jApp::coord()->request->getResponse('html');
  100. $method = new ReflectionMethod('jResponse', '_checkRequestType');
  101. $method->setAccessible(TRUE);
  102. $_SERVER['REQUEST_METHOD'] = 'GET';
  103. $this->assertTrue($method->invoke($rep));
  104. $_SERVER['REQUEST_METHOD'] = 'HEAD';
  105. $this->assertTrue($method->invoke($rep));
  106. $_SERVER['REQUEST_METHOD'] = 'POST';
  107. $method->invoke($rep);
  108. }
  109. else
  110. trigger_error('you dont support the ReflexionMethod class'); //for not fail at the assertion
  111. }
  112. }