1   /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.intercept.web;
17  
18  import org.acegisecurity.MockFilterChain;
19  
20  import org.jmock.MockObjectTestCase;
21  
22  import org.springframework.mock.web.MockHttpServletRequest;
23  import org.springframework.mock.web.MockHttpServletResponse;
24  
25  import javax.servlet.ServletRequest;
26  import javax.servlet.ServletResponse;
27  
28  
29  /**
30   * Tests {@link FilterInvocation}.
31   *
32   * @author Ben Alex
33   * @author colin sampaleanu
34   * @version $Id: FilterInvocationTests.java 1496 2006-05-23 13:38:33Z benalex $
35   */
36  public class FilterInvocationTests extends MockObjectTestCase {
37      //~ Constructors ===================================================================================================
38  
39      public FilterInvocationTests() {
40          super();
41      }
42  
43      public FilterInvocationTests(String arg0) {
44          super(arg0);
45      }
46  
47      //~ Methods ========================================================================================================
48  
49      public static void main(String[] args) {
50          junit.textui.TestRunner.run(FilterInvocationTests.class);
51      }
52  
53      public final void setUp() throws Exception {
54          super.setUp();
55      }
56  
57      public void testGettersAndStringMethods() {
58          MockHttpServletRequest request = new MockHttpServletRequest(null, null);
59          request.setServletPath("/HelloWorld");
60          request.setPathInfo("/some/more/segments.html");
61          request.setServerName("www.example.com");
62          request.setScheme("http");
63          request.setServerPort(80);
64          request.setContextPath("/mycontext");
65          request.setRequestURI("/mycontext/HelloWorld/some/more/segments.html");
66  
67          MockHttpServletResponse response = new MockHttpServletResponse();
68          MockFilterChain chain = new MockFilterChain();
69          FilterInvocation fi = new FilterInvocation(request, response, chain);
70          assertEquals(request, fi.getRequest());
71          assertEquals(request, fi.getHttpRequest());
72          assertEquals(response, fi.getResponse());
73          assertEquals(response, fi.getHttpResponse());
74          assertEquals(chain, fi.getChain());
75          assertEquals("/HelloWorld/some/more/segments.html", fi.getRequestUrl());
76          assertEquals("FilterInvocation: URL: /HelloWorld/some/more/segments.html", fi.toString());
77          assertEquals("http://www.example.com/mycontext/HelloWorld/some/more/segments.html", fi.getFullRequestUrl());
78      }
79  
80      public void testNoArgConstructorDoesntExist() {
81          Class clazz = FilterInvocation.class;
82  
83          try {
84              clazz.getDeclaredConstructor((Class[]) null);
85              fail("Should have thrown NoSuchMethodException");
86          } catch (NoSuchMethodException expected) {
87              assertTrue(true);
88          }
89      }
90  
91      public void testRejectsNullFilterChain() {
92          MockHttpServletRequest request = new MockHttpServletRequest(null, null);
93          MockHttpServletResponse response = new MockHttpServletResponse();
94  
95          try {
96              new FilterInvocation(request, response, null);
97              fail("Should have thrown IllegalArgumentException");
98          } catch (IllegalArgumentException expected) {
99              assertTrue(true);
100         }
101     }
102 
103     public void testRejectsNullServletRequest() {
104         MockHttpServletResponse response = new MockHttpServletResponse();
105         MockFilterChain chain = new MockFilterChain();
106 
107         try {
108             new FilterInvocation(null, response, chain);
109             fail("Should have thrown IllegalArgumentException");
110         } catch (IllegalArgumentException expected) {
111             assertTrue(true);
112         }
113     }
114 
115     public void testRejectsNullServletResponse() {
116         MockHttpServletRequest request = new MockHttpServletRequest(null, null);
117         MockFilterChain chain = new MockFilterChain();
118 
119         try {
120             new FilterInvocation(request, null, chain);
121             fail("Should have thrown IllegalArgumentException");
122         } catch (IllegalArgumentException expected) {
123             assertTrue(true);
124         }
125     }
126 
127     public void testRejectsServletRequestWhichIsNotHttpServletRequest() {
128         ServletRequest request = (ServletRequest) newDummy(ServletRequest.class);
129         MockHttpServletResponse response = new MockHttpServletResponse();
130         MockFilterChain chain = new MockFilterChain();
131 
132         try {
133             new FilterInvocation(request, response, chain);
134             fail("Should have thrown IllegalArgumentException");
135         } catch (IllegalArgumentException expected) {
136             assertEquals("Can only process HttpServletRequest", expected.getMessage());
137         }
138     }
139 
140     public void testRejectsServletResponseWhichIsNotHttpServletResponse() {
141         MockHttpServletRequest request = new MockHttpServletRequest(null, null);
142         ServletResponse response = (ServletResponse) newDummy(ServletResponse.class);
143         MockFilterChain chain = new MockFilterChain();
144 
145         try {
146             new FilterInvocation(request, response, chain);
147             fail("Should have thrown IllegalArgumentException");
148         } catch (IllegalArgumentException expected) {
149             assertEquals("Can only process HttpServletResponse", expected.getMessage());
150         }
151     }
152 
153     public void testStringMethodsWithAQueryString() {
154         MockHttpServletRequest request = new MockHttpServletRequest();
155         request.setQueryString("foo=bar");
156         request.setServletPath("/HelloWorld");
157         request.setServerName("www.example.com");
158         request.setScheme("http");
159         request.setServerPort(80);
160         request.setContextPath("/mycontext");
161         request.setRequestURI("/mycontext/HelloWorld");
162 
163         MockHttpServletResponse response = new MockHttpServletResponse();
164         MockFilterChain chain = new MockFilterChain();
165         FilterInvocation fi = new FilterInvocation(request, response, chain);
166         assertEquals("/HelloWorld?foo=bar", fi.getRequestUrl());
167         assertEquals("FilterInvocation: URL: /HelloWorld?foo=bar", fi.toString());
168         assertEquals("http://www.example.com/mycontext/HelloWorld?foo=bar", fi.getFullRequestUrl());
169     }
170 
171     public void testStringMethodsWithoutAnyQueryString() {
172         MockHttpServletRequest request = new MockHttpServletRequest(null, null);
173         request.setServletPath("/HelloWorld");
174         request.setServerName("www.example.com");
175         request.setScheme("http");
176         request.setServerPort(80);
177         request.setContextPath("/mycontext");
178         request.setRequestURI("/mycontext/HelloWorld");
179 
180         MockHttpServletResponse response = new MockHttpServletResponse();
181         MockFilterChain chain = new MockFilterChain();
182         FilterInvocation fi = new FilterInvocation(request, response, chain);
183         assertEquals("/HelloWorld", fi.getRequestUrl());
184         assertEquals("FilterInvocation: URL: /HelloWorld", fi.toString());
185         assertEquals("http://www.example.com/mycontext/HelloWorld", fi.getFullRequestUrl());
186     }
187 }