001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.securechannel;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.MockPortResolver;
021:
022: import org.acegisecurity.util.PortMapperImpl;
023:
024: import org.springframework.mock.web.MockHttpServletRequest;
025: import org.springframework.mock.web.MockHttpServletResponse;
026:
027: import java.util.HashMap;
028: import java.util.Map;
029:
030: /**
031: * Tests {@link RetryWithHttpEntryPoint}.
032: *
033: * @author Ben Alex
034: * @version $Id: RetryWithHttpEntryPointTests.java 1496 2006-05-23 13:38:33Z benalex $
035: */
036: public class RetryWithHttpEntryPointTests extends TestCase {
037: //~ Methods ========================================================================================================
038:
039: public static void main(String[] args) {
040: junit.textui.TestRunner.run(RetryWithHttpEntryPointTests.class);
041: }
042:
043: public final void setUp() throws Exception {
044: super .setUp();
045: }
046:
047: public void testDetectsMissingPortMapper() throws Exception {
048: RetryWithHttpEntryPoint ep = new RetryWithHttpEntryPoint();
049: ep.setPortMapper(null);
050:
051: try {
052: ep.afterPropertiesSet();
053: fail("Should have thrown IllegalArgumentException");
054: } catch (IllegalArgumentException expected) {
055: assertEquals("portMapper is required", expected
056: .getMessage());
057: }
058: }
059:
060: public void testDetectsMissingPortResolver() throws Exception {
061: RetryWithHttpEntryPoint ep = new RetryWithHttpEntryPoint();
062: ep.setPortResolver(null);
063:
064: try {
065: ep.afterPropertiesSet();
066: fail("Should have thrown IllegalArgumentException");
067: } catch (IllegalArgumentException expected) {
068: assertEquals("portResolver is required", expected
069: .getMessage());
070: }
071: }
072:
073: public void testGettersSetters() {
074: RetryWithHttpEntryPoint ep = new RetryWithHttpEntryPoint();
075: ep.setPortMapper(new PortMapperImpl());
076: ep.setPortResolver(new MockPortResolver(8080, 8443));
077: assertTrue(ep.getPortMapper() != null);
078: assertTrue(ep.getPortResolver() != null);
079: }
080:
081: public void testNormalOperation() throws Exception {
082: MockHttpServletRequest request = new MockHttpServletRequest();
083: request.setQueryString("open=true");
084: request.setScheme("https");
085: request.setServerName("www.example.com");
086: request.setContextPath("/bigWebApp");
087: request.setServletPath("/hello");
088: request.setPathInfo("/pathInfo.html");
089: request.setServerPort(443);
090:
091: MockHttpServletResponse response = new MockHttpServletResponse();
092:
093: RetryWithHttpEntryPoint ep = new RetryWithHttpEntryPoint();
094: ep.setPortMapper(new PortMapperImpl());
095: ep.setPortResolver(new MockPortResolver(80, 443));
096: ep.afterPropertiesSet();
097:
098: ep.commence(request, response);
099: assertEquals(
100: "http://www.example.com/bigWebApp/hello/pathInfo.html?open=true",
101: response.getRedirectedUrl());
102: }
103:
104: public void testNormalOperationWithNullPathInfoAndNullQueryString()
105: throws Exception {
106: MockHttpServletRequest request = new MockHttpServletRequest();
107: request.setScheme("https");
108: request.setServerName("www.example.com");
109: request.setContextPath("/bigWebApp");
110: request.setServletPath("/hello");
111: request.setPathInfo(null);
112: request.setServerPort(443);
113:
114: MockHttpServletResponse response = new MockHttpServletResponse();
115:
116: RetryWithHttpEntryPoint ep = new RetryWithHttpEntryPoint();
117: ep.setPortMapper(new PortMapperImpl());
118: ep.setPortResolver(new MockPortResolver(80, 443));
119: ep.afterPropertiesSet();
120:
121: ep.commence(request, response);
122: assertEquals("http://www.example.com/bigWebApp/hello", response
123: .getRedirectedUrl());
124: }
125:
126: public void testOperationWhenTargetPortIsUnknown() throws Exception {
127: MockHttpServletRequest request = new MockHttpServletRequest();
128: request.setQueryString("open=true");
129: request.setScheme("https");
130: request.setServerName("www.example.com");
131: request.setContextPath("/bigWebApp");
132: request.setServletPath("/hello");
133: request.setPathInfo("/pathInfo.html");
134: request.setServerPort(8768);
135:
136: MockHttpServletResponse response = new MockHttpServletResponse();
137:
138: RetryWithHttpEntryPoint ep = new RetryWithHttpEntryPoint();
139: ep.setPortMapper(new PortMapperImpl());
140: ep.setPortResolver(new MockPortResolver(8768, 1234));
141: ep.afterPropertiesSet();
142:
143: ep.commence(request, response);
144: assertEquals("/bigWebApp", response.getRedirectedUrl());
145: }
146:
147: public void testOperationWithNonStandardPort() throws Exception {
148: MockHttpServletRequest request = new MockHttpServletRequest();
149: request.setQueryString("open=true");
150: request.setScheme("https");
151: request.setServerName("www.example.com");
152: request.setContextPath("/bigWebApp");
153: request.setServletPath("/hello");
154: request.setPathInfo("/pathInfo.html");
155: request.setServerPort(9999);
156:
157: MockHttpServletResponse response = new MockHttpServletResponse();
158:
159: PortMapperImpl portMapper = new PortMapperImpl();
160: Map map = new HashMap();
161: map.put("8888", "9999");
162: portMapper.setPortMappings(map);
163:
164: RetryWithHttpEntryPoint ep = new RetryWithHttpEntryPoint();
165: ep.setPortResolver(new MockPortResolver(8888, 9999));
166: ep.setPortMapper(portMapper);
167: ep.afterPropertiesSet();
168:
169: ep.commence(request, response);
170: assertEquals(
171: "http://www.example.com:8888/bigWebApp/hello/pathInfo.html?open=true",
172: response.getRedirectedUrl());
173: }
174: }
|