1
1
/*
2
- * Copyright 2002-2021 the original author or authors.
2
+ * Copyright 2002-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
18
18
19
19
import java .io .File ;
20
20
import java .io .IOException ;
21
- import java .net .URL ;
22
21
import java .net .URLDecoder ;
23
22
import java .nio .charset .Charset ;
24
23
import java .nio .charset .StandardCharsets ;
32
31
import java .util .Set ;
33
32
import java .util .StringTokenizer ;
34
33
35
- import com .gargoylesoftware .htmlunit .CookieManager ;
36
34
import com .gargoylesoftware .htmlunit .FormEncodingType ;
37
35
import com .gargoylesoftware .htmlunit .WebClient ;
38
36
import com .gargoylesoftware .htmlunit .WebRequest ;
69
67
*
70
68
* @author Rob Winch
71
69
* @author Sam Brannen
70
+ * @author Rossen Stoyanchev
72
71
* @since 4.2
73
72
* @see MockMvcWebConnection
74
73
*/
@@ -111,54 +110,60 @@ public HtmlUnitRequestBuilder(Map<String, MockHttpSession> sessions, WebClient w
111
110
}
112
111
113
112
113
+ /**
114
+ * Set the contextPath to be used.
115
+ * <p>The value may be null in which case the first path segment of the
116
+ * URL is turned into the contextPath. Otherwise it must conform to
117
+ * {@link HttpServletRequest#getContextPath()} which states it can be
118
+ * an empty string, or it must start with a "/" and not end with a "/".
119
+ * @param contextPath a valid contextPath
120
+ * @throws IllegalArgumentException if the contextPath is not a valid
121
+ * {@link HttpServletRequest#getContextPath()}
122
+ */
123
+ public void setContextPath (@ Nullable String contextPath ) {
124
+ MockMvcWebConnection .validateContextPath (contextPath );
125
+ this .contextPath = contextPath ;
126
+ }
127
+
128
+ public void setForwardPostProcessor (RequestPostProcessor forwardPostProcessor ) {
129
+ this .forwardPostProcessor = forwardPostProcessor ;
130
+ }
131
+
132
+
114
133
@ Override
115
134
public MockHttpServletRequest buildRequest (ServletContext servletContext ) {
116
- Charset charset = getCharset ();
117
135
String httpMethod = this .webRequest .getHttpMethod ().name ();
118
- UriComponents uriComponents = uriComponents ();
119
- String path = uriComponents .getPath ();
136
+ UriComponents uri = UriComponentsBuilder .fromUriString (this .webRequest .getUrl ().toExternalForm ()).build ();
120
137
121
- MockHttpServletRequest request =
122
- new HtmlUnitMockHttpServletRequest ( servletContext , httpMethod , (path != null ? path : "" ));
138
+ MockHttpServletRequest request = new HtmlUnitMockHttpServletRequest (
139
+ servletContext , httpMethod , (uri . getPath () != null ? uri . getPath () : "" ));
123
140
124
141
parent (request , this .parentBuilder );
125
- String host = uriComponents .getHost ();
126
- request .setServerName (host != null ? host : "" ); // needs to be first for additional headers
142
+
143
+ request .setProtocol ("HTTP/1.1" );
144
+ request .setScheme (uri .getScheme () != null ? uri .getScheme () : "" );
145
+ request .setServerName (uri .getHost () != null ? uri .getHost () : "" ); // needs to be first for additional headers
146
+ ports (uri , request );
127
147
authType (request );
148
+ contextPath (request , uri );
149
+ servletPath (uri , request );
150
+ request .setPathInfo (null );
151
+
152
+ Charset charset = this .webRequest .getCharset ();
153
+ charset = (charset != null ? charset : StandardCharsets .ISO_8859_1 );
128
154
request .setCharacterEncoding (charset .name ());
129
155
content (request , charset );
130
- contextPath (request , uriComponents );
131
156
contentType (request );
157
+
132
158
cookies (request );
133
- headers ( request );
159
+ this . webRequest . getAdditionalHeaders (). forEach ( request :: addHeader );
134
160
locales (request );
135
- servletPath (uriComponents , request );
136
- params (request , uriComponents );
137
- ports (uriComponents , request );
138
- request .setProtocol ("HTTP/1.1" );
139
- request .setQueryString (uriComponents .getQuery ());
140
- String scheme = uriComponents .getScheme ();
141
- request .setScheme (scheme != null ? scheme : "" );
142
- request .setPathInfo (null );
161
+ params (request , uri );
162
+ request .setQueryString (uri .getQuery ());
143
163
144
164
return postProcess (request );
145
165
}
146
166
147
- private Charset getCharset () {
148
- Charset charset = this .webRequest .getCharset ();
149
- return (charset != null ? charset : StandardCharsets .ISO_8859_1 );
150
- }
151
-
152
- private MockHttpServletRequest postProcess (MockHttpServletRequest request ) {
153
- if (this .parentPostProcessor != null ) {
154
- request = this .parentPostProcessor .postProcessRequest (request );
155
- }
156
- if (this .forwardPostProcessor != null ) {
157
- request = this .forwardPostProcessor .postProcessRequest (request );
158
- }
159
- return request ;
160
- }
161
-
162
167
private void parent (MockHttpServletRequest request , @ Nullable RequestBuilder parent ) {
163
168
if (parent == null ) {
164
169
return ;
@@ -208,50 +213,30 @@ private void parent(MockHttpServletRequest request, @Nullable RequestBuilder par
208
213
}
209
214
}
210
215
211
- /**
212
- * Set the contextPath to be used.
213
- * <p>The value may be null in which case the first path segment of the
214
- * URL is turned into the contextPath. Otherwise it must conform to
215
- * {@link HttpServletRequest#getContextPath()} which states it can be
216
- * an empty string, or it must start with a "/" and not end with a "/".
217
- * @param contextPath a valid contextPath
218
- * @throws IllegalArgumentException if the contextPath is not a valid
219
- * {@link HttpServletRequest#getContextPath()}
220
- */
221
- public void setContextPath (@ Nullable String contextPath ) {
222
- MockMvcWebConnection .validateContextPath (contextPath );
223
- this .contextPath = contextPath ;
224
- }
225
-
226
- public void setForwardPostProcessor (RequestPostProcessor forwardPostProcessor ) {
227
- this .forwardPostProcessor = forwardPostProcessor ;
216
+ private void ports (UriComponents uriComponents , MockHttpServletRequest request ) {
217
+ int serverPort = uriComponents .getPort ();
218
+ request .setServerPort (serverPort );
219
+ if (serverPort == -1 ) {
220
+ int portConnection = this .webRequest .getUrl ().getDefaultPort ();
221
+ request .setLocalPort (serverPort );
222
+ request .setRemotePort (portConnection );
223
+ }
224
+ else {
225
+ request .setRemotePort (serverPort );
226
+ }
228
227
}
229
228
230
229
private void authType (MockHttpServletRequest request ) {
231
- String authorization = header ("Authorization" );
230
+ String authorization = getHeader ("Authorization" );
232
231
String [] authSplit = StringUtils .split (authorization , ": " );
233
232
if (authSplit != null ) {
234
233
request .setAuthType (authSplit [0 ]);
235
234
}
236
235
}
237
236
238
- private void content (MockHttpServletRequest request , Charset charset ) {
239
- String requestBody = this .webRequest .getRequestBody ();
240
- if (requestBody == null ) {
241
- return ;
242
- }
243
- request .setContent (requestBody .getBytes (charset ));
244
- }
245
-
246
- private void contentType (MockHttpServletRequest request ) {
247
- String contentType = header ("Content-Type" );
248
- if (contentType == null ) {
249
- FormEncodingType encodingType = this .webRequest .getEncodingType ();
250
- if (encodingType != null ) {
251
- contentType = encodingType .getName ();
252
- }
253
- }
254
- request .setContentType (contentType != null ? contentType : MediaType .ALL_VALUE );
237
+ @ Nullable
238
+ private String getHeader (String headerName ) {
239
+ return this .webRequest .getAdditionalHeaders ().get (headerName );
255
240
}
256
241
257
242
private void contextPath (MockHttpServletRequest request , UriComponents uriComponents ) {
@@ -273,10 +258,36 @@ private void contextPath(MockHttpServletRequest request, UriComponents uriCompon
273
258
}
274
259
}
275
260
261
+ private void servletPath (UriComponents uriComponents , MockHttpServletRequest request ) {
262
+ String path = uriComponents .getPath ();
263
+ String requestPath = (path != null ? path : "" );
264
+ String servletPath = requestPath .substring (request .getContextPath ().length ());
265
+ request .setServletPath (servletPath );
266
+ }
267
+
268
+ private void content (MockHttpServletRequest request , Charset charset ) {
269
+ String requestBody = this .webRequest .getRequestBody ();
270
+ if (requestBody == null ) {
271
+ return ;
272
+ }
273
+ request .setContent (requestBody .getBytes (charset ));
274
+ }
275
+
276
+ private void contentType (MockHttpServletRequest request ) {
277
+ String contentType = getHeader ("Content-Type" );
278
+ if (contentType == null ) {
279
+ FormEncodingType encodingType = this .webRequest .getEncodingType ();
280
+ if (encodingType != null ) {
281
+ contentType = encodingType .getName ();
282
+ }
283
+ }
284
+ request .setContentType (contentType != null ? contentType : MediaType .ALL_VALUE );
285
+ }
286
+
276
287
private void cookies (MockHttpServletRequest request ) {
277
288
List <Cookie > cookies = new ArrayList <>();
278
289
279
- String cookieHeaderValue = header ("Cookie" );
290
+ String cookieHeaderValue = getHeader ("Cookie" );
280
291
if (cookieHeaderValue != null ) {
281
292
StringTokenizer tokens = new StringTokenizer (cookieHeaderValue , "=;" );
282
293
while (tokens .hasMoreTokens ()) {
@@ -312,15 +323,6 @@ private void processCookie(MockHttpServletRequest request, List<Cookie> cookies,
312
323
}
313
324
}
314
325
315
- @ Nullable
316
- private String header (String headerName ) {
317
- return this .webRequest .getAdditionalHeaders ().get (headerName );
318
- }
319
-
320
- private void headers (MockHttpServletRequest request ) {
321
- this .webRequest .getAdditionalHeaders ().forEach (request ::addHeader );
322
- }
323
-
324
326
private MockHttpSession httpSession (MockHttpServletRequest request , final String sessionid ) {
325
327
MockHttpSession session ;
326
328
synchronized (this .sessions ) {
@@ -341,11 +343,11 @@ private MockHttpSession httpSession(MockHttpServletRequest request, final String
341
343
}
342
344
343
345
private void addSessionCookie (MockHttpServletRequest request , String sessionid ) {
344
- getCookieManager ().addCookie (createCookie (request , sessionid ));
346
+ this . webClient . getCookieManager ().addCookie (createCookie (request , sessionid ));
345
347
}
346
348
347
349
private void removeSessionCookie (MockHttpServletRequest request , String sessionid ) {
348
- getCookieManager ().removeCookie (createCookie (request , sessionid ));
350
+ this . webClient . getCookieManager ().removeCookie (createCookie (request , sessionid ));
349
351
}
350
352
351
353
private com .gargoylesoftware .htmlunit .util .Cookie createCookie (MockHttpServletRequest request , String sessionid ) {
@@ -354,7 +356,7 @@ private com.gargoylesoftware.htmlunit.util.Cookie createCookie(MockHttpServletRe
354
356
}
355
357
356
358
private void locales (MockHttpServletRequest request ) {
357
- String locale = header ("Accept-Language" );
359
+ String locale = getHeader ("Accept-Language" );
358
360
if (locale == null ) {
359
361
request .addPreferredLocale (Locale .getDefault ());
360
362
}
@@ -407,36 +409,18 @@ private byte[] readAllBytes(File file) {
407
409
}
408
410
}
409
411
410
- private void servletPath (MockHttpServletRequest request , String requestPath ) {
411
- String servletPath = requestPath .substring (request .getContextPath ().length ());
412
- request .setServletPath (servletPath );
413
- }
414
-
415
- private void servletPath (UriComponents uriComponents , MockHttpServletRequest request ) {
416
- if ("" .equals (request .getPathInfo ())) {
417
- request .setPathInfo (null );
418
- }
419
- String path = uriComponents .getPath ();
420
- servletPath (request , (path != null ? path : "" ));
421
- }
422
-
423
- private void ports (UriComponents uriComponents , MockHttpServletRequest request ) {
424
- int serverPort = uriComponents .getPort ();
425
- request .setServerPort (serverPort );
426
- if (serverPort == -1 ) {
427
- int portConnection = this .webRequest .getUrl ().getDefaultPort ();
428
- request .setLocalPort (serverPort );
429
- request .setRemotePort (portConnection );
412
+ private MockHttpServletRequest postProcess (MockHttpServletRequest request ) {
413
+ if (this .parentPostProcessor != null ) {
414
+ request = this .parentPostProcessor .postProcessRequest (request );
430
415
}
431
- else {
432
- request . setRemotePort ( serverPort );
416
+ if ( this . forwardPostProcessor != null ) {
417
+ request = this . forwardPostProcessor . postProcessRequest ( request );
433
418
}
419
+ return request ;
434
420
}
435
421
436
- private UriComponents uriComponents () {
437
- URL url = this .webRequest .getUrl ();
438
- return UriComponentsBuilder .fromUriString (url .toExternalForm ()).build ();
439
- }
422
+
423
+ /* Mergeable methods */
440
424
441
425
@ Override
442
426
public boolean isMergeEnabled () {
@@ -461,10 +445,6 @@ public Object merge(@Nullable Object parent) {
461
445
return this ;
462
446
}
463
447
464
- private CookieManager getCookieManager () {
465
- return this .webClient .getCookieManager ();
466
- }
467
-
468
448
469
449
/**
470
450
* An extension to {@link MockHttpServletRequest} that ensures that when a
0 commit comments