@@ -105,8 +105,8 @@ public MockHttpServletRequest buildRequest(ServletContext servletContext) {
105105 String httpMethod = this .webRequest .getHttpMethod ().name ();
106106 UriComponents uriComponents = uriComponents ();
107107
108- MockHttpServletRequest request = new HtmlUnitMockHttpServletRequest (servletContext , httpMethod ,
109- uriComponents .getPath ());
108+ MockHttpServletRequest request = new HtmlUnitMockHttpServletRequest (
109+ servletContext , httpMethod , uriComponents .getPath ());
110110 parent (request , this .parentBuilder );
111111 request .setServerName (uriComponents .getHost ()); // needs to be first for additional headers
112112 authType (request );
@@ -123,7 +123,7 @@ public MockHttpServletRequest buildRequest(ServletContext servletContext) {
123123 request .setProtocol ("HTTP/1.1" );
124124 request .setQueryString (uriComponents .getQuery ());
125125 request .setScheme (uriComponents .getScheme ());
126- pathInfo ( uriComponents , request );
126+ request . setPathInfo ( null );
127127
128128 return postProcess (request );
129129 }
@@ -223,14 +223,14 @@ private void content(MockHttpServletRequest request, String charset) {
223223 try {
224224 request .setContent (requestBody .getBytes (charset ));
225225 }
226- catch (UnsupportedEncodingException e ) {
227- throw new RuntimeException ( e );
226+ catch (UnsupportedEncodingException ex ) {
227+ throw new IllegalStateException ( ex );
228228 }
229229 }
230230
231231 private void contentType (MockHttpServletRequest request ) {
232232 String contentType = header ("Content-Type" );
233- request .setContentType (contentType == null ? MediaType . ALL_VALUE . toString () : contentType );
233+ request .setContentType (contentType != null ? contentType : MediaType . ALL_VALUE );
234234 }
235235
236236 private void contextPath (MockHttpServletRequest request , UriComponents uriComponents ) {
@@ -245,8 +245,8 @@ private void contextPath(MockHttpServletRequest request, UriComponents uriCompon
245245 }
246246 else {
247247 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 );
250250 }
251251 request .setContextPath (this .contextPath );
252252 }
@@ -360,21 +360,26 @@ private void locales(MockHttpServletRequest request) {
360360 private void params (MockHttpServletRequest request , UriComponents uriComponents ) {
361361 for (Entry <String , List <String >> entry : uriComponents .getQueryParams ().entrySet ()) {
362362 String name = entry .getKey ();
363+ String urlDecodedName = urlDecode (name );
363364 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 );
371367 }
372368 }
373369 for (NameValuePair param : this .webRequest .getRequestParameters ()) {
374370 request .addParameter (param .getName (), param .getValue ());
375371 }
376372 }
377373
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+
378383 private Locale parseLocale (String locale ) {
379384 Matcher matcher = LOCALE_PATTERN .matcher (locale );
380385 if (!matcher .matches ()) {
@@ -392,10 +397,6 @@ private Locale parseLocale(String locale) {
392397 return new Locale (language , country , qualifier );
393398 }
394399
395- private void pathInfo (UriComponents uriComponents , MockHttpServletRequest request ) {
396- request .setPathInfo (null );
397- }
398-
399400 private void servletPath (MockHttpServletRequest request , String requestPath ) {
400401 String servletPath = requestPath .substring (request .getContextPath ().length ());
401402 if ("" .equals (servletPath )) {
@@ -426,8 +427,7 @@ private void ports(UriComponents uriComponents, MockHttpServletRequest request)
426427
427428 private UriComponents uriComponents () {
428429 URL url = this .webRequest .getUrl ();
429- UriComponentsBuilder uriBldr = UriComponentsBuilder .fromUriString (url .toExternalForm ());
430- return uriBldr .build ();
430+ return UriComponentsBuilder .fromUriString (url .toExternalForm ()).build ();
431431 }
432432
433433 @ Override
@@ -450,14 +450,18 @@ public Object merge(Object parent) {
450450 return this ;
451451 }
452452
453+ private CookieManager getCookieManager () {
454+ return this .webClient .getCookieManager ();
455+ }
456+
453457
454458 /**
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.
457461 */
458462 private final class HtmlUnitMockHttpServletRequest extends MockHttpServletRequest {
459463
460- private HtmlUnitMockHttpServletRequest (ServletContext servletContext , String method , String requestURI ) {
464+ public HtmlUnitMockHttpServletRequest (ServletContext servletContext , String method , String requestURI ) {
461465 super (servletContext , method , requestURI );
462466 }
463467
@@ -486,16 +490,17 @@ public void setSession(HttpSession session) {
486490 }
487491 }
488492
493+
489494 /**
490495 * 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.
493498 */
494499 private final class HtmlUnitMockHttpSession extends MockHttpSession {
495500
496501 private final MockHttpServletRequest request ;
497502
498- private HtmlUnitMockHttpSession (MockHttpServletRequest request ) {
503+ public HtmlUnitMockHttpSession (MockHttpServletRequest request ) {
499504 super (request .getServletContext ());
500505 this .request = request ;
501506 }
@@ -514,8 +519,4 @@ public void invalidate() {
514519 }
515520 }
516521
517- private CookieManager getCookieManager () {
518- return this .webClient .getCookieManager ();
519- }
520-
521522}
0 commit comments