Added: tomcat/trunk/test/org/apache/catalina/connector/TestKeepAliveCount.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/connector/TestKeepAliveCount.java?rev=823234&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/connector/TestKeepAliveCount.java 
(added)
+++ tomcat/trunk/test/org/apache/catalina/connector/TestKeepAliveCount.java Thu 
Oct  8 17:08:20 2009
@@ -0,0 +1,131 @@
+/*
+ *  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.connector;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+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;
+
+public class TestKeepAliveCount extends TestTomcatBase{
+
+    public void testHttp10() throws Exception {
+        TestKeepAliveClient client = new TestKeepAliveClient();
+        client.doHttp10Request();
+    }
+    
+    public void testHttp11() throws Exception {
+        TestKeepAliveClient client = new TestKeepAliveClient();
+        client.doHttp11Request();
+    }
+ 
+    
+    private class TestKeepAliveClient extends SimpleHttpClient {
+
+
+        private boolean init;
+        
+        private synchronized void init() throws Exception {
+            if (init) return;
+            
+            Tomcat tomcat = getTomcatInstance();
+            StandardContext root = tomcat.addContext("", TEMP_DIR);
+            Tomcat.addServlet(root, "Simple", new SimpleServlet());
+            root.addServletMapping("/test", "Simple");
+            tomcat.getConnector().setProperty("maxKeepAliveRequests", "5");
+            tomcat.getConnector().setProperty("soTimeout", "20000");
+            tomcat.getConnector().setProperty("keepAliveTimeout", "50000");
+            tomcat.getConnector().setProperty("port", "8080");
+            init = true;
+        }
+        
+        private void doHttp10Request() throws Exception {
+            Tomcat tomcat = getTomcatInstance();
+            init();
+            tomcat.start();
+            // Open connection
+            connect();
+            
+            // Send request in two parts
+            String[] request = new String[1];
+            request[0] =
+                "GET /test HTTP/1.0" + CRLF + CRLF;
+            setRequest(request);
+            processRequest(false); // blocks until response has been read
+            boolean passed = (this.readLine()==null);
+            // Close the connection
+            disconnect();
+            reset();
+            tomcat.stop();
+            assertTrue(passed);
+        }
+        
+        private void doHttp11Request() throws Exception {
+            Tomcat tomcat = getTomcatInstance();
+            init();
+            tomcat.start();
+            // Open connection
+            connect();
+            
+            // Send request in two parts
+            String[] request = new String[1];
+            request[0] =
+                "GET /test HTTP/1.1" + CRLF + 
+                "Host: localhost" + CRLF +
+                "Connection: Keep-Alive" + CRLF+
+                "Keep-Alive: 300"+ CRLF+ CRLF;
+            
+            setRequest(request);
+            
+            for (int i=0; i<5; i++) {
+                processRequest(false); // blocks until response has been read
+                assertTrue(getResponseLine()!=null && 
getResponseLine().trim().startsWith("HTTP/1.1 200"));
+            }
+            boolean passed = (this.readLine()==null);
+            // Close the connection
+            disconnect();
+            reset();
+            tomcat.stop();
+            assertTrue(passed);
+        }
+        
+        @Override
+        public boolean isResponseBodyOK() {
+            return true;
+        }
+        
+    }
+    
+    
+    private static class SimpleServlet extends HttpServlet {
+
+        @Override
+        protected void service(HttpServletRequest req, HttpServletResponse 
resp) throws ServletException, IOException {
+            resp.setContentLength(0);
+            resp.flushBuffer();
+        }
+        
+    }
+    
+}

Modified: 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=823234&r1=823233&r2=823234&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/startup/SimpleHttpClient.java 
(original)
+++ tomcat/trunk/test/org/apache/catalina/startup/SimpleHttpClient.java Thu Oct 
 8 17:08:20 2009
@@ -75,6 +75,9 @@
     }
     
     public void processRequest() throws IOException, InterruptedException {
+        processRequest(true);
+    }
+    public void processRequest(boolean readBody) throws IOException, 
InterruptedException {
         // Send the request
         boolean first = true;
         for (String requestPart : request) {
@@ -92,17 +95,19 @@
         
         // Put the headers into the map
         String line = readLine();
-        while (line.length() > 0) {
+        while (line!=null && 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);
+        if (readBody) {
             line = readLine();
+            while (line != null && line.length() > 0) {
+                builder.append(line);
+                line = readLine();
+            }
         }
         responseBody = builder.toString();
 
@@ -142,6 +147,10 @@
     public boolean isResponse500() {
         return getResponseLine().startsWith(FAIL_500);
     }
+    
+    public Socket getSocket() {
+        return socket;
+    }
 
     public abstract boolean isResponseBodyOK();
 }
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to