1 29 package org.apache.commons.httpclient; 30 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 34 import junit.framework.Test; 35 import junit.framework.TestCase; 36 import junit.framework.TestSuite; 37 38 import org.apache.commons.httpclient.methods.GetMethod; 39 import org.apache.commons.httpclient.server.HttpRequestHandler; 40 import org.apache.commons.httpclient.server.RequestLine; 41 import org.apache.commons.httpclient.server.ResponseWriter; 42 import org.apache.commons.httpclient.server.SimpleHttpServer; 43 import org.apache.commons.httpclient.server.SimpleHttpServerConnection; 44 import org.apache.commons.httpclient.server.SimpleRequest; 45 46 57 public class TestBadContentLength extends TestCase { 58 private HttpClient client = null; 59 private SimpleHttpServer server = null; 60 61 public TestBadContentLength(String testName) { 63 super(testName); 64 } 65 66 public static void main(String args[]) { 68 String [] testCaseName = { TestBadContentLength.class.getName()}; 69 junit.textui.TestRunner.main(testCaseName); 70 } 71 72 74 public static Test suite() { 75 return new TestSuite(TestBadContentLength.class); 76 } 77 78 80 public void setUp() throws IOException { 81 client = new HttpClient(); 82 server = new SimpleHttpServer(); server.setTestname(getName()); 84 server.setRequestHandler(new MyHttpRequestHandler()); 85 } 86 87 public void tearDown() throws IOException { 88 client = null; 89 90 server.destroy(); 91 } 92 93 103 public void test1Lenient() throws IOException { 104 client.getParams().makeLenient(); 105 106 GetMethod m = 107 new GetMethod("http://localhost:" + server.getLocalPort() + "/"); 108 109 client.executeMethod(m); 110 assertEquals(200, m.getStatusCode()); 111 assertEquals("12345", m.getResponseBodyAsString()); 112 113 m = new GetMethod("http://localhost:" + server.getLocalPort() + "/"); 114 115 client.executeMethod(m); 116 assertEquals(200, m.getStatusCode()); 117 assertEquals("12345", m.getResponseBodyAsString()); 118 m.releaseConnection(); 119 } 120 121 139 public void test1Strict() throws IOException { 140 client.getParams().makeStrict(); 141 142 GetMethod m = 143 new GetMethod("http://localhost:" + server.getLocalPort() + "/"); 144 145 client.executeMethod(m); 146 assertEquals(200, m.getStatusCode()); 147 assertEquals("12345", m.getResponseBodyAsString()); 148 149 m = new GetMethod("http://localhost:" + server.getLocalPort() + "/"); 150 151 client.executeMethod(m); 152 assertEquals(200, m.getStatusCode()); 153 154 InputStream in = m.getResponseBodyAsStream(); 155 while (in.read() != -1) { 156 } 157 158 m.releaseConnection(); 159 } 160 161 public void enableThisTestForDebuggingOnly() 162 throws InterruptedException { 163 while (server.isRunning()) { 164 Thread.sleep(100); 165 } 166 } 167 168 private class MyHttpRequestHandler implements HttpRequestHandler { 169 private int requestNo = 0; 170 171 public boolean processRequest( 172 final SimpleHttpServerConnection conn, 173 final SimpleRequest request) throws IOException 174 { 175 RequestLine requestLine = request.getRequestLine(); 176 ResponseWriter out = conn.getWriter(); 177 if ("GET".equals(requestLine.getMethod()) 178 && "/".equals(requestLine.getUri())) { 179 180 requestNo++; 181 182 out.println("HTTP/1.1 200 OK"); 183 out.println("Content-Type: text/html"); 184 out.println("Content-Length: 5"); 185 out.println("Connection: keep-alive"); 186 out.println(); 187 out.println("12345"); 189 out.println("AND SOME MORE\r\nGARBAGE!"); 191 out.println("HTTP/1.0 404 Not Found"); 192 out.println("Content-Type: text/plain"); 193 out.println(""); 194 out.println("THIS-IS-A-FAKE-RESPONSE!"); 195 196 out.flush(); 197 if (requestNo < 2) { 199 conn.setKeepAlive(true); 200 } 201 } 202 return true; 203 } 204 } 205 206 } 207 | Popular Tags |