@@ -105,8 +105,8 @@ public MockHttpServletRequest buildRequest(ServletContext servletContext) {
105
105
String httpMethod = this .webRequest .getHttpMethod ().name ();
106
106
UriComponents uriComponents = uriComponents ();
107
107
108
- MockHttpServletRequest request = new HtmlUnitMockHttpServletRequest (servletContext , httpMethod ,
109
- uriComponents .getPath ());
108
+ MockHttpServletRequest request = new HtmlUnitMockHttpServletRequest (
109
+ servletContext , httpMethod , uriComponents .getPath ());
110
110
parent (request , this .parentBuilder );
111
111
request .setServerName (uriComponents .getHost ()); // needs to be first for additional headers
112
112
authType (request );
@@ -123,7 +123,7 @@ public MockHttpServletRequest buildRequest(ServletContext servletContext) {
123
123
request .setProtocol ("HTTP/1.1" );
124
124
request .setQueryString (uriComponents .getQuery ());
125
125
request .setScheme (uriComponents .getScheme ());
126
- pathInfo ( uriComponents , request );
126
+ request . setPathInfo ( null );
127
127
128
128
return postProcess (request );
129
129
}
@@ -223,14 +223,14 @@ private void content(MockHttpServletRequest request, String charset) {
223
223
try {
224
224
request .setContent (requestBody .getBytes (charset ));
225
225
}
226
- catch (UnsupportedEncodingException e ) {
227
- throw new RuntimeException ( e );
226
+ catch (UnsupportedEncodingException ex ) {
227
+ throw new IllegalStateException ( ex );
228
228
}
229
229
}
230
230
231
231
private void contentType (MockHttpServletRequest request ) {
232
232
String contentType = header ("Content-Type" );
233
- request .setContentType (contentType == null ? MediaType . ALL_VALUE . toString () : contentType );
233
+ request .setContentType (contentType != null ? contentType : MediaType . ALL_VALUE );
234
234
}
235
235
236
236
private void contextPath (MockHttpServletRequest request , UriComponents uriComponents ) {
@@ -245,8 +245,8 @@ private void contextPath(MockHttpServletRequest request, UriComponents uriCompon
245
245
}
246
246
else {
247
247
if (!uriComponents .getPath ().startsWith (this .contextPath )) {
248
- throw new IllegalArgumentException (uriComponents .getPath () + " should start with contextPath "
249
- + this .contextPath );
248
+ throw new IllegalArgumentException (uriComponents .getPath () + " should start with contextPath " +
249
+ this .contextPath );
250
250
}
251
251
request .setContextPath (this .contextPath );
252
252
}
@@ -360,21 +360,26 @@ private void locales(MockHttpServletRequest request) {
360
360
private void params (MockHttpServletRequest request , UriComponents uriComponents ) {
361
361
for (Entry <String , List <String >> entry : uriComponents .getQueryParams ().entrySet ()) {
362
362
String name = entry .getKey ();
363
+ String urlDecodedName = urlDecode (name );
363
364
for (String value : entry .getValue ()) {
364
- try {
365
- value = (value != null ? URLDecoder .decode (value , "UTF-8" ) : "" );
366
- request .addParameter (name , value );
367
- }
368
- catch (UnsupportedEncodingException e ) {
369
- throw new RuntimeException (e );
370
- }
365
+ value = (value != null ? urlDecode (value ) : "" );
366
+ request .addParameter (urlDecodedName , value );
371
367
}
372
368
}
373
369
for (NameValuePair param : this .webRequest .getRequestParameters ()) {
374
370
request .addParameter (param .getName (), param .getValue ());
375
371
}
376
372
}
377
373
374
+ private String urlDecode (String value ) {
375
+ try {
376
+ return URLDecoder .decode (value , "UTF-8" );
377
+ }
378
+ catch (UnsupportedEncodingException ex ) {
379
+ throw new IllegalStateException (ex );
380
+ }
381
+ }
382
+
378
383
private Locale parseLocale (String locale ) {
379
384
Matcher matcher = LOCALE_PATTERN .matcher (locale );
380
385
if (!matcher .matches ()) {
@@ -392,10 +397,6 @@ private Locale parseLocale(String locale) {
392
397
return new Locale (language , country , qualifier );
393
398
}
394
399
395
- private void pathInfo (UriComponents uriComponents , MockHttpServletRequest request ) {
396
- request .setPathInfo (null );
397
- }
398
-
399
400
private void servletPath (MockHttpServletRequest request , String requestPath ) {
400
401
String servletPath = requestPath .substring (request .getContextPath ().length ());
401
402
if ("" .equals (servletPath )) {
@@ -426,8 +427,7 @@ private void ports(UriComponents uriComponents, MockHttpServletRequest request)
426
427
427
428
private UriComponents uriComponents () {
428
429
URL url = this .webRequest .getUrl ();
429
- UriComponentsBuilder uriBldr = UriComponentsBuilder .fromUriString (url .toExternalForm ());
430
- return uriBldr .build ();
430
+ return UriComponentsBuilder .fromUriString (url .toExternalForm ()).build ();
431
431
}
432
432
433
433
@ Override
@@ -450,14 +450,18 @@ public Object merge(Object parent) {
450
450
return this ;
451
451
}
452
452
453
+ private CookieManager getCookieManager () {
454
+ return this .webClient .getCookieManager ();
455
+ }
456
+
453
457
454
458
/**
455
- * An extension to {@link MockHttpServletRequest} that ensures that
456
- * when a new {@link HttpSession} is created, it is added to the managed sessions.
459
+ * An extension to {@link MockHttpServletRequest} that ensures that when a
460
+ * new {@link HttpSession} is created, it is added to the managed sessions.
457
461
*/
458
462
private final class HtmlUnitMockHttpServletRequest extends MockHttpServletRequest {
459
463
460
- private HtmlUnitMockHttpServletRequest (ServletContext servletContext , String method , String requestURI ) {
464
+ public HtmlUnitMockHttpServletRequest (ServletContext servletContext , String method , String requestURI ) {
461
465
super (servletContext , method , requestURI );
462
466
}
463
467
@@ -486,16 +490,17 @@ public void setSession(HttpSession session) {
486
490
}
487
491
}
488
492
493
+
489
494
/**
490
495
* An extension to {@link MockHttpSession} that ensures when
491
- * {@link #invalidate()} is called that the {@link HttpSession} is
492
- * removed from the managed sessions.
496
+ * {@link #invalidate()} is called that the {@link HttpSession}
497
+ * is removed from the managed sessions.
493
498
*/
494
499
private final class HtmlUnitMockHttpSession extends MockHttpSession {
495
500
496
501
private final MockHttpServletRequest request ;
497
502
498
- private HtmlUnitMockHttpSession (MockHttpServletRequest request ) {
503
+ public HtmlUnitMockHttpSession (MockHttpServletRequest request ) {
499
504
super (request .getServletContext ());
500
505
this .request = request ;
501
506
}
@@ -514,8 +519,4 @@ public void invalidate() {
514
519
}
515
520
}
516
521
517
- private CookieManager getCookieManager () {
518
- return this .webClient .getCookieManager ();
519
- }
520
-
521
522
}
0 commit comments