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.captcha;
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.net.URLEncoder;
028:
029: import java.util.HashMap;
030: import java.util.Map;
031:
032: /**
033: * Tests {@link CaptchaEntryPoint}.
034: *
035: * @author marc antoine Garrigue
036: * @version $Id: CaptchaEntryPointTests.java 1496 2006-05-23 13:38:33Z benalex $
037: */
038: public class CaptchaEntryPointTests extends TestCase {
039: //~ Methods ========================================================================================================
040:
041: public static void main(String[] args) {
042: junit.textui.TestRunner.run(CaptchaEntryPointTests.class);
043: }
044:
045: // ~ Methods
046: // ================================================================
047: public final void setUp() throws Exception {
048: super .setUp();
049: }
050:
051: public void testDetectsMissingCaptchaFormUrl() throws Exception {
052: CaptchaEntryPoint ep = new CaptchaEntryPoint();
053: ep.setPortMapper(new PortMapperImpl());
054: ep.setPortResolver(new MockPortResolver(80, 443));
055:
056: try {
057: ep.afterPropertiesSet();
058: fail("Should have thrown IllegalArgumentException");
059: } catch (IllegalArgumentException expected) {
060: assertEquals("captchaFormUrl must be specified", expected
061: .getMessage());
062: }
063: }
064:
065: public void testDetectsMissingPortMapper() throws Exception {
066: CaptchaEntryPoint ep = new CaptchaEntryPoint();
067: ep.setCaptchaFormUrl("xxx");
068: ep.setPortMapper(null);
069:
070: try {
071: ep.afterPropertiesSet();
072: fail("Should have thrown IllegalArgumentException");
073: } catch (IllegalArgumentException expected) {
074: assertEquals("portMapper must be specified", expected
075: .getMessage());
076: }
077: }
078:
079: public void testDetectsMissingPortResolver() throws Exception {
080: CaptchaEntryPoint ep = new CaptchaEntryPoint();
081: ep.setCaptchaFormUrl("xxx");
082: ep.setPortResolver(null);
083:
084: try {
085: ep.afterPropertiesSet();
086: fail("Should have thrown IllegalArgumentException");
087: } catch (IllegalArgumentException expected) {
088: assertEquals("portResolver must be specified", expected
089: .getMessage());
090: }
091: }
092:
093: public void testGettersSetters() {
094: CaptchaEntryPoint ep = new CaptchaEntryPoint();
095: ep.setCaptchaFormUrl("/hello");
096: ep.setPortMapper(new PortMapperImpl());
097: ep.setPortResolver(new MockPortResolver(8080, 8443));
098: assertEquals("/hello", ep.getCaptchaFormUrl());
099: assertTrue(ep.getPortMapper() != null);
100: assertTrue(ep.getPortResolver() != null);
101:
102: assertEquals("original_requestUrl", ep
103: .getOriginalRequestUrlParameterName());
104: ep.setOriginalRequestUrlParameterName("Z");
105: assertEquals("Z", ep.getOriginalRequestUrlParameterName());
106:
107: assertEquals(true, ep.isIncludeOriginalRequest());
108: ep.setIncludeOriginalRequest(false);
109: assertEquals(false, ep.isIncludeOriginalRequest());
110:
111: assertEquals(false, ep.isOutsideWebApp());
112: ep.setOutsideWebApp(true);
113: assertEquals(true, ep.isOutsideWebApp());
114:
115: ep.setForceHttps(false);
116: assertFalse(ep.getForceHttps());
117: ep.setForceHttps(true);
118: assertTrue(ep.getForceHttps());
119: }
120:
121: public void testHttpsOperationFromOriginalHttpUrl()
122: throws Exception {
123: MockHttpServletRequest request = new MockHttpServletRequest();
124:
125: request.setRequestURI("/some_path");
126: request.setScheme("http");
127: request.setServerName("www.example.com");
128: request.setContextPath("/bigWebApp");
129: request.setServerPort(80);
130:
131: MockHttpServletResponse response = new MockHttpServletResponse();
132:
133: CaptchaEntryPoint ep = new CaptchaEntryPoint();
134: ep.setIncludeOriginalRequest(false);
135: ep.setCaptchaFormUrl("/hello");
136: ep.setPortMapper(new PortMapperImpl());
137: ep.setForceHttps(true);
138: ep.setPortMapper(new PortMapperImpl());
139: ep.setPortResolver(new MockPortResolver(80, 443));
140: ep.afterPropertiesSet();
141:
142: ep.commence(request, response);
143: assertEquals("https://www.example.com/bigWebApp/hello",
144: response.getRedirectedUrl());
145:
146: request.setServerPort(8080);
147: response = new MockHttpServletResponse();
148: ep.setPortResolver(new MockPortResolver(8080, 8443));
149: ep.commence(request, response);
150: assertEquals("https://www.example.com:8443/bigWebApp/hello",
151: response.getRedirectedUrl());
152:
153: // Now test an unusual custom HTTP:HTTPS is handled properly
154: request.setServerPort(8888);
155: response = new MockHttpServletResponse();
156: ep.commence(request, response);
157: assertEquals("https://www.example.com:8443/bigWebApp/hello",
158: response.getRedirectedUrl());
159:
160: PortMapperImpl portMapper = new PortMapperImpl();
161: Map map = new HashMap();
162: map.put("8888", "9999");
163: portMapper.setPortMappings(map);
164: response = new MockHttpServletResponse();
165:
166: ep = new CaptchaEntryPoint();
167: ep.setCaptchaFormUrl("/hello");
168: ep.setPortMapper(new PortMapperImpl());
169: ep.setForceHttps(true);
170: ep.setPortMapper(portMapper);
171: ep.setPortResolver(new MockPortResolver(8888, 9999));
172: ep.setIncludeOriginalRequest(false);
173:
174: ep.afterPropertiesSet();
175:
176: ep.commence(request, response);
177: assertEquals("https://www.example.com:9999/bigWebApp/hello",
178: response.getRedirectedUrl());
179: }
180:
181: public void testHttpsOperationFromOriginalHttpsUrl()
182: throws Exception {
183: MockHttpServletRequest request = new MockHttpServletRequest();
184:
185: request.setRequestURI("/some_path");
186: request.setScheme("https");
187: request.setServerName("www.example.com");
188: request.setContextPath("/bigWebApp");
189: request.setServerPort(443);
190:
191: MockHttpServletResponse response = new MockHttpServletResponse();
192:
193: CaptchaEntryPoint ep = new CaptchaEntryPoint();
194: ep.setIncludeOriginalRequest(false);
195: ep.setCaptchaFormUrl("/hello");
196: ep.setPortMapper(new PortMapperImpl());
197: ep.setForceHttps(true);
198: ep.setPortMapper(new PortMapperImpl());
199: ep.setPortResolver(new MockPortResolver(80, 443));
200: ep.afterPropertiesSet();
201:
202: ep.commence(request, response);
203: assertEquals("https://www.example.com/bigWebApp/hello",
204: response.getRedirectedUrl());
205:
206: request.setServerPort(8443);
207: response = new MockHttpServletResponse();
208: ep.setPortResolver(new MockPortResolver(8080, 8443));
209: ep.commence(request, response);
210: assertEquals("https://www.example.com:8443/bigWebApp/hello",
211: response.getRedirectedUrl());
212: }
213:
214: public void testNormalOperation() throws Exception {
215: CaptchaEntryPoint ep = new CaptchaEntryPoint();
216: ep.setCaptchaFormUrl("/hello");
217: ep.setPortMapper(new PortMapperImpl());
218: ep.setPortResolver(new MockPortResolver(80, 443));
219: ep.afterPropertiesSet();
220: ep.setIncludeOriginalRequest(false);
221:
222: MockHttpServletRequest request = new MockHttpServletRequest();
223: request.setRequestURI("/some_path");
224: request.setContextPath("/bigWebApp");
225: request.setScheme("http");
226: request.setServerName("www.example.com");
227: request.setContextPath("/bigWebApp");
228: request.setServerPort(80);
229:
230: MockHttpServletResponse response = new MockHttpServletResponse();
231:
232: ep.afterPropertiesSet();
233: ep.commence(request, response);
234: assertEquals("http://www.example.com/bigWebApp/hello", response
235: .getRedirectedUrl());
236: }
237:
238: public void testOperationWhenHttpsRequestsButHttpsPortUnknown()
239: throws Exception {
240: CaptchaEntryPoint ep = new CaptchaEntryPoint();
241: ep.setCaptchaFormUrl("/hello");
242: ep.setPortMapper(new PortMapperImpl());
243: ep.setPortResolver(new MockPortResolver(8888, 1234));
244: ep.setForceHttps(true);
245: ep.setIncludeOriginalRequest(false);
246:
247: ep.afterPropertiesSet();
248:
249: MockHttpServletRequest request = new MockHttpServletRequest();
250: request.setRequestURI("/some_path");
251: request.setContextPath("/bigWebApp");
252: request.setScheme("http");
253: request.setServerName("www.example.com");
254: request.setContextPath("/bigWebApp");
255: request.setServerPort(8888); // NB: Port we can't resolve
256:
257: MockHttpServletResponse response = new MockHttpServletResponse();
258:
259: ep.afterPropertiesSet();
260: ep.commence(request, response);
261:
262: // Response doesn't switch to HTTPS, as we didn't know HTTP port 8888 to
263: // HTTP port mapping
264: assertEquals("http://www.example.com:8888/bigWebApp/hello",
265: response.getRedirectedUrl());
266: }
267:
268: public void testOperationWithOriginalRequestIncludes()
269: throws Exception {
270: CaptchaEntryPoint ep = new CaptchaEntryPoint();
271: ep.setCaptchaFormUrl("/hello");
272:
273: PortMapperImpl mapper = new PortMapperImpl();
274: mapper.getTranslatedPortMappings().put(new Integer(8888),
275: new Integer(1234));
276: ep.setPortMapper(mapper);
277:
278: ep.setPortResolver(new MockPortResolver(8888, 1234));
279: ep.setIncludeOriginalRequest(true);
280: ep.afterPropertiesSet();
281:
282: MockHttpServletRequest request = new MockHttpServletRequest();
283: request.setMethod("post");
284: request.setRequestURI("/some_path");
285: request.setScheme("http");
286: request.setServerName("www.example.com");
287:
288: // request.setContextPath("/bigWebApp");
289: // TODO correct this when the getRequestUrl from mock works...
290: request.setServerPort(8888); // NB: Port we can't resolve
291:
292: MockHttpServletResponse response = new MockHttpServletResponse();
293:
294: ep.afterPropertiesSet();
295: ep.commence(request, response);
296: assertEquals(
297: "http://www.example.com:8888/hello?original_requestUrl="
298: + URLEncoder
299: .encode(
300: "http://www.example.com:8888/some_path",
301: "UTF-8")
302: + "&original_request_method=post", response
303: .getRedirectedUrl());
304:
305: // test the query params
306: request.addParameter("name", "value");
307: response = new MockHttpServletResponse();
308: ep.commence(request, response);
309: assertEquals(
310: "http://www.example.com:8888/hello?original_requestUrl="
311: + URLEncoder
312: .encode(
313: "http://www.example.com:8888/some_path",
314: "UTF-8")
315: + "&original_request_method=post", response
316: .getRedirectedUrl());
317:
318: // test the multiple query params
319: ep.setIncludeOriginalParameters(true);
320:
321: request.addParameter("name", "value");
322: request.addParameter("name1", "value2");
323: response = new MockHttpServletResponse();
324: ep.commence(request, response);
325: assertEquals(
326: "http://www.example.com:8888/hello?original_requestUrl="
327: + URLEncoder
328: .encode(
329: "http://www.example.com:8888/some_path",
330: "UTF-8")
331: + "&original_request_method=post"
332: + "&original_request_parameters="
333: + URLEncoder.encode(
334: "name__value;;name1__value2", "UTF-8"),
335: response.getRedirectedUrl());
336:
337: // test add parameter to captcha form url??
338: ep.setCaptchaFormUrl("/hello?toto=titi");
339: response = new MockHttpServletResponse();
340: ep.commence(request, response);
341: assertEquals(
342: "http://www.example.com:8888/hello?toto=titi&original_requestUrl="
343: + URLEncoder
344: .encode(
345: "http://www.example.com:8888/some_path",
346: "UTF-8")
347: + "&original_request_method=post"
348: + "&original_request_parameters="
349: + URLEncoder.encode(
350: "name__value;;name1__value2", "UTF-8"),
351: response.getRedirectedUrl());
352:
353: // with forcing!!!
354: ep.setForceHttps(true);
355: response = new MockHttpServletResponse();
356: ep.commence(request, response);
357: assertEquals(
358: "https://www.example.com:1234/hello?toto=titi&original_requestUrl="
359: + URLEncoder
360: .encode(
361: "http://www.example.com:8888/some_path",
362: "UTF-8")
363: + "&original_request_method=post"
364: + "&original_request_parameters="
365: + URLEncoder.encode(
366: "name__value;;name1__value2", "UTF-8"),
367: response.getRedirectedUrl());
368: }
369:
370: public void testOperationWithOutsideWebApp() throws Exception {
371: CaptchaEntryPoint ep = new CaptchaEntryPoint();
372: ep.setCaptchaFormUrl("https://www.jcaptcha.net/dotest/");
373:
374: PortMapperImpl mapper = new PortMapperImpl();
375: mapper.getTranslatedPortMappings().put(new Integer(8888),
376: new Integer(1234));
377: ep.setPortMapper(mapper);
378:
379: ep.setPortResolver(new MockPortResolver(8888, 1234));
380: ep.setIncludeOriginalRequest(true);
381: ep.setOutsideWebApp(true);
382:
383: ep.afterPropertiesSet();
384:
385: MockHttpServletRequest request = new MockHttpServletRequest();
386: request.setRequestURI("/some_path");
387: request.setScheme("http");
388: request.setServerName("www.example.com");
389: request.setMethod("post");
390:
391: // request.setContextPath("/bigWebApp");
392: // TODO correct this when the getRequestUrl from mock works...
393: request.setServerPort(8888); // NB: Port we can't resolve
394:
395: MockHttpServletResponse response = new MockHttpServletResponse();
396:
397: ep.afterPropertiesSet();
398: ep.commence(request, response);
399: assertEquals(
400: "https://www.jcaptcha.net/dotest/?original_requestUrl="
401: + URLEncoder
402: .encode(
403: "http://www.example.com:8888/some_path",
404: "UTF-8")
405: + "&original_request_method=post", response
406: .getRedirectedUrl());
407:
408: // test the query params
409: request.addParameter("name", "value");
410: response = new MockHttpServletResponse();
411: ep.commence(request, response);
412: assertEquals(
413: "https://www.jcaptcha.net/dotest/?original_requestUrl="
414: + URLEncoder
415: .encode(
416: "http://www.example.com:8888/some_path",
417: "UTF-8")
418: + "&original_request_method=post", response
419: .getRedirectedUrl());
420:
421: // test the multiple query params
422: ep.setIncludeOriginalParameters(true);
423: request.addParameter("name", "value");
424: request.addParameter("name1", "value2");
425: response = new MockHttpServletResponse();
426: ep.commence(request, response);
427: assertEquals(
428: "https://www.jcaptcha.net/dotest/?original_requestUrl="
429: + URLEncoder
430: .encode(
431: "http://www.example.com:8888/some_path",
432: "UTF-8")
433: + "&original_request_method=post"
434: + "&original_request_parameters="
435: + URLEncoder.encode(
436: "name__value;;name1__value2", "UTF-8"),
437: response.getRedirectedUrl());
438:
439: // test add parameter to captcha form url??
440: ep
441: .setCaptchaFormUrl("https://www.jcaptcha.net/dotest/?toto=titi");
442: response = new MockHttpServletResponse();
443: ep.commence(request, response);
444: assertEquals(
445: "https://www.jcaptcha.net/dotest/?toto=titi&original_requestUrl="
446: + URLEncoder
447: .encode(
448: "http://www.example.com:8888/some_path",
449: "UTF-8")
450: + "&original_request_method=post"
451: + "&original_request_parameters="
452: + URLEncoder.encode(
453: "name__value;;name1__value2", "UTF-8"),
454: response.getRedirectedUrl());
455:
456: // with forcing!!!
457: ep.setForceHttps(true);
458: response = new MockHttpServletResponse();
459: ep.commence(request, response);
460: assertEquals(
461: "https://www.jcaptcha.net/dotest/?toto=titi&original_requestUrl="
462: + URLEncoder
463: .encode(
464: "http://www.example.com:8888/some_path",
465: "UTF-8")
466: + "&original_request_method=post"
467: + "&original_request_parameters="
468: + URLEncoder.encode(
469: "name__value;;name1__value2", "UTF-8"),
470: response.getRedirectedUrl());
471: }
472: }
|