1 17 18 package org.apache.geronimo.tomcat; 19 20 import java.io.BufferedReader ; 21 import java.io.InputStreamReader ; 22 import java.net.HttpURLConnection ; 23 import java.net.URL ; 24 25 import org.apache.geronimo.tomcat.app.MockWebServiceContainer; 26 import org.apache.geronimo.util.encoders.Base64; 27 28 29 32 public class ContainerTest extends AbstractWebModuleTest { 33 34 public void testWebServiceHandler() throws Exception { 35 36 String contextPath = "/foo/webservice.ws"; 37 MockWebServiceContainer webServiceInvoker = new MockWebServiceContainer(); 38 container.addWebService(contextPath, null, webServiceInvoker, null, null, null,null, cl); 39 HttpURLConnection connection = (HttpURLConnection ) new URL ("http://localhost:8181" + contextPath).openConnection(); 40 try { 41 BufferedReader reader = new BufferedReader (new InputStreamReader (connection.getInputStream())); 42 assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode()); 43 assertEquals("Hello World", reader.readLine()); 44 } finally { 45 connection.disconnect(); 46 } 47 container.removeWebService(contextPath); 48 connection = (HttpURLConnection ) new URL ("http://localhost:8181" + contextPath).openConnection(); 49 try { 50 connection.getInputStream(); 51 fail(); 52 } catch (Exception e) { 53 assertEquals(HttpURLConnection.HTTP_NOT_FOUND, connection.getResponseCode()); 55 connection.disconnect(); 56 } 57 58 } 59 60 public void testSecureWebServiceHandler() throws Exception { 61 62 setUpSecurity(); 63 64 String contextPath = "/foo/webservice.ws"; 65 MockWebServiceContainer webServiceInvoker = new MockWebServiceContainer(); 66 container.addWebService(contextPath, null, webServiceInvoker, securityRealmName, securityRealmName, "NONE", "BASIC", cl); 67 68 HttpURLConnection connection = (HttpURLConnection ) new URL ("http://localhost:8181" + contextPath).openConnection(); 70 try { 71 connection.getInputStream(); 72 fail(); 73 } catch (Exception e) { 74 assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, connection.getResponseCode()); 75 } finally { 76 connection.disconnect(); 77 } 78 79 connection = (HttpURLConnection ) new URL ("http://localhost:8181" + contextPath).openConnection(); 81 String authentication = new String (Base64.encode("alan:starcraft".getBytes())); 82 connection.setRequestProperty("Authorization", "Basic " + authentication); 83 try { 84 BufferedReader reader = new BufferedReader (new InputStreamReader (connection.getInputStream())); 85 assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode()); 86 assertEquals("Hello World", reader.readLine()); 87 } finally { 88 connection.disconnect(); 89 } 90 container.removeWebService(contextPath); 91 connection = (HttpURLConnection ) new URL ("http://localhost:8181" + contextPath).openConnection(); 92 try { 93 connection.getInputStream(); 94 fail(); 95 } catch (Exception e) { 96 assertEquals(HttpURLConnection.HTTP_NOT_FOUND, connection.getResponseCode()); 98 connection.disconnect(); 99 } 100 101 } 102 103 104 protected void setUp() throws Exception { 105 super.setUp(); 106 super.init(null); 107 } 108 109 } 110 | Popular Tags |