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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
package com.meterware.servletunit;
/********************************************************************************************************************
* $Id: RequestContextTest.java 887 2008-04-03 06:20:33Z wolfgang_fahl $
*
* Copyright (c) 2003, 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 com.meterware.httpunit.HttpUnitTest;
import java.net.URL;
import java.util.Enumeration;
import java.util.Map;
import java.util.Locale;
import java.security.Principal;
import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.io.BufferedReader;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletInputStream;
import javax.servlet.RequestDispatcher;
import junit.framework.TestSuite;
/**
*
* @author <a href="russgold@httpunit.org">Russell Gold</a>
**/
public class RequestContextTest extends HttpUnitTest {
public static void main( String args[] ) {
junit.textui.TestRunner.run( suite() );
}
public static TestSuite suite() {
return new TestSuite( RequestContextTest.class );
}
public RequestContextTest( String testName ) {
super( testName );
}
/**
* Verify parsing of a query string.
*/
public void testQueryStringParsing() throws Exception {
RequestContext rc = new RequestContext( new URL( "http://localhost/basic?param=red¶m1=old¶m=blue" ));
assertMatchingSet( "parameter names", new String[] { "param", "param1" }, rc.getParameterNames() );
assertMatchingSet( "param values", new String[] { "red", "blue" }, rc.getParameterValues( "param" ) );
assertEquals( "param1 value", "old", ((String[]) rc.getParameterMap().get( "param1"))[0] );
}
/**
* Verify override of parent request parameters.
*/
public void testParameterOverride() throws Exception {
HttpServletRequest request = new DummyHttpServletRequest( new URL( "http://localhost/basic?param=red¶m1=old¶m=blue" ) );
RequestContext context = new RequestContext( new URL( "http://localhost/second?param=yellow¶m2=fast" ));
context.setParentRequest( request );
assertMatchingSet( "parameter names", new String[] { "param", "param1", "param2" }, context.getParameterNames() );
assertMatchingSet( "param values", new String[] { "yellow" }, context.getParameterValues( "param" ) );
assertEquals( "param1 value", "old", ((String[]) context.getParameterMap().get( "param1"))[0] );
}
/**
* Verify parsing of message body parameters.
*/
public void testPostParameterParsing() throws Exception {
RequestContext rc = new RequestContext( new URL( "http://localhost/basic" ));
rc.setMessageBody( "param=red¶m1=old¶m=blue".getBytes() );
assertMatchingSet( "parameter names", new String[] { "param", "param1" }, rc.getParameterNames() );
assertMatchingSet( "param values", new String[] { "red", "blue" }, rc.getParameterValues( "param" ) );
assertEquals( "param1 value", "old", ((String[]) rc.getParameterMap().get( "param1"))[0] );
}
/**
* Verify parsing of message body parameters using a specified character encoding.
*/
public void testEncodedParameterParsing() throws Exception {
RequestContext rc = new RequestContext( new URL( "http://localhost/basic" ));
String hebrewValue = "\u05d0\u05d1\u05d2\u05d3";
String paramString = "param=red¶m1=%E0%E1%E2%E3¶m=blue";
rc.setMessageBody( paramString.getBytes( "iso-8859-1" ) );
rc.setMessageEncoding( "iso-8859-8" );
assertMatchingSet( "parameter names", new String[] { "param", "param1" }, rc.getParameterNames() );
assertMatchingSet( "param values", new String[] { "red", "blue" }, rc.getParameterValues( "param" ) );
assertEquals( "param1 value", hebrewValue, ((String[]) rc.getParameterMap().get( "param1"))[0] );
}
class DummyHttpServletRequest implements HttpServletRequest {
private RequestContext _requestContext;
public DummyHttpServletRequest( URL requestURL ) {
_requestContext = new RequestContext( requestURL );
}
public String getAuthType() {
return null;
}
public Cookie[] getCookies() {
return new Cookie[0];
}
public long getDateHeader( String s ) {
return 0;
}
public String getHeader( String s ) {
return null;
}
public Enumeration getHeaders( String s ) {
return null;
}
public Enumeration getHeaderNames() {
return null;
}
public int getIntHeader( String s ) {
return 0;
}
public String getMethod() {
return null;
}
public String getPathInfo() {
return null;
}
public String getPathTranslated() {
return null;
}
public String getContextPath() {
return null;
}
public String getQueryString() {
return null;
}
public String getRemoteUser() {
return null;
}
public boolean isUserInRole( String s ) {
return false;
}
public Principal getUserPrincipal() {
return null;
}
public String getRequestedSessionId() {
return null;
}
public String getRequestURI() {
return null;
}
public StringBuffer getRequestURL() {
return null;
}
public String getServletPath() {
return null;
}
public HttpSession getSession( boolean b ) {
return null;
}
public HttpSession getSession() {
return null;
}
public boolean isRequestedSessionIdValid() {
return false;
}
public boolean isRequestedSessionIdFromCookie() {
return false;
}
public boolean isRequestedSessionIdFromURL() {
return false;
}
public boolean isRequestedSessionIdFromUrl() {
return false;
}
public Object getAttribute( String s ) {
return null;
}
public Enumeration getAttributeNames() {
return null;
}
public String getCharacterEncoding() {
return null;
}
public void setCharacterEncoding( String s ) throws UnsupportedEncodingException {
}
public int getContentLength() {
return 0;
}
public String getContentType() {
return null;
}
public ServletInputStream getInputStream() throws IOException {
return null;
}
public String getParameter( String s ) {
return _requestContext.getParameter( s );
}
public Enumeration getParameterNames() {
return _requestContext.getParameterNames();
}
public String[] getParameterValues( String s ) {
return _requestContext.getParameterValues( s );
}
public Map getParameterMap() {
return _requestContext.getParameterMap();
}
public String getProtocol() {
return null;
}
public String getScheme() {
return null;
}
public String getServerName() {
return null;
}
public int getServerPort() {
return 0;
}
public BufferedReader getReader() throws IOException {
return null;
}
public String getRemoteAddr() {
return null;
}
public String getRemoteHost() {
return null;
}
public void setAttribute( String s, Object o ) {
}
public void removeAttribute( String s ) {
}
public Locale getLocale() {
return null;
}
public Enumeration getLocales() {
return null;
}
public boolean isSecure() {
return false;
}
public RequestDispatcher getRequestDispatcher( String s ) {
return null;
}
public String getRealPath( String s ) {
return null;
}
public int getRemotePort() {
return 0; //To change body of implemented methods use File | Settings | File Templates.
}
public String getLocalName() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public String getLocalAddr() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public int getLocalPort() {
return 0; //To change body of implemented methods use File | Settings | File Templates.
}
}
}
|