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).

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