PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/org/apache/solr/servlet/CacheHeaderTest.java

https://github.com/adsabs/solr-affiliation-disambiguation
Java | 246 lines | 176 code | 37 blank | 33 comment | 4 complexity | 6bd53527ed1318607a310d512d92e39b MD5 | raw file
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.solr.servlet;
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.OutputStreamWriter;
  21. import java.io.Writer;
  22. import java.util.Date;
  23. import org.apache.commons.httpclient.Header;
  24. import org.apache.commons.httpclient.HttpMethodBase;
  25. import org.apache.commons.httpclient.NameValuePair;
  26. import org.apache.commons.httpclient.util.DateUtil;
  27. /**
  28. * A test case for the several HTTP cache headers emitted by Solr
  29. */
  30. public class CacheHeaderTest extends CacheHeaderTestBase {
  31. @Override
  32. public String getSolrConfigFilename() {
  33. return "solrconfig.xml";
  34. }
  35. protected static final String CHARSET = "UTF-8";
  36. protected static final String CONTENTS = "id\n100\n101\n102";
  37. public void testCacheVetoHandler() throws Exception {
  38. File f=makeFile(CONTENTS);
  39. HttpMethodBase m=getUpdateMethod("GET");
  40. m.setQueryString(new NameValuePair[] { new NameValuePair("stream.file",f.getCanonicalPath())});
  41. getClient().executeMethod(m);
  42. assertEquals(200, m.getStatusCode());
  43. checkVetoHeaders(m);
  44. }
  45. public void testCacheVetoException() throws Exception {
  46. HttpMethodBase m = getSelectMethod("GET");
  47. // We force an exception from Solr. This should emit "no-cache" HTTP headers
  48. m.setQueryString(new NameValuePair[] { new NameValuePair("q", "xyz:solr"),
  49. new NameValuePair("qt", "standard") });
  50. getClient().executeMethod(m);
  51. assertFalse(m.getStatusCode() == 200);
  52. checkVetoHeaders(m);
  53. }
  54. protected void checkVetoHeaders(HttpMethodBase m) throws Exception {
  55. Header head = m.getResponseHeader("Cache-Control");
  56. assertNotNull("We got no Cache-Control header", head);
  57. assertEquals("no-cache, no-store", head.getValue());
  58. head = m.getResponseHeader("Pragma");
  59. assertNotNull("We got no Pragma header", head);
  60. assertEquals("no-cache", head.getValue());
  61. head = m.getResponseHeader("Expires");
  62. assertNotNull("We got no Expires header", head);
  63. Date d = DateUtil.parseDate(head.getValue());
  64. assertTrue("We got no Expires header far in the past", System
  65. .currentTimeMillis()
  66. - d.getTime() > 100000);
  67. }
  68. protected void doLastModified(String method) throws Exception {
  69. // We do a first request to get the last modified
  70. // This must result in a 200 OK response
  71. HttpMethodBase get = getSelectMethod(method);
  72. getClient().executeMethod(get);
  73. checkResponseBody(method, get);
  74. assertEquals("Got no response code 200 in initial request", 200, get
  75. .getStatusCode());
  76. Header head = get.getResponseHeader("Last-Modified");
  77. assertNotNull("We got no Last-Modified header", head);
  78. Date lastModified = DateUtil.parseDate(head.getValue());
  79. // If-Modified-Since tests
  80. get = getSelectMethod(method);
  81. get.addRequestHeader("If-Modified-Since", DateUtil.formatDate(new Date()));
  82. getClient().executeMethod(get);
  83. checkResponseBody(method, get);
  84. assertEquals("Expected 304 NotModified response with current date", 304,
  85. get.getStatusCode());
  86. get = getSelectMethod(method);
  87. get.addRequestHeader("If-Modified-Since", DateUtil.formatDate(new Date(
  88. lastModified.getTime() - 10000)));
  89. getClient().executeMethod(get);
  90. checkResponseBody(method, get);
  91. assertEquals("Expected 200 OK response with If-Modified-Since in the past",
  92. 200, get.getStatusCode());
  93. // If-Unmodified-Since tests
  94. get = getSelectMethod(method);
  95. get.addRequestHeader("If-Unmodified-Since", DateUtil.formatDate(new Date(
  96. lastModified.getTime() - 10000)));
  97. getClient().executeMethod(get);
  98. checkResponseBody(method, get);
  99. assertEquals(
  100. "Expected 412 Precondition failed with If-Unmodified-Since in the past",
  101. 412, get.getStatusCode());
  102. get = getSelectMethod(method);
  103. get
  104. .addRequestHeader("If-Unmodified-Since", DateUtil
  105. .formatDate(new Date()));
  106. getClient().executeMethod(get);
  107. checkResponseBody(method, get);
  108. assertEquals(
  109. "Expected 200 OK response with If-Unmodified-Since and current date",
  110. 200, get.getStatusCode());
  111. }
  112. // test ETag
  113. protected void doETag(String method) throws Exception {
  114. HttpMethodBase get = getSelectMethod(method);
  115. getClient().executeMethod(get);
  116. checkResponseBody(method, get);
  117. assertEquals("Got no response code 200 in initial request", 200, get
  118. .getStatusCode());
  119. Header head = get.getResponseHeader("ETag");
  120. assertNotNull("We got no ETag in the response", head);
  121. assertTrue("Not a valid ETag", head.getValue().startsWith("\"")
  122. && head.getValue().endsWith("\""));
  123. String etag = head.getValue();
  124. // If-None-Match tests
  125. // we set a non matching ETag
  126. get = getSelectMethod(method);
  127. get.addRequestHeader("If-None-Match", "\"xyz123456\"");
  128. getClient().executeMethod(get);
  129. checkResponseBody(method, get);
  130. assertEquals(
  131. "If-None-Match: Got no response code 200 in response to non matching ETag",
  132. 200, get.getStatusCode());
  133. // now we set matching ETags
  134. get = getSelectMethod(method);
  135. get.addRequestHeader("If-None-Match", "\"xyz1223\"");
  136. get.addRequestHeader("If-None-Match", "\"1231323423\", \"1211211\", "
  137. + etag);
  138. getClient().executeMethod(get);
  139. checkResponseBody(method, get);
  140. assertEquals("If-None-Match: Got no response 304 to matching ETag", 304,
  141. get.getStatusCode());
  142. // we now set the special star ETag
  143. get = getSelectMethod(method);
  144. get.addRequestHeader("If-None-Match", "*");
  145. getClient().executeMethod(get);
  146. checkResponseBody(method, get);
  147. assertEquals("If-None-Match: Got no response 304 for star ETag", 304, get
  148. .getStatusCode());
  149. // If-Match tests
  150. // we set a non matching ETag
  151. get = getSelectMethod(method);
  152. get.addRequestHeader("If-Match", "\"xyz123456\"");
  153. getClient().executeMethod(get);
  154. checkResponseBody(method, get);
  155. assertEquals(
  156. "If-Match: Got no response code 412 in response to non matching ETag",
  157. 412, get.getStatusCode());
  158. // now we set matching ETags
  159. get = getSelectMethod(method);
  160. get.addRequestHeader("If-Match", "\"xyz1223\"");
  161. get.addRequestHeader("If-Match", "\"1231323423\", \"1211211\", " + etag);
  162. getClient().executeMethod(get);
  163. checkResponseBody(method, get);
  164. assertEquals("If-Match: Got no response 200 to matching ETag", 200, get
  165. .getStatusCode());
  166. // now we set the special star ETag
  167. get = getSelectMethod(method);
  168. get.addRequestHeader("If-Match", "*");
  169. getClient().executeMethod(get);
  170. checkResponseBody(method, get);
  171. assertEquals("If-Match: Got no response 200 to star ETag", 200, get
  172. .getStatusCode());
  173. }
  174. protected void doCacheControl(String method) throws Exception {
  175. if ("POST".equals(method)) {
  176. HttpMethodBase m = getSelectMethod(method);
  177. getClient().executeMethod(m);
  178. checkResponseBody(method, m);
  179. Header head = m.getResponseHeader("Cache-Control");
  180. assertNull("We got a cache-control header in response to POST", head);
  181. head = m.getResponseHeader("Expires");
  182. assertNull("We got an Expires header in response to POST", head);
  183. } else {
  184. HttpMethodBase m = getSelectMethod(method);
  185. getClient().executeMethod(m);
  186. checkResponseBody(method, m);
  187. Header head = m.getResponseHeader("Cache-Control");
  188. assertNotNull("We got no cache-control header", head);
  189. head = m.getResponseHeader("Expires");
  190. assertNotNull("We got no Expires header in response", head);
  191. }
  192. }
  193. protected File makeFile(String contents) {
  194. return makeFile(contents, CHARSET);
  195. }
  196. protected File makeFile(String contents, String charset) {
  197. try {
  198. File f = File.createTempFile(getClass().getName(),"csv");
  199. f.deleteOnExit();
  200. Writer out = new OutputStreamWriter(new FileOutputStream(f),
  201. charset);
  202. out.write(contents);
  203. out.close();
  204. return f;
  205. } catch (Exception e) {
  206. throw new RuntimeException(e);
  207. }
  208. }
  209. }