1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
package com.meterware.servletunit;
/********************************************************************************************************************
* $Id: FiltersTest.java 604 2004-02-26 17:36:47Z russgold $
*
* Copyright (c) 2004, Russell Gold
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*******************************************************************************************************************/
import junit.framework.TestCase;
import junit.framework.TestSuite;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.*;
import com.meterware.httpunit.HttpUnitUtils;
import com.meterware.httpunit.WebResponse;
import org.xml.sax.InputSource;
/**
* Tests the servlet filtering capability added in Servlet 2.3.
*
* @author <a href="mailto:russgold@httpunit.org">Russell Gold</a>
**/
public class FiltersTest extends TestCase {
final static FilterMetaData FILTER1 = new FilterMetaDataImpl(1);
final static FilterMetaData FILTER2 = new FilterMetaDataImpl(2);
final static FilterMetaData FILTER3 = new FilterMetaDataImpl(3);
final static FilterMetaData FILTER4 = new FilterMetaDataImpl(4);
final static FilterMetaData FILTER5 = new FilterMetaDataImpl(5);
final static FilterMetaData FILTER6 = new FilterMetaDataImpl(6);
private static boolean _servletCalled;
public static void main(String args[]) {
junit.textui.TestRunner.run( suite() );
}
public static TestSuite suite() {
return new TestSuite( FiltersTest.class );
}
public FiltersTest( String name ) {
super( name );
}
/**
* Verifies that the no-filter case is handled by servlet metadata.
*/
public void testNoFilterAssociation() throws Exception {
WebXMLString wxs = new WebXMLString();
wxs.addServlet( "Simple", "/SimpleServlet", SimpleGetServlet.class );
WebApplication application = new WebApplication( HttpUnitUtils.newParser().parse( new InputSource( wxs.asInputStream() ) ), null );
ServletMetaData metaData = application.getServletRequest( new URL( "http://localhost/SimpleServlet" ) );
FilterMetaData[] filters = metaData.getFilters();
assertEquals( "number of associated filters", 0, filters.length );
}
/**
* Verifies that a simple filter is associated with a servlet by its name.
*/
public void testNameFilterAssociation() throws Exception {
WebXMLString wxs = new WebXMLString();
wxs.addServlet( "Simple", "/SimpleServlet", SimpleGetServlet.class );
wxs.addFilterForServlet( "Trivial", TrivialFilter.class, "Simple" );
WebApplication application = new WebApplication( HttpUnitUtils.newParser().parse( new InputSource( wxs.asInputStream() ) ), null );
ServletMetaData metaData = application.getServletRequest( new URL( "http://localhost/SimpleServlet" ) );
FilterMetaData[] filters = metaData.getFilters();
assertEquals( "number of associated filters", 1, filters.length );
assertEquals( "filter class", TrivialFilter.class, filters[0].getFilter().getClass() );
}
/**
* Verifies that a simple filter will be called before a servlet with the same URL.
*/
public void testFilterByNameInvocation() throws Exception {
WebXMLString wxs = new WebXMLString();
wxs.addServlet( "Simple", "/SimpleServlet", SimpleGetServlet.class );
wxs.addFilterForServlet( "Trivial", TrivialFilter.class, "Simple" );
ServletRunner sr = new ServletRunner( wxs.asInputStream() );
ServletUnitClient wc = sr.newClient();
InvocationContext ic = wc.newInvocation( "http://localhost/SimpleServlet" );
assertTrue( "Did not find a filter", ic.isFilterActive() );
Filter filter = ic.getFilter();
assertNotNull( "Filter is null", filter );
assertEquals( "Filter class", TrivialFilter.class, filter.getClass() );
ic.pushFilter( ic.getRequest(), ic.getResponse() );
assertFalse( "Did not switch to servlet", ic.isFilterActive() );
assertEquals( "Servlet class", SimpleGetServlet.class, ic.getServlet().getClass() );
ic.popRequest();
assertTrue( "Did not pop back to filter", ic.isFilterActive() );
assertSame( "Restored filter", filter, ic.getFilter() );
}
/**
* Verifies that a simple filter will be called before a servlet with the same URL.
*/
public void testNamedFilterOrder() throws Exception {
WebXMLString wxs = new WebXMLString();
wxs.addServlet( "Simple", "/SimpleServlet", SimpleGetServlet.class );
wxs.addFilterForServlet( "Trivial", TrivialFilter.class, "Simple" );
wxs.addFilterForServlet( "Attribute", AttributeFilter.class, "Simple" );
ServletRunner sr = new ServletRunner( wxs.asInputStream() );
ServletUnitClient wc = sr.newClient();
InvocationContext ic = wc.newInvocation( "http://localhost/SimpleServlet" );
Filter filter1 = ic.getFilter();
assertEquals( "Filter 1 class", TrivialFilter.class, filter1.getClass() );
ic.pushFilter( ic.getRequest(), ic.getResponse() );
assertTrue( "Did not find a filter", ic.isFilterActive() );
Filter filter2 = ic.getFilter();
assertEquals( "Filter 2 class", AttributeFilter.class, filter2.getClass() );
ic.pushFilter( ic.getRequest(), ic.getResponse() );
assertFalse( "Did not switch to servlet", ic.isFilterActive() );
assertEquals( "Servlet class", SimpleGetServlet.class, ic.getServlet().getClass() );
ic.popRequest();
assertSame( "Restored filter 2", filter2, ic.getFilter() );
ic.popRequest();
assertSame( "Restored filter 1", filter1, ic.getFilter() );
}
/**
* Verifies that request / response wrappering for filters is supported.
*/
public void testFilterRequestWrapping() throws Exception {
WebXMLString wxs = new WebXMLString();
wxs.addServlet( "Simple", "/SimpleServlet", SimpleGetServlet.class );
wxs.addFilterForServlet( "Trivial", TrivialFilter.class, "Simple" );
ServletRunner sr = new ServletRunner( wxs.asInputStream() );
ServletUnitClient wc = sr.newClient();
InvocationContext ic = wc.newInvocation( "http://localhost/SimpleServlet" );
HttpServletRequest originalRequest = ic.getRequest();
HttpServletResponse originalResponse = ic.getResponse();
HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper( originalResponse );
HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper( originalRequest );
ic.pushFilter( requestWrapper, responseWrapper );
assertFalse( "Did not switch to servlet", ic.isFilterActive() );
assertSame( "Servlet request", requestWrapper, ic.getRequest() );
assertSame( "Servlet response", responseWrapper, ic.getResponse() );
ic.popRequest();
assertSame( "Filter request", originalRequest, ic.getRequest() );
assertSame( "Filter response", originalResponse, ic.getResponse() );
}
/**
* Verifies that the filter chain invokes the servlet.
*/
public void testFilterChain() throws Exception {
WebXMLString wxs = new WebXMLString();
wxs.addServlet( "Simple", "/SimpleServlet", SimpleGetServlet.class );
wxs.addFilterForServlet( "Trivial", TrivialFilter.class, "Simple" );
ServletRunner sr = new ServletRunner( wxs.asInputStream() );
ServletUnitClient wc = sr.newClient();
InvocationContext ic = wc.newInvocation( "http://localhost/SimpleServlet" );
_servletCalled = false;
HttpServletResponse originalResponse = ic.getResponse();
HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper( originalResponse );
ic.getFilterChain().doFilter( ic.getRequest(), responseWrapper );
assertTrue( "Servlet was not called", _servletCalled );
assertTrue( "Filter marked as active", ic.isFilterActive() );
assertSame( "Response object after doFilter", originalResponse, ic.getResponse() );
}
/**
* Verifies that filters are automatically called.
*/
public void testFilterInvocation() throws Exception {
WebXMLString wxs = new WebXMLString();
wxs.addServlet( "Simple", "/SimpleServlet", SimpleGetServlet.class );
wxs.addFilterForServlet( "Attribute", AttributeFilter.class, "Simple" );
ServletRunner sr = new ServletRunner( wxs.asInputStream() );
ServletUnitClient wc = sr.newClient();
WebResponse wr = wc.getResponse( "http://localhost/SimpleServlet" );
assertEquals( "Filtered response ", "by-filter", wr.getText().trim() );
}
/**
* Verifies that a simple filter is associated with a url pattern.
*/
public void testUrlFilterAssociation() throws Exception {
WebXMLString wxs = new WebXMLString();
wxs.addServlet( "Simple", "/helpMe/SimpleServlet", SimpleGetServlet.class );
wxs.addFilterForUrl( "Trivial", TrivialFilter.class, "/helpMe/*" );
wxs.addFilterForUrl( "Other", AttributeFilter.class, "/Simple" );
WebApplication application = new WebApplication( HttpUnitUtils.newParser().parse( new InputSource( wxs.asInputStream() ) ), null );
ServletMetaData metaData = application.getServletRequest( new URL( "http://localhost/helpMe/SimpleServlet" ) );
FilterMetaData[] filters = metaData.getFilters();
assertEquals( "number of associated filters", 1, filters.length );
assertEquals( "filter class", TrivialFilter.class, filters[0].getFilter().getClass() );
}
public void testFilterMapping() throws Exception {
FilterUrlMap map = new FilterUrlMap();
map.put( "/foo/bar/*", FILTER1 );
map.put( "/baz/*", FILTER2 );
map.put( "/catalog", FILTER3 );
map.put( "*.bop", FILTER4 );
map.put( "/foo/bar/*", FILTER5 );
map.put( "/foo/*", FILTER6 );
checkMapping( map, "/catalog", new FilterMetaData[] { FILTER3 } );
checkMapping( map, "/catalog/racecar.bop", new FilterMetaData[] { FILTER4 } );
checkMapping( map, "/index.bop", new FilterMetaData[] { FILTER4 } );
checkMapping( map, "/foo/bar/index.html", new FilterMetaData[] { FILTER1, FILTER5, FILTER6 } );
checkMapping( map, "/foo/index.bop", new FilterMetaData[] { FILTER4, FILTER6 } );
checkMapping( map, "/baz", new FilterMetaData[] { FILTER2 } );
checkMapping( map, "/bazel", new FilterMetaData[0] );
checkMapping( map, "/baz/index.html", new FilterMetaData[] { FILTER2 } );
checkMapping( map, "/something/else", new FilterMetaData[0] );
}
private void checkMapping( FilterUrlMap map, String urlString, FilterMetaData[] expectedFilters ) {
assertEquals( "Filters selected for '" + urlString + "'",
Arrays.asList( expectedFilters),
Arrays.asList( map.getMatchingFilters( urlString ) ) );
}
public void testFilterInitialization() throws Exception {
WebXMLString wxs = new WebXMLString();
wxs.addServlet( "Simple", "/SimpleServlet", SimpleGetServlet.class );
Properties params = new Properties();
params.setProperty( "color", "red" );
params.setProperty( "age", "12" );
wxs.addFilterForServlet( "Config", AttributeFilter.class, "Simple", params );
ServletRunner sr = new ServletRunner( wxs.asInputStream() );
ServletUnitClient wc = sr.newClient();
InvocationContext ic = wc.newInvocation( "http://localhost/SimpleServlet" );
AttributeFilter filter = (AttributeFilter) ic.getFilter();
FilterConfig filterConfig = filter._filterConfig;
assertNotNull( "Filter was not initialized", filterConfig );
assertEquals( "Filter name", "Config", filterConfig.getFilterName() );
assertNotNull( "No servlet context provided", filterConfig.getServletContext() );
assertNull( "init parameter 'gender' should be null", filterConfig.getInitParameter( "gender" ) );
assertEquals( "init parameter 'red'", "red", filterConfig.getInitParameter( "color" ) );
ArrayList names = new ArrayList();
for (Enumeration e = filterConfig.getInitParameterNames(); e.hasMoreElements(); ) {
String name = (String) e.nextElement();
names.add( name );
}
assertEquals( "Number of names in enumeration", 2, names.size() );
assertTrue( "'color' not found in enumeration", names.contains( "color" ) );
assertTrue( "'age' not found in enumeration", names.contains( "age" ) );
}
// TODO combination of named and url filters (url filters go first)
// TODO filter shutdown
// TODO filters with request dispatchers
// TODO filters throwing UnavailableException
static class AttributeFilter implements Filter {
private FilterConfig _filterConfig;
public void init( FilterConfig filterConfig ) throws ServletException {
_filterConfig = filterConfig;
}
public void destroy() {}
public void doFilter( ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain ) throws IOException, ServletException {
servletRequest.setAttribute( "called", "by-filter" );
filterChain.doFilter( servletRequest, servletResponse );
}
}
static class TrivialFilter implements Filter {
public void init( FilterConfig filterConfig ) throws ServletException {}
public void destroy() {}
public void doFilter( ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain ) throws IOException, ServletException {
servletRequest.setAttribute( "called", "trivially" );
filterChain.doFilter( servletRequest, servletResponse );
}
}
static class SimpleGetServlet extends HttpServlet {
protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
resp.setContentType( "text/html" );
PrintWriter pw = resp.getWriter();
pw.print( req.getAttribute( "called" ) );
pw.close();
_servletCalled = true;
}
}
static class FilterMetaDataImpl implements FilterMetaData {
private int _index;
public FilterMetaDataImpl( int index ) {
_index = index;
}
public Filter getFilter() throws ServletException {
return null;
}
public String toString() {
return "Filter" + _index;
}
}
}
|