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 RetryWithHttpsEntryPoint}.
032: *
033: * @author Ben Alex
034: * @version $Id: RetryWithHttpsEntryPointTests.java 1496 2006-05-23 13:38:33Z benalex $
035: */
036: public class RetryWithHttpsEntryPointTests extends TestCase {
037: //~ Methods ========================================================================================================
038:
039: public static void main(String[] args) {
040: junit.textui.TestRunner
041: .run(RetryWithHttpsEntryPointTests.class);
042: }
043:
044: public final void setUp() throws Exception {
045: super .setUp();
046: }
047:
048: public void testDetectsMissingPortMapper() throws Exception {
049: RetryWithHttpsEntryPoint ep = new RetryWithHttpsEntryPoint();
050: ep.setPortMapper(null);
051:
052: try {
053: ep.afterPropertiesSet();
054: fail("Should have thrown IllegalArgumentException");
055: } catch (IllegalArgumentException expected) {
056: assertEquals("portMapper is required", expected
057: .getMessage());
058: }
059: }
060:
061: public void testDetectsMissingPortResolver() throws Exception {
062: RetryWithHttpsEntryPoint ep = new RetryWithHttpsEntryPoint();
063: ep.setPortResolver(null);
064:
065: try {
066: ep.afterPropertiesSet();
067: fail("Should have thrown IllegalArgumentException");
068: } catch (IllegalArgumentException expected) {
069: assertEquals("portResolver is required", expected
070: .getMessage());
071: }
072: }
073:
074: public void testGettersSetters() {
075: RetryWithHttpsEntryPoint ep = new RetryWithHttpsEntryPoint();
076: ep.setPortMapper(new PortMapperImpl());
077: ep.setPortResolver(new MockPortResolver(8080, 8443));
078: assertTrue(ep.getPortMapper() != null);
079: assertTrue(ep.getPortResolver() != null);
080: }
081:
082: public void testNormalOperation() throws Exception {
083: MockHttpServletRequest request = new MockHttpServletRequest();
084: request.setQueryString("open=true");
085: request.setScheme("http");
086: request.setServerName("www.example.com");
087: request.setContextPath("/bigWebApp");
088: request.setServletPath("/hello");
089: request.setPathInfo("/pathInfo.html");
090: request.setServerPort(80);
091:
092: MockHttpServletResponse response = new MockHttpServletResponse();
093:
094: RetryWithHttpsEntryPoint ep = new RetryWithHttpsEntryPoint();
095: ep.setPortMapper(new PortMapperImpl());
096: ep.setPortResolver(new MockPortResolver(80, 443));
097: ep.afterPropertiesSet();
098:
099: ep.commence(request, response);
100: assertEquals(
101: "https://www.example.com/bigWebApp/hello/pathInfo.html?open=true",
102: response.getRedirectedUrl());
103: }
104:
105: public void testNormalOperationWithNullPathInfoAndNullQueryString()
106: throws Exception {
107: MockHttpServletRequest request = new MockHttpServletRequest();
108: request.setScheme("http");
109: request.setServerName("www.example.com");
110: request.setContextPath("/bigWebApp");
111: request.setServletPath("/hello");
112: request.setPathInfo(null);
113: request.setServerPort(80);
114:
115: MockHttpServletResponse response = new MockHttpServletResponse();
116:
117: RetryWithHttpsEntryPoint ep = new RetryWithHttpsEntryPoint();
118: ep.setPortMapper(new PortMapperImpl());
119: ep.setPortResolver(new MockPortResolver(80, 443));
120: ep.afterPropertiesSet();
121:
122: ep.commence(request, response);
123: assertEquals("https://www.example.com/bigWebApp/hello",
124: response.getRedirectedUrl());
125: }
126:
127: public void testOperationWhenTargetPortIsUnknown() throws Exception {
128: MockHttpServletRequest request = new MockHttpServletRequest();
129: request.setQueryString("open=true");
130: request.setScheme("http");
131: request.setServerName("www.example.com");
132: request.setContextPath("/bigWebApp");
133: request.setServletPath("/hello");
134: request.setPathInfo("/pathInfo.html");
135: request.setServerPort(8768);
136:
137: MockHttpServletResponse response = new MockHttpServletResponse();
138:
139: RetryWithHttpsEntryPoint ep = new RetryWithHttpsEntryPoint();
140: ep.setPortMapper(new PortMapperImpl());
141: ep.setPortResolver(new MockPortResolver(8768, 1234));
142: ep.afterPropertiesSet();
143:
144: ep.commence(request, response);
145: assertEquals("/bigWebApp", response.getRedirectedUrl());
146: }
147:
148: public void testOperationWithNonStandardPort() throws Exception {
149: MockHttpServletRequest request = new MockHttpServletRequest();
150: request.setQueryString("open=true");
151: request.setScheme("http");
152: request.setServerName("www.example.com");
153: request.setContextPath("/bigWebApp");
154: request.setServletPath("/hello");
155: request.setPathInfo("/pathInfo.html");
156: request.setServerPort(8888);
157:
158: MockHttpServletResponse response = new MockHttpServletResponse();
159:
160: PortMapperImpl portMapper = new PortMapperImpl();
161: Map map = new HashMap();
162: map.put("8888", "9999");
163: portMapper.setPortMappings(map);
164:
165: RetryWithHttpsEntryPoint ep = new RetryWithHttpsEntryPoint();
166: ep.setPortResolver(new MockPortResolver(8888, 9999));
167: ep.setPortMapper(portMapper);
168: ep.afterPropertiesSet();
169:
170: ep.commence(request, response);
171: assertEquals(
172: "https://www.example.com:9999/bigWebApp/hello/pathInfo.html?open=true",
173: response.getRedirectedUrl());
174: }
175: }
|