001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.tomcat;
017:
018: import java.io.BufferedReader;
019: import java.io.InputStreamReader;
020: import java.net.HttpURLConnection;
021: import java.net.URL;
022:
023: import org.apache.geronimo.tomcat.app.MockWebServiceContainer;
024: import org.apache.geronimo.crypto.encoders.Base64;
025:
026: /**
027: * @version $Rev: 617588 $ $Date: 2008-02-01 10:20:07 -0800 (Fri, 01 Feb 2008) $
028: */
029: public class ContainerTest extends AbstractWebModuleTest {
030:
031: public void testWebServiceHandler() throws Exception {
032:
033: String contextPath = "/foo/webservice.ws";
034: MockWebServiceContainer webServiceInvoker = new MockWebServiceContainer();
035: container.addWebService(contextPath, null, webServiceInvoker,
036: null, null, null, null, cl);
037: HttpURLConnection connection = (HttpURLConnection) new URL(
038: connector.getConnectUrl() + contextPath)
039: .openConnection();
040: try {
041: BufferedReader reader = new BufferedReader(
042: new InputStreamReader(connection.getInputStream()));
043: assertEquals(HttpURLConnection.HTTP_OK, connection
044: .getResponseCode());
045: assertEquals("Hello World", reader.readLine());
046: } finally {
047: connection.disconnect();
048: }
049: container.removeWebService(contextPath);
050: connection = (HttpURLConnection) new URL(connector
051: .getConnectUrl()
052: + contextPath).openConnection();
053: try {
054: connection.getInputStream();
055: fail();
056: } catch (Exception e) {
057: // see if we removed the ws.
058: assertEquals(HttpURLConnection.HTTP_NOT_FOUND, connection
059: .getResponseCode());
060: connection.disconnect();
061: }
062: }
063:
064: public void testSecureWebServiceHandler() throws Exception {
065:
066: setUpSecurity();
067:
068: String contextPath = "/foo/webservice.ws";
069: MockWebServiceContainer webServiceInvoker = new MockWebServiceContainer();
070: container.addWebService(contextPath, null, webServiceInvoker,
071: securityRealmName, securityRealmName, "NONE", "BASIC",
072: cl);
073:
074: //Veryify its secured
075: HttpURLConnection connection = (HttpURLConnection) new URL(
076: connector.getConnectUrl() + contextPath)
077: .openConnection();
078: try {
079: connection.getInputStream();
080: fail();
081: } catch (Exception e) {
082: assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED,
083: connection.getResponseCode());
084: } finally {
085: connection.disconnect();
086: }
087:
088: //Authenticate
089: connection = (HttpURLConnection) new URL(connector
090: .getConnectUrl()
091: + contextPath).openConnection();
092: String authentication = new String(Base64
093: .encode("alan:starcraft".getBytes()));
094: connection.setRequestProperty("Authorization", "Basic "
095: + authentication);
096: try {
097: BufferedReader reader = new BufferedReader(
098: new InputStreamReader(connection.getInputStream()));
099: assertEquals(HttpURLConnection.HTTP_OK, connection
100: .getResponseCode());
101: assertEquals("Hello World", reader.readLine());
102: } finally {
103: connection.disconnect();
104: }
105: container.removeWebService(contextPath);
106: connection = (HttpURLConnection) new URL(connector
107: .getConnectUrl()
108: + contextPath).openConnection();
109: try {
110: connection.getInputStream();
111: fail();
112: } catch (Exception e) {
113: // see if we removed the ws.
114: assertEquals(HttpURLConnection.HTTP_NOT_FOUND, connection
115: .getResponseCode());
116: connection.disconnect();
117: }
118:
119: }
120:
121: protected void setUp() throws Exception {
122: super.setUp();
123: super.init(null);
124: }
125:
126: }
|