/test/general/ApiCacheParserTest.php

https://bitbucket.org/insigngmbh/googlephpapi · PHP · 225 lines · 167 code · 20 blank · 38 comment · 0 complexity · 50b608602c59470891824a3f1c95fa81 MD5 · raw file

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