1 package de.halbekunst.juplo.cachecontrol;
3 import com.meterware.httpunit.WebResponse;
4 import com.meterware.servletunit.InvocationContext;
5 import com.meterware.servletunit.ServletRunner;
6 import com.meterware.servletunit.ServletUnitClient;
7 import java.io.IOException;
8 import java.io.PrintWriter;
9 import java.util.Enumeration;
10 import java.util.HashMap;
12 import javax.servlet.ServletOutputStream;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15 import javax.servlet.http.HttpServletResponseWrapper;
16 import org.junit.Before;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
24 public abstract class HttpTestCase {
25 private final static Logger log = LoggerFactory.getLogger(HttpTestCase.class);
27 private ServletRunner sr;
28 ServletUnitClient client;
33 public void init() throws Exception {
34 sr = new ServletRunner(ParameterGuessingTest.class.getResourceAsStream("/web.xml"));
35 client = sr.newClient();
38 protected WebResponse executeRequest(String uri) throws Exception {
39 log.debug("---------- GET: {}", uri);
40 InvocationContext invocation = client.newInvocation(uri);
41 HttpServletRequest request = invocation.getRequest();
42 log.debug("Request - {}: {}", request.getMethod(), request.getProtocol());
43 Enumeration<String> headers = request.getHeaderNames();
44 while (headers.hasMoreElements()) {
45 String header = headers.nextElement();
46 Enumeration<String> values = request.getHeaders(header);
47 while (values.hasMoreElements())
48 log.debug("Request - {}: {}", header, values.nextElement());
50 log.debug("Invocing service method.");
53 * We cannot call invocation.service(), because we have to wrap the
54 * response. Therefore this was coppied from InvocationContextImpl.
56 TestHttpServletResponse wrappedResponse = new TestHttpServletResponse(invocation.getResponse());
57 if (invocation.isFilterActive()) {
58 invocation.getFilter().doFilter(invocation.getRequest(), wrappedResponse, invocation.getFilterChain());
61 invocation.getServlet().service(invocation.getRequest(), wrappedResponse);
64 WebResponse response = client.getResponse(invocation);
65 log.debug("Response - {}: {}", response.getResponseCode(), response.getResponseMessage());
66 log.debug("Response - {}, {} bytes", response.getContentType(), wrappedResponse.getCount());
67 for (String header : response.getHeaderFieldNames()) {
68 for (String value : response.getHeaderFields(header)) {
69 log.debug("Response - {}: {}", header, value);
76 class TestHttpServletResponse extends HttpServletResponseWrapper {
78 private CountingServletOutputStream out;
79 private HttpServletResponse response;
80 private ServletOutputStream stream;
81 private PrintWriter writer;
82 private boolean committed = false;
85 TestHttpServletResponse(HttpServletResponse response) {
87 this.response = response;
91 public long getCount() {
100 public void flushBuffer() throws IOException {
106 public int getBufferSize() {
111 public boolean isCommitted() {
116 public void reset() {
118 throw new IllegalStateException("call to reset() after response has been commited!");
125 public void resetBuffer() {
127 throw new IllegalStateException("call to resetBuffer() after response has been commited!");
134 public void setBufferSize(int size) {
135 if (out != null && out.count > 0)
136 throw new IllegalStateException("call to setBuffer() after content has been written!");
141 public ServletOutputStream getOutputStream() throws IOException {
144 throw new IllegalStateException("ServletOutputStream and PrintWriter cannot be requested both!");
146 if (stream == null) {
147 out = new CountingServletOutputStream(response.getOutputStream());
155 public PrintWriter getWriter() throws IOException {
158 throw new IllegalStateException("ServletOutputStream and PrintWriter cannot be requested both!");
160 if (writer == null) {
161 out = new CountingServletOutputStream(response.getOutputStream());
162 writer = new PrintWriter(out);
169 class CountingServletOutputStream extends ServletOutputStream {
171 private ServletOutputStream out;
175 CountingServletOutputStream(ServletOutputStream out) {
181 public void write(int i) throws IOException {
183 /** Simulate commit, when count is getting bigger then buffer */
184 if (count == buffer + 1) {
185 log.debug("simulating commit because buffer overflow! buffer: {}, count: {}", buffer, count);