ApiCacheParserTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. class ApiCacheParserTest extends BaseTest
  21. {
  22. public function testIsResponseCacheable()
  23. {
  24. $client = $this->getClient();
  25. $resp = new Google_Http_Request('http://localhost', 'POST');
  26. $result = Google_Http_CacheParser::isResponseCacheable($resp);
  27. $this->assertFalse($result);
  28. // The response has expired, and we don't have an etag for
  29. // revalidation.
  30. $resp = new Google_Http_Request('http://localhost', 'GET');
  31. $resp->setResponseHttpCode('200');
  32. $resp->setResponseHeaders(
  33. array(
  34. 'Cache-Control' => 'max-age=3600, must-revalidate',
  35. 'Expires' => 'Fri, 30 Oct 1998 14:19:41 GMT',
  36. 'Date' => 'Mon, 29 Jun 1998 02:28:12 GMT',
  37. 'Last-Modified' => 'Mon, 29 Jun 1998 02:28:12 GMT',
  38. )
  39. );
  40. $result = Google_Http_CacheParser::isResponseCacheable($resp);
  41. $this->assertFalse($result);
  42. // Verify cacheable responses.
  43. $resp = new Google_Http_Request('http://localhost', 'GET');
  44. $resp->setResponseHttpCode('200');
  45. $resp->setResponseHeaders(
  46. array(
  47. 'Cache-Control' => 'max-age=3600, must-revalidate',
  48. 'Expires' => 'Fri, 30 Oct 2013 14:19:41 GMT',
  49. 'Date' => 'Mon, 29 Jun 2011 02:28:12 GMT',
  50. 'Last-Modified' => 'Mon, 29 Jun 2011 02:28:12 GMT',
  51. 'ETag' => '3e86-410-3596fbbc',
  52. )
  53. );
  54. $result = Google_Http_CacheParser::isResponseCacheable($resp);
  55. $this->assertTrue($result);
  56. // Verify that responses to HEAD requests are cacheable.
  57. $resp = new Google_Http_Request('http://localhost', 'HEAD');
  58. $resp->setResponseHttpCode('200');
  59. $resp->setResponseBody(null);
  60. $resp->setResponseHeaders(
  61. array(
  62. 'Cache-Control' => 'max-age=3600, must-revalidate',
  63. 'Expires' => 'Fri, 30 Oct 2013 14:19:41 GMT',
  64. 'Date' => 'Mon, 29 Jun 2011 02:28:12 GMT',
  65. 'Last-Modified' => 'Mon, 29 Jun 2011 02:28:12 GMT',
  66. 'ETag' => '3e86-410-3596fbbc',
  67. )
  68. );
  69. $result = Google_Http_CacheParser::isResponseCacheable($resp);
  70. $this->assertTrue($result);
  71. // Verify that Vary: * cannot get cached.
  72. $resp = new Google_Http_Request('http://localhost', 'GET');
  73. $resp->setResponseHttpCode('200');
  74. $resp->setResponseHeaders(
  75. array(
  76. 'Cache-Control' => 'max-age=3600, must-revalidate',
  77. 'Expires' => 'Fri, 30 Oct 2013 14:19:41 GMT',
  78. 'Date' => 'Mon, 29 Jun 2011 02:28:12 GMT',
  79. 'Last-Modified' => 'Mon, 29 Jun 2011 02:28:12 GMT',
  80. 'Vary' => 'foo',
  81. 'ETag' => '3e86-410-3596fbbc',
  82. )
  83. );
  84. $result = Google_Http_CacheParser::isResponseCacheable($resp);
  85. $this->assertFalse($result);
  86. // Verify 201s cannot get cached.
  87. $resp = new Google_Http_Request('http://localhost', 'GET');
  88. $resp->setResponseHttpCode('201');
  89. $resp->setResponseBody(null);
  90. $resp->setResponseHeaders(
  91. array(
  92. 'Cache-Control' => 'max-age=3600, must-revalidate',
  93. 'Expires' => 'Fri, 30 Oct 2013 14:19:41 GMT',
  94. 'Last-Modified' => 'Mon, 29 Jun 2011 02:28:12 GMT',
  95. 'ETag' => '3e86-410-3596fbbc',
  96. )
  97. );
  98. $result = Google_Http_CacheParser::isResponseCacheable($resp);
  99. $this->assertFalse($result);
  100. // Verify pragma: no-cache.
  101. $resp = new Google_Http_Request('http://localhost', 'GET');
  102. $resp->setResponseHttpCode('200');
  103. $resp->setResponseHeaders(
  104. array(
  105. 'Expires' => 'Wed, 11 Jan 2012 04:03:37 GMT',
  106. 'Date' => 'Wed, 11 Jan 2012 04:03:37 GMT',
  107. 'Pragma' => 'no-cache',
  108. 'Cache-Control' => 'private, max-age=0, must-revalidate, no-transform',
  109. 'ETag' => '3e86-410-3596fbbc',
  110. )
  111. );
  112. $result = Google_Http_CacheParser::isResponseCacheable($resp);
  113. $this->assertFalse($result);
  114. // Verify Cache-Control: no-store.
  115. $resp = new Google_Http_Request('http://localhost', 'GET');
  116. $resp->setResponseHttpCode('200');
  117. $resp->setResponseHeaders(
  118. array(
  119. 'Expires' => 'Wed, 11 Jan 2012 04:03:37 GMT',
  120. 'Date' => 'Wed, 11 Jan 2012 04:03:37 GMT',
  121. 'Cache-Control' => 'no-store',
  122. 'ETag' => '3e86-410-3596fbbc',
  123. )
  124. );
  125. $result = Google_Http_CacheParser::isResponseCacheable($resp);
  126. $this->assertFalse($result);
  127. // Verify that authorized responses are not cacheable.
  128. $resp = new Google_Http_Request('http://localhost', 'GET');
  129. $resp->setRequestHeaders(array('Authorization' => 'Bearer Token'));
  130. $resp->setResponseHttpCode('200');
  131. $resp->setResponseHeaders(
  132. array(
  133. 'Cache-Control' => 'max-age=3600, must-revalidate',
  134. 'Expires' => 'Fri, 30 Oct 2013 14:19:41 GMT',
  135. 'Last-Modified' => 'Mon, 29 Jun 2011 02:28:12 GMT',
  136. 'ETag' => '3e86-410-3596fbbc',
  137. )
  138. );
  139. $result = Google_Http_CacheParser::isResponseCacheable($resp);
  140. $this->assertFalse($result);
  141. }
  142. public function testIsExpired()
  143. {
  144. $now = time();
  145. $future = $now + (365 * 24 * 60 * 60);
  146. $client = $this->getClient();
  147. // Expires 1 year in the future. Response is fresh.
  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', $future) . ' GMT',
  153. 'Date' => gmdate('D, d M Y H:i:s', $now) . ' GMT',
  154. )
  155. );
  156. $this->assertFalse(Google_Http_CacheParser::isExpired($resp));
  157. // The response expires soon. Response is fresh.
  158. $resp = new Google_Http_Request('http://localhost', 'GET');
  159. $resp->setResponseHttpCode('200');
  160. $resp->setResponseHeaders(
  161. array(
  162. 'Expires' => gmdate('D, d M Y H:i:s', $now + 2) . ' GMT',
  163. 'Date' => gmdate('D, d M Y H:i:s', $now) . ' GMT',
  164. )
  165. );
  166. $this->assertFalse(Google_Http_CacheParser::isExpired($resp));
  167. // Expired 1 year ago. Response is stale.
  168. $past = $now - (365 * 24 * 60 * 60);
  169. $resp = new Google_Http_Request('http://localhost', 'GET');
  170. $resp->setResponseHttpCode('200');
  171. $resp->setResponseHeaders(
  172. array(
  173. 'Expires' => gmdate('D, d M Y H:i:s', $past) . ' GMT',
  174. 'Date' => gmdate('D, d M Y H:i:s', $now) . ' GMT',
  175. )
  176. );
  177. $this->assertTrue(Google_Http_CacheParser::isExpired($resp));
  178. // Invalid expires header. Response is stale.
  179. $resp = new Google_Http_Request('http://localhost', 'GET');
  180. $resp->setResponseHttpCode('200');
  181. $resp->setResponseHeaders(
  182. array(
  183. 'Expires' => '-1',
  184. 'Date' => gmdate('D, d M Y H:i:s', $now) . ' GMT',
  185. )
  186. );
  187. $this->assertTrue(Google_Http_CacheParser::isExpired($resp));
  188. // The response expires immediately. G+ APIs do this. Response is stale.
  189. $resp = new Google_Http_Request('http://localhost', 'GET');
  190. $resp->setResponseHttpCode('200');
  191. $resp->setResponseHeaders(
  192. array(
  193. 'Expires' => gmdate('D, d M Y H:i:s', $now) . ' GMT',
  194. 'Date' => gmdate('D, d M Y H:i:s', $now) . ' GMT',
  195. )
  196. );
  197. $this->assertTrue(Google_Http_CacheParser::isExpired($resp));
  198. }
  199. public function testMustRevalidate()
  200. {
  201. $now = time();
  202. $client = $this->getClient();
  203. // Expires 1 year in the future, and contains the must-revalidate directive.
  204. // Don't revalidate. must-revalidate only applies to expired entries.
  205. $future = $now + (365 * 24 * 60 * 60);
  206. $resp = new Google_Http_Request('http://localhost', 'GET');
  207. $resp->setResponseHttpCode('200');
  208. $resp->setResponseHeaders(
  209. array(
  210. 'Cache-Control' => 'max-age=3600, must-revalidate',
  211. 'Expires' => gmdate('D, d M Y H:i:s', $future) . ' GMT',
  212. 'Date' => gmdate('D, d M Y H:i:s', $now) . ' GMT',
  213. )
  214. );
  215. $this->assertFalse(Google_Http_CacheParser::mustRevalidate($resp));
  216. // Contains the max-age=3600 directive, but was created 2 hours ago.
  217. // Must revalidate.
  218. $past = $now - (2 * 60 * 60);
  219. $resp = new Google_Http_Request('http://localhost', 'GET');
  220. $resp->setResponseHttpCode('200');
  221. $resp->setResponseHeaders(
  222. array(
  223. 'Cache-Control' => 'max-age=3600',
  224. 'Expires' => gmdate('D, d M Y H:i:s', $future) . ' GMT',
  225. 'Date' => gmdate('D, d M Y H:i:s', $past) . ' GMT',
  226. )
  227. );
  228. $this->assertTrue(Google_Http_CacheParser::mustRevalidate($resp));
  229. // Contains the max-age=3600 directive, and was created 600 seconds ago.
  230. // No need to revalidate, regardless of the expires header.
  231. $past = $now - (600);
  232. $resp = new Google_Http_Request('http://localhost', 'GET');
  233. $resp->setResponseHttpCode('200');
  234. $resp->setResponseHeaders(
  235. array(
  236. 'Cache-Control' => 'max-age=3600',
  237. 'Expires' => gmdate('D, d M Y H:i:s', $past) . ' GMT',
  238. 'Date' => gmdate('D, d M Y H:i:s', $past) . ' GMT',
  239. )
  240. );
  241. $this->assertFalse(Google_Http_CacheParser::mustRevalidate($resp));
  242. }
  243. }