1 20 package org.apache.cactus.integration.ant.container; 21 22 import java.io.BufferedReader ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.InputStreamReader ; 26 import java.io.OutputStream ; 27 import java.io.StringReader ; 28 import java.net.ConnectException ; 29 import java.net.ServerSocket ; 30 import java.net.Socket ; 31 import java.util.Random ; 32 import java.util.StringTokenizer ; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 37 import junit.framework.Assert; 38 39 45 public final class MockHttpServer implements Runnable 46 { 47 48 50 53 private int port; 54 55 58 private String response; 59 60 63 private volatile boolean stopFlag = false; 64 65 68 private String actualMethod; 69 70 73 private String expectedMethod; 74 75 78 private String actualUri; 79 80 83 private String expectedUri; 84 85 88 private int expectedRequestCount = -1; 89 90 93 private int actualRequestCount = 0; 94 95 98 private Log log = LogFactory.getLog(MockHttpServer.class); 99 100 102 107 public MockHttpServer(int thePort) 108 { 109 if (thePort <= 0) 110 { 111 throw new IllegalArgumentException ("Invalid port number"); 112 } 113 this.port = thePort; 114 } 115 116 118 122 public void run() 123 { 124 if (this.response == null) 125 { 126 throw new IllegalStateException ("Response content not set"); 127 } 128 129 try 130 { 131 ServerSocket serverSocket = new ServerSocket (port); 132 while (!this.stopFlag) 133 { 134 Socket socket = serverSocket.accept(); 135 try 136 { 137 if (!this.stopFlag) 138 { 139 processRequest(socket); 140 } 141 } 142 catch (IOException ioe) 143 { 144 this.log.error("Couldn't process request", ioe); 145 } 146 finally 147 { 148 socket.close(); 149 } 150 } 151 serverSocket.close(); 152 } 153 catch (IOException ioe) 154 { 155 this.log.error("Problem with server socket", ioe); 156 } 157 } 158 159 161 166 public void expectMethod(String theMethod) 167 { 168 this.expectedMethod = theMethod; 169 } 170 171 176 public void expectRequestCount(int theRequestCount) 177 { 178 this.expectedRequestCount = theRequestCount; 179 } 180 181 186 public void expectUri(String theUri) 187 { 188 this.expectedUri = theUri; 189 } 190 191 196 public int getPort() 197 { 198 return this.port; 199 } 200 201 206 public boolean isStopped() 207 { 208 return this.stopFlag; 209 } 210 211 216 public void setResponse(String theResponse) 217 { 218 this.response = theResponse; 219 } 220 221 224 public void stop() 225 { 226 this.stopFlag = true; 227 try 228 { 229 Socket sock = new Socket ("localhost", this.port); 230 sock.getOutputStream().write("SHUTDOWN\n".getBytes()); 231 } 232 catch (IOException ioe) 233 { 234 this.log.error("Error while trying to stop", ioe); 235 } 236 } 237 238 241 public void verify() 242 { 243 if (this.expectedRequestCount >= 0) 244 { 245 Assert.assertTrue("Expected " + this.expectedRequestCount 246 + " requests, but got " + this.actualRequestCount, 247 this.expectedRequestCount == this.actualRequestCount); 248 } 249 if (this.expectedMethod != null) 250 { 251 Assert.assertEquals(this.expectedMethod, this.actualMethod); 252 } 253 if (this.expectedUri != null) 254 { 255 Assert.assertEquals(this.expectedUri, this.actualUri); 256 } 257 } 258 259 261 270 public static int findUnusedLocalPort(String theHost, int theLowest, 271 int theHighest) 272 throws IOException 273 { 274 final Random random = new Random (System.currentTimeMillis()); 275 for (int i = 0; i < 10; i++) 276 { 277 int port = (int) 278 (random.nextFloat() * (theHighest - theLowest)) + theLowest; 279 Socket s = null; 280 try 281 { 282 s = new Socket (theHost, port); 283 } 284 catch (ConnectException e) 285 { 286 return port; 287 } 288 finally 289 { 290 if (s != null) 291 { 292 s.close(); 293 } 294 } 295 } 296 return -1; 297 } 298 299 301 307 private void processRequest(Socket theSocket) throws IOException 308 { 309 BufferedReader in = null; 310 OutputStream out = null; 311 try 312 { 313 readRequest(theSocket.getInputStream()); 314 writeResponse(theSocket.getOutputStream()); 315 } 316 finally 317 { 318 if (in != null) 319 { 320 in.close(); 321 } 322 if (out != null) 323 { 324 out.close(); 325 } 326 } 327 } 328 329 335 private void readRequest(InputStream theIn) throws IOException 336 { 337 BufferedReader reader = 338 new BufferedReader (new InputStreamReader (theIn)); 339 340 String statusLine = reader.readLine(); 341 StringTokenizer tokenizer = new StringTokenizer (statusLine); 342 this.actualRequestCount++; 343 this.actualMethod = tokenizer.nextToken(); 344 this.actualUri = tokenizer.nextToken(); 345 } 346 347 353 private void writeResponse(OutputStream theOut) throws IOException 354 { 355 if (this.response != null) 357 { 358 BufferedReader reader = new BufferedReader ( 359 new StringReader (this.response)); 360 String line = null; 361 while ((line = reader.readLine()) != null) 362 { 363 theOut.write(line.getBytes()); 364 theOut.write("\r\n".getBytes()); 365 } 366 } 367 } 368 369 } 370 | Popular Tags |