Author: markt
Date: Mon Aug 24 08:22:21 2009
New Revision: 807113
URL: http://svn.apache.org/viewvc?rev=807113&view=rev
Log:
Extract the SimpleHttpClient in to a separate class and move it to a more
logical package since it will be reused.
Don't override method unnecessarily.
Added:
tomcat/trunk/test/org/apache/catalina/core/
tomcat/trunk/test/org/apache/catalina/core/TestStandardContext.java
tomcat/trunk/test/org/apache/catalina/startup/SimpleHttpClient.java
Modified:
tomcat/trunk/test/org/apache/catalina/connector/TestRequest.java
Modified: tomcat/trunk/test/org/apache/catalina/connector/TestRequest.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/connector/TestRequest.java?rev=807113&r1=807112&r2=807113&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/connector/TestRequest.java (original)
+++ tomcat/trunk/test/org/apache/catalina/connector/TestRequest.java Mon Aug 24
08:22:21 2009
@@ -17,20 +17,9 @@
package org.apache.catalina.connector;
-import java.io.BufferedReader;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
import java.io.PrintWriter;
-import java.io.Reader;
-import java.io.Writer;
-import java.net.Socket;
-import java.net.UnknownHostException;
-import java.util.ArrayList;
import java.util.Enumeration;
-import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@@ -38,6 +27,7 @@
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.core.StandardContext;
+import org.apache.catalina.startup.SimpleHttpClient;
import org.apache.catalina.startup.TestTomcatBase;
import org.apache.catalina.startup.Tomcat;
@@ -45,15 +35,6 @@
* Test case for {...@link Request}.
*/
public class TestRequest extends TestTomcatBase {
- @Override
- public void setUp() throws Exception {
- super.setUp();
- }
-
- @Override
- public void tearDown() throws Exception {
- super.tearDown();
- }
/**
* Test case for https://issues.apache.org/bugzilla/show_bug.cgi?id=37794
@@ -210,131 +191,4 @@
}
}
-
- /**
- * Simple client for unit testing. It isn't robust, it isn't secure and
- * should not be used as the basis for production code. Its only purpose
- * is to do the bare minimum for the unit tests.
- */
- private abstract static class SimpleHttpClient {
- public static final String TEMP_DIR =
- System.getProperty("java.io.tmpdir");
-
- public static final String CRLF = "\r\n";
-
- public static final String OK_200 = "HTTP/1.1 200";
- public static final String FAIL_500 = "HTTP/1.1 500";
-
- private Socket socket;
- private Writer writer;
- private BufferedReader reader;
- private int port = 8080;
-
- private String[] request;
- private int requestPause = 1000;
-
- private String responseLine;
- private List<String> responseHeaders = new ArrayList<String>();
- private String responseBody;
-
- public void setPort(int thePort) {
- port = thePort;
- }
-
- public void setRequest(String[] theRequest) {
- request = theRequest;
- }
-
- public void setRequestPause(int theRequestPause) {
- requestPause = theRequestPause;
- }
-
- public String getResponseLine() {
- return responseLine;
- }
-
- public List<String> getResponseHeaders() {
- return responseHeaders;
- }
-
- public String getResponseBody() {
- return responseBody;
- }
-
- public void connect() throws UnknownHostException, IOException {
- socket = new Socket("localhost", port);
- OutputStream os = socket.getOutputStream();
- writer = new OutputStreamWriter(os);
- InputStream is = socket.getInputStream();
- Reader r = new InputStreamReader(is);
- reader = new BufferedReader(r);
- }
-
- public void processRequest() throws IOException, InterruptedException {
- // Send the request
- boolean first = true;
- for (String requestPart : request) {
- if (first) {
- first = false;
- } else {
- Thread.sleep(requestPause);
- }
- writer.write(requestPart);
- writer.flush();
- }
-
- // Read the response
- responseLine = readLine();
-
- // Put the headers into the map
- String line = readLine();
- while (line.length() > 0) {
- responseHeaders.add(line);
- line = readLine();
- }
-
- // Read the body, if any
- StringBuilder builder = new StringBuilder();
- line = readLine();
- while (line != null && line.length() > 0) {
- builder.append(line);
- line = readLine();
- }
- responseBody = builder.toString();
-
- }
-
- public String readLine() throws IOException {
- return reader.readLine();
- }
-
- public void disconnect() throws IOException {
- writer.close();
- reader.close();
- socket.close();
- }
-
- public void reset() {
- socket = null;
- writer = null;
- reader = null;
-
- request = null;
- requestPause = 1000;
-
- responseLine = null;
- responseHeaders = new ArrayList<String>();
- responseBody = null;
- }
-
- public boolean isResponse200() {
- return getResponseLine().startsWith(OK_200);
- }
-
- public boolean isResponse500() {
- return getResponseLine().startsWith(FAIL_500);
- }
-
- public abstract boolean isResponseBodyOK();
- }
}
Added: tomcat/trunk/test/org/apache/catalina/core/TestStandardContext.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/core/TestStandardContext.java?rev=807113&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/core/TestStandardContext.java (added)
+++ tomcat/trunk/test/org/apache/catalina/core/TestStandardContext.java Mon Aug
24 08:22:21 2009
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.catalina.core;
+
+import java.io.File;
+import java.io.IOException;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+import org.apache.catalina.deploy.FilterDef;
+import org.apache.catalina.deploy.FilterMap;
+import org.apache.catalina.startup.SimpleHttpClient;
+import org.apache.catalina.startup.TestTomcatBase;
+import org.apache.catalina.startup.Tomcat;
+
+public class TestStandardContext extends TestTomcatBase {
+
+ private static final String REQUEST =
+ "GET / HTTP/1.1\r\n" +
+ "Host: anything\r\n" +
+ "Connection: close\r\n" +
+ "\r\n";
+
+ public void testBug46243() throws Exception {
+
+ // Set up a container
+ Tomcat tomcat = getTomcatInstance();
+
+ File docBase = new File(tomcat.getHost().getAppBase(), "ROOT");
+ docBase.mkdirs();
+
+ StandardContext root = tomcat.addContext("", "ROOT");
+
+ // Add test a filter that fails
+ FilterDef filterDef = new FilterDef();
+ filterDef.setFilterClass(Bug46243Filter.class.getName());
+ filterDef.setFilterName("Bug46243");
+ root.addFilterDef(filterDef);
+ FilterMap filterMap = new FilterMap();
+ filterMap.setFilterName("Bug46243");
+ filterMap.addURLPattern("*");
+ root.addFilterMap(filterMap);
+
+ // Add a test servlet so there is something to generate a response if
+ // it works (although it shouldn't)
+ Tomcat.addServlet(root, "Bug46243", new HelloWorldServlet());
+ root.addServletMapping("/", "Bug46243");
+
+ tomcat.start();
+
+ // Configure the client
+ Bug46243Client client = new Bug46243Client();
+ client.setPort(getPort());
+ client.setRequest(new String[] { REQUEST });
+
+ client.connect();
+ client.processRequest();
+ assertTrue(client.isResponse404());
+ }
+
+ private static final class Bug46243Client extends SimpleHttpClient {
+ public boolean isResponseBodyOK() {
+ // Don't care about the body in this test
+ return true;
+ }
+ }
+
+ public static final class Bug46243Filter implements Filter {
+
+ @Override
+ public void destroy() {
+ // NOOP
+ }
+
+ @Override
+ public void doFilter(ServletRequest request, ServletResponse response,
+ FilterChain chain) throws IOException, ServletException {
+ // If it works, do nothing
+ chain.doFilter(request, response);
+ }
+
+ @Override
+ public void init(FilterConfig filterConfig) throws ServletException {
+ throw new ServletException("Init fail", new
ClassNotFoundException());
+ }
+
+ }
+}
Added: tomcat/trunk/test/org/apache/catalina/startup/SimpleHttpClient.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/SimpleHttpClient.java?rev=807113&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/startup/SimpleHttpClient.java (added)
+++ tomcat/trunk/test/org/apache/catalina/startup/SimpleHttpClient.java Mon Aug
24 08:22:21 2009
@@ -0,0 +1,147 @@
+package org.apache.catalina.startup;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.Writer;
+import java.net.Socket;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Simple client for unit testing. It isn't robust, it isn't secure and
+ * should not be used as the basis for production code. Its only purpose
+ * is to do the bare minimum for the unit tests. It does not support keep-alive
+ * connections - make sure you send a Connection: close header with the
request.
+ */
+public abstract class SimpleHttpClient {
+ public static final String TEMP_DIR =
+ System.getProperty("java.io.tmpdir");
+
+ public static final String CRLF = "\r\n";
+
+ public static final String OK_200 = "HTTP/1.1 200";
+ public static final String FAIL_404 = "HTTP/1.1 404";
+ public static final String FAIL_500 = "HTTP/1.1 500";
+
+ private Socket socket;
+ private Writer writer;
+ private BufferedReader reader;
+ private int port = 8080;
+
+ private String[] request;
+ private int requestPause = 1000;
+
+ private String responseLine;
+ private List<String> responseHeaders = new ArrayList<String>();
+ private String responseBody;
+
+ public void setPort(int thePort) {
+ port = thePort;
+ }
+
+ public void setRequest(String[] theRequest) {
+ request = theRequest;
+ }
+
+ public void setRequestPause(int theRequestPause) {
+ requestPause = theRequestPause;
+ }
+
+ public String getResponseLine() {
+ return responseLine;
+ }
+
+ public List<String> getResponseHeaders() {
+ return responseHeaders;
+ }
+
+ public String getResponseBody() {
+ return responseBody;
+ }
+
+ public void connect() throws UnknownHostException, IOException {
+ socket = new Socket("localhost", port);
+ OutputStream os = socket.getOutputStream();
+ writer = new OutputStreamWriter(os);
+ InputStream is = socket.getInputStream();
+ Reader r = new InputStreamReader(is);
+ reader = new BufferedReader(r);
+ }
+
+ public void processRequest() throws IOException, InterruptedException {
+ // Send the request
+ boolean first = true;
+ for (String requestPart : request) {
+ if (first) {
+ first = false;
+ } else {
+ Thread.sleep(requestPause);
+ }
+ writer.write(requestPart);
+ writer.flush();
+ }
+
+ // Read the response
+ responseLine = readLine();
+
+ // Put the headers into the map
+ String line = readLine();
+ while (line.length() > 0) {
+ responseHeaders.add(line);
+ line = readLine();
+ }
+
+ // Read the body, if any
+ StringBuilder builder = new StringBuilder();
+ line = readLine();
+ while (line != null && line.length() > 0) {
+ builder.append(line);
+ line = readLine();
+ }
+ responseBody = builder.toString();
+
+ }
+
+ public String readLine() throws IOException {
+ return reader.readLine();
+ }
+
+ public void disconnect() throws IOException {
+ writer.close();
+ reader.close();
+ socket.close();
+ }
+
+ public void reset() {
+ socket = null;
+ writer = null;
+ reader = null;
+
+ request = null;
+ requestPause = 1000;
+
+ responseLine = null;
+ responseHeaders = new ArrayList<String>();
+ responseBody = null;
+ }
+
+ public boolean isResponse200() {
+ return getResponseLine().startsWith(OK_200);
+ }
+
+ public boolean isResponse404() {
+ return getResponseLine().startsWith(FAIL_404);
+ }
+
+ public boolean isResponse500() {
+ return getResponseLine().startsWith(FAIL_500);
+ }
+
+ public abstract boolean isResponseBodyOK();
+}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]