ApiCacheParserTest

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.

Defined (1)

The class is defined in the following location(s).

/lib/vendor/google/apiclient/tests/general/ApiCacheParserTest.php  
  1. class ApiCacheParserTest extends BaseTest { 
  2. public function testIsResponseCacheable() { 
  3. $client = $this->getClient(); 
  4. $resp = new Google_Http_Request('http://localhost', 'POST'); 
  5. $result = Google_Http_CacheParser::isResponseCacheable($resp); 
  6. $this->assertFalse($result); 
  7.  
  8. // The response has expired, and we don't have an etag for 
  9. // revalidation. 
  10. $resp = new Google_Http_Request('http://localhost', 'GET'); 
  11. $resp->setResponseHttpCode('200'); 
  12. $resp->setResponseHeaders(array( 
  13. 'Cache-Control' => 'max-age=3600, must-revalidate',  
  14. 'Expires' => 'Fri, 30 Oct 1998 14:19:41 GMT',  
  15. 'Date' => 'Mon, 29 Jun 1998 02:28:12 GMT',  
  16. 'Last-Modified' => 'Mon, 29 Jun 1998 02:28:12 GMT',  
  17. )); 
  18. $result = Google_Http_CacheParser::isResponseCacheable($resp); 
  19. $this->assertFalse($result); 
  20.  
  21. // Verify cacheable responses. 
  22. $resp = new Google_Http_Request('http://localhost', 'GET'); 
  23. $resp->setResponseHttpCode('200'); 
  24. $resp->setResponseHeaders(array( 
  25. 'Cache-Control' => 'max-age=3600, must-revalidate',  
  26. 'Expires' => 'Fri, 30 Oct 2013 14:19:41 GMT',  
  27. 'Date' => 'Mon, 29 Jun 2011 02:28:12 GMT',  
  28. 'Last-Modified' => 'Mon, 29 Jun 2011 02:28:12 GMT',  
  29. 'ETag' => '3e86-410-3596fbbc',  
  30. )); 
  31. $result = Google_Http_CacheParser::isResponseCacheable($resp); 
  32. $this->assertTrue($result); 
  33.  
  34. // Verify that responses to HEAD requests are cacheable. 
  35. $resp = new Google_Http_Request('http://localhost', 'HEAD'); 
  36. $resp->setResponseHttpCode('200'); 
  37. $resp->setResponseBody(null); 
  38. $resp->setResponseHeaders(array( 
  39. 'Cache-Control' => 'max-age=3600, must-revalidate',  
  40. 'Expires' => 'Fri, 30 Oct 2013 14:19:41 GMT',  
  41. 'Date' => 'Mon, 29 Jun 2011 02:28:12 GMT',  
  42. 'Last-Modified' => 'Mon, 29 Jun 2011 02:28:12 GMT',  
  43. 'ETag' => '3e86-410-3596fbbc',  
  44. )); 
  45. $result = Google_Http_CacheParser::isResponseCacheable($resp); 
  46. $this->assertTrue($result); 
  47.  
  48. // Verify that Vary: * cannot get cached. 
  49. $resp = new Google_Http_Request('http://localhost', 'GET'); 
  50. $resp->setResponseHttpCode('200'); 
  51. $resp->setResponseHeaders(array( 
  52. 'Cache-Control' => 'max-age=3600, must-revalidate',  
  53. 'Expires' => 'Fri, 30 Oct 2013 14:19:41 GMT',  
  54. 'Date' => 'Mon, 29 Jun 2011 02:28:12 GMT',  
  55. 'Last-Modified' => 'Mon, 29 Jun 2011 02:28:12 GMT',  
  56. 'Vary' => 'foo',  
  57. 'ETag' => '3e86-410-3596fbbc',  
  58. )); 
  59. $result = Google_Http_CacheParser::isResponseCacheable($resp); 
  60. $this->assertFalse($result); 
  61.  
  62. // Verify 201s cannot get cached. 
  63. $resp = new Google_Http_Request('http://localhost', 'GET'); 
  64. $resp->setResponseHttpCode('201'); 
  65. $resp->setResponseBody(null); 
  66. $resp->setResponseHeaders(array( 
  67. 'Cache-Control' => 'max-age=3600, must-revalidate',  
  68. 'Expires' => 'Fri, 30 Oct 2013 14:19:41 GMT',  
  69. 'Last-Modified' => 'Mon, 29 Jun 2011 02:28:12 GMT',  
  70. 'ETag' => '3e86-410-3596fbbc',  
  71. )); 
  72. $result = Google_Http_CacheParser::isResponseCacheable($resp); 
  73. $this->assertFalse($result); 
  74.  
  75. // Verify pragma: no-cache. 
  76. $resp = new Google_Http_Request('http://localhost', 'GET'); 
  77. $resp->setResponseHttpCode('200'); 
  78. $resp->setResponseHeaders(array( 
  79. 'Expires' => 'Wed, 11 Jan 2012 04:03:37 GMT',  
  80. 'Date' => 'Wed, 11 Jan 2012 04:03:37 GMT',  
  81. 'Pragma' => 'no-cache',  
  82. 'Cache-Control' => 'private, max-age=0, must-revalidate, no-transform',  
  83. 'ETag' => '3e86-410-3596fbbc',  
  84. )); 
  85. $result = Google_Http_CacheParser::isResponseCacheable($resp); 
  86. $this->assertFalse($result); 
  87.  
  88. // Verify Cache-Control: no-store. 
  89. $resp = new Google_Http_Request('http://localhost', 'GET'); 
  90. $resp->setResponseHttpCode('200'); 
  91. $resp->setResponseHeaders(array( 
  92. 'Expires' => 'Wed, 11 Jan 2012 04:03:37 GMT',  
  93. 'Date' => 'Wed, 11 Jan 2012 04:03:37 GMT',  
  94. 'Cache-Control' => 'no-store',  
  95. 'ETag' => '3e86-410-3596fbbc',  
  96. )); 
  97. $result = Google_Http_CacheParser::isResponseCacheable($resp); 
  98. $this->assertFalse($result); 
  99.  
  100. // Verify that authorized responses are not cacheable. 
  101. $resp = new Google_Http_Request('http://localhost', 'GET'); 
  102. $resp->setRequestHeaders(array('Authorization' => 'Bearer Token')); 
  103. $resp->setResponseHttpCode('200'); 
  104. $resp->setResponseHeaders(array( 
  105. 'Cache-Control' => 'max-age=3600, must-revalidate',  
  106. 'Expires' => 'Fri, 30 Oct 2013 14:19:41 GMT',  
  107. 'Last-Modified' => 'Mon, 29 Jun 2011 02:28:12 GMT',  
  108. 'ETag' => '3e86-410-3596fbbc',  
  109. )); 
  110. $result = Google_Http_CacheParser::isResponseCacheable($resp); 
  111. $this->assertFalse($result); 
  112.  
  113. public function testIsExpired() { 
  114. $now = time(); 
  115. $future = $now + (365 * 24 * 60 * 60); 
  116. $client = $this->getClient(); 
  117.  
  118. // Expires 1 year in the future. Response is fresh. 
  119. $resp = new Google_Http_Request('http://localhost', 'GET'); 
  120. $resp->setResponseHttpCode('200'); 
  121. $resp->setResponseHeaders(array( 
  122. 'Expires' => gmdate('D, d M Y H:i:s', $future) . ' GMT',  
  123. 'Date' => gmdate('D, d M Y H:i:s', $now) . ' GMT',  
  124. )); 
  125. $this->assertFalse(Google_Http_CacheParser::isExpired($resp)); 
  126.  
  127. // The response expires soon. Response is fresh. 
  128. $resp = new Google_Http_Request('http://localhost', 'GET'); 
  129. $resp->setResponseHttpCode('200'); 
  130. $resp->setResponseHeaders(array( 
  131. 'Expires' => gmdate('D, d M Y H:i:s', $now + 2) . ' GMT',  
  132. 'Date' => gmdate('D, d M Y H:i:s', $now) . ' GMT',  
  133. )); 
  134. $this->assertFalse(Google_Http_CacheParser::isExpired($resp)); 
  135.  
  136. // Expired 1 year ago. Response is stale. 
  137. $past = $now - (365 * 24 * 60 * 60); 
  138. $resp = new Google_Http_Request('http://localhost', 'GET'); 
  139. $resp->setResponseHttpCode('200'); 
  140. $resp->setResponseHeaders(array( 
  141. 'Expires' => gmdate('D, d M Y H:i:s', $past) . ' GMT',  
  142. 'Date' => gmdate('D, d M Y H:i:s', $now) . ' GMT',  
  143. )); 
  144. $this->assertTrue(Google_Http_CacheParser::isExpired($resp)); 
  145.  
  146. // Invalid expires header. Response is stale. 
  147. $resp = new Google_Http_Request('http://localhost', 'GET'); 
  148. $resp->setResponseHttpCode('200'); 
  149. $resp->setResponseHeaders(array( 
  150. 'Expires' => '-1',  
  151. 'Date' => gmdate('D, d M Y H:i:s', $now) . ' GMT',  
  152. )); 
  153. $this->assertTrue(Google_Http_CacheParser::isExpired($resp)); 
  154.  
  155. // The response expires immediately. G+ APIs do this. Response is stale. 
  156. $resp = new Google_Http_Request('http://localhost', 'GET'); 
  157. $resp->setResponseHttpCode('200'); 
  158. $resp->setResponseHeaders(array( 
  159. 'Expires' => gmdate('D, d M Y H:i:s', $now) . ' GMT',  
  160. 'Date' => gmdate('D, d M Y H:i:s', $now) . ' GMT',  
  161. )); 
  162. $this->assertTrue(Google_Http_CacheParser::isExpired($resp)); 
  163.  
  164. public function testMustRevalidate() { 
  165. $now = time(); 
  166. $client = $this->getClient(); 
  167.  
  168. // Expires 1 year in the future, and contains the must-revalidate directive. 
  169. // Don't revalidate. must-revalidate only applies to expired entries. 
  170. $future = $now + (365 * 24 * 60 * 60); 
  171. $resp = new Google_Http_Request('http://localhost', 'GET'); 
  172. $resp->setResponseHttpCode('200'); 
  173. $resp->setResponseHeaders(array( 
  174. 'Cache-Control' => 'max-age=3600, must-revalidate',  
  175. 'Expires' => gmdate('D, d M Y H:i:s', $future) . ' GMT',  
  176. 'Date' => gmdate('D, d M Y H:i:s', $now) . ' GMT',  
  177. )); 
  178. $this->assertFalse(Google_Http_CacheParser::mustRevalidate($resp)); 
  179.  
  180. // Contains the max-age=3600 directive, but was created 2 hours ago. 
  181. // Must revalidate. 
  182. $past = $now - (2 * 60 * 60); 
  183. $resp = new Google_Http_Request('http://localhost', 'GET'); 
  184. $resp->setResponseHttpCode('200'); 
  185. $resp->setResponseHeaders(array( 
  186. 'Cache-Control' => 'max-age=3600',  
  187. 'Expires' => gmdate('D, d M Y H:i:s', $future) . ' GMT',  
  188. 'Date' => gmdate('D, d M Y H:i:s', $past) . ' GMT',  
  189. )); 
  190. $this->assertTrue(Google_Http_CacheParser::mustRevalidate($resp)); 
  191.  
  192. // Contains the max-age=3600 directive, and was created 600 seconds ago. 
  193. // No need to revalidate, regardless of the expires header. 
  194. $past = $now - (600); 
  195. $resp = new Google_Http_Request('http://localhost', 'GET'); 
  196. $resp->setResponseHttpCode('200'); 
  197. $resp->setResponseHeaders(array( 
  198. 'Cache-Control' => 'max-age=3600',  
  199. 'Expires' => gmdate('D, d M Y H:i:s', $past) . ' GMT',  
  200. 'Date' => gmdate('D, d M Y H:i:s', $past) . ' GMT',  
  201. )); 
  202. $this->assertFalse(Google_Http_CacheParser::mustRevalidate($resp));