1 17 package org.apache.servicemix.http; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.InputStream ; 21 import java.io.OutputStream ; 22 import java.net.HttpURLConnection ; 23 import java.net.URL ; 24 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 28 import junit.framework.TestCase; 29 30 import org.apache.commons.httpclient.HttpClient; 31 import org.apache.commons.httpclient.HttpMethod; 32 import org.apache.commons.httpclient.methods.GetMethod; 33 import org.apache.commons.httpclient.methods.PostMethod; 34 import org.apache.commons.httpclient.methods.StringRequestEntity; 35 import org.apache.servicemix.components.http.InvalidStatusResponseException; 36 import org.apache.servicemix.http.jetty.JettyContextManager; 37 import org.apache.servicemix.jbi.util.FileUtil; 38 import org.mortbay.thread.BoundedThreadPool; 39 40 public class ServerManagerTest extends TestCase { 41 42 protected JettyContextManager server; 43 protected HttpConfiguration configuration; 44 45 protected void setUp() throws Exception { 46 System.setProperty("DEBUG", "true"); 47 System.setProperty("java.protocol.handler.pkgs", "HTTPClient"); 48 configuration = new HttpConfiguration(); 49 server = new JettyContextManager(); 50 server.setConfiguration(configuration); 51 } 52 53 protected void tearDown() throws Exception { 54 server.shutDown(); 55 } 56 57 public void test() throws Exception { 58 server.init(); 59 server.start(); 60 61 checkFail("http://localhost:8192/Service1/echo", null); 63 Object ctx1 = server.createContext("http://localhost:8192/Service1", new TestHttpProcessor()); 64 request("http://localhost:8192/Service1/echo", null); 65 server.remove(ctx1); 66 checkFail("http://localhost:8192/Service1/echo", null); 67 68 checkFail("http://localhost:8192/Service2/echo", null); 70 Object ctx2 = server.createContext("http://localhost:8192/Service2", new TestHttpProcessor()); 71 request("http://localhost:8192/Service2/echo", null); 72 server.remove(ctx2); 73 checkFail("http://localhost:8192/Service2/echo", null); 74 75 checkFail("http://localhost:8193/echo", null); 77 Object ctx3 = server.createContext("http://localhost:8193", new TestHttpProcessor()); 78 request("http://localhost:8193/echo", null); 79 server.remove(ctx3); 80 checkFail("http://localhost:8193/echo", null); 81 } 82 83 public void testOverlappingPath() throws Exception { 84 server.init(); 85 server.start(); 86 87 server.createContext("http://localhost:8192/Service1/test1", new TestHttpProcessor()); 88 89 try { 90 server.createContext("http://localhost:8192/Service1/test1", new TestHttpProcessor()); 91 fail("Contexts are overlapping, an exception should have been thrown"); 92 } catch (Exception e) { 93 } 95 96 try { 97 server.createContext("http://localhost:8192/Service1/test1/test", new TestHttpProcessor()); 98 fail("Contexts are overlapping, an exception should have been thrown"); 99 } catch (Exception e) { 100 } 102 103 try { 104 server.createContext("http://localhost:8192/Service1", new TestHttpProcessor()); 105 fail("Contexts are overlapping, an exception should have been thrown"); 106 } catch (Exception e) { 107 } 109 } 110 111 public void testSetMaxThreads() throws Exception { 112 int maxThreads = 512; 113 configuration.setJettyThreadPoolSize(maxThreads); 114 server.init(); 115 assertTrue(server.getThreadPool() instanceof BoundedThreadPool); 116 int threads = ((BoundedThreadPool) server.getThreadPool()).getMaxThreads(); 117 assertEquals("The max number of threads is incorrect!", maxThreads, threads); 118 } 119 120 protected void checkFail(String url, String content) { 121 try { 122 request(url, content); 123 fail("Request should have failed: " + url); 124 } catch (Exception e) { 125 } 127 } 128 129 protected String request(String url, String content) throws Exception { 130 if (true) { 131 return requestWithHttpClient(url, content); 132 } else { 133 return requestWithUrlConnection(url, content); 134 } 135 } 136 137 private String requestWithHttpClient(String url, String content) throws Exception { 138 HttpMethod method; 139 if (content != null) { 140 PostMethod post = new PostMethod(url); 141 post.setRequestEntity(new StringRequestEntity(content)); 142 method = post; 143 } else { 144 GetMethod get = new GetMethod(url); 145 method = get; 146 } 147 new HttpClient().executeMethod(method); 148 if (method.getStatusCode() != 200) { 149 throw new InvalidStatusResponseException(method.getStatusCode()); 150 } 151 return method.getResponseBodyAsString(); 152 } 153 154 private String requestWithUrlConnection(String url, String content) throws Exception { 155 HttpURLConnection connection = (HttpURLConnection ) new URL (url).openConnection(); 156 try { 157 connection.setDoInput(true); 158 if (content != null) { 159 connection.setDoOutput(true); 160 OutputStream os = connection.getOutputStream(); 161 os.write(content.getBytes()); 162 os.close(); 163 } 164 InputStream is = connection.getInputStream(); 165 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 166 FileUtil.copyInputStream(is, baos); 167 return baos.toString(); 168 } finally { 169 connection.disconnect(); 170 } 171 } 172 173 public static class TestHttpProcessor implements HttpProcessor { 174 public SslParameters getSsl() { 175 return null; 176 } 177 public String getAuthMethod() { 178 return null; 179 } 180 public void process(HttpServletRequest request, HttpServletResponse response) throws Exception { 181 System.out.println(request); 182 } 183 184 } 185 } 186 | Popular Tags |