Skip to content

Commit 9346c89

Browse files
committed
Polishing in HtmlUnitRequestBuilder
Order methods according to Spring Framework conventions. Order request initialization by URI component. See gh-27837
1 parent 29fe109 commit 9346c89

File tree

1 file changed

+93
-113
lines changed

1 file changed

+93
-113
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java

+93-113
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21-
import java.net.URL;
2221
import java.net.URLDecoder;
2322
import java.nio.charset.Charset;
2423
import java.nio.charset.StandardCharsets;
@@ -32,7 +31,6 @@
3231
import java.util.Set;
3332
import java.util.StringTokenizer;
3433

35-
import com.gargoylesoftware.htmlunit.CookieManager;
3634
import com.gargoylesoftware.htmlunit.FormEncodingType;
3735
import com.gargoylesoftware.htmlunit.WebClient;
3836
import com.gargoylesoftware.htmlunit.WebRequest;
@@ -69,6 +67,7 @@
6967
*
7068
* @author Rob Winch
7169
* @author Sam Brannen
70+
* @author Rossen Stoyanchev
7271
* @since 4.2
7372
* @see MockMvcWebConnection
7473
*/
@@ -111,54 +110,60 @@ public HtmlUnitRequestBuilder(Map<String, MockHttpSession> sessions, WebClient w
111110
}
112111

113112

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+
114133
@Override
115134
public MockHttpServletRequest buildRequest(ServletContext servletContext) {
116-
Charset charset = getCharset();
117135
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();
120137

121-
MockHttpServletRequest request =
122-
new HtmlUnitMockHttpServletRequest(servletContext, httpMethod, (path != null ? path : ""));
138+
MockHttpServletRequest request = new HtmlUnitMockHttpServletRequest(
139+
servletContext, httpMethod, (uri.getPath() != null ? uri.getPath() : ""));
123140

124141
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);
127147
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);
128154
request.setCharacterEncoding(charset.name());
129155
content(request, charset);
130-
contextPath(request, uriComponents);
131156
contentType(request);
157+
132158
cookies(request);
133-
headers(request);
159+
this.webRequest.getAdditionalHeaders().forEach(request::addHeader);
134160
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());
143163

144164
return postProcess(request);
145165
}
146166

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-
162167
private void parent(MockHttpServletRequest request, @Nullable RequestBuilder parent) {
163168
if (parent == null) {
164169
return;
@@ -208,50 +213,30 @@ private void parent(MockHttpServletRequest request, @Nullable RequestBuilder par
208213
}
209214
}
210215

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+
}
228227
}
229228

230229
private void authType(MockHttpServletRequest request) {
231-
String authorization = header("Authorization");
230+
String authorization = getHeader("Authorization");
232231
String[] authSplit = StringUtils.split(authorization, ": ");
233232
if (authSplit != null) {
234233
request.setAuthType(authSplit[0]);
235234
}
236235
}
237236

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);
255240
}
256241

257242
private void contextPath(MockHttpServletRequest request, UriComponents uriComponents) {
@@ -273,10 +258,36 @@ private void contextPath(MockHttpServletRequest request, UriComponents uriCompon
273258
}
274259
}
275260

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+
276287
private void cookies(MockHttpServletRequest request) {
277288
List<Cookie> cookies = new ArrayList<>();
278289

279-
String cookieHeaderValue = header("Cookie");
290+
String cookieHeaderValue = getHeader("Cookie");
280291
if (cookieHeaderValue != null) {
281292
StringTokenizer tokens = new StringTokenizer(cookieHeaderValue, "=;");
282293
while (tokens.hasMoreTokens()) {
@@ -312,15 +323,6 @@ private void processCookie(MockHttpServletRequest request, List<Cookie> cookies,
312323
}
313324
}
314325

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-
324326
private MockHttpSession httpSession(MockHttpServletRequest request, final String sessionid) {
325327
MockHttpSession session;
326328
synchronized (this.sessions) {
@@ -341,11 +343,11 @@ private MockHttpSession httpSession(MockHttpServletRequest request, final String
341343
}
342344

343345
private void addSessionCookie(MockHttpServletRequest request, String sessionid) {
344-
getCookieManager().addCookie(createCookie(request, sessionid));
346+
this.webClient.getCookieManager().addCookie(createCookie(request, sessionid));
345347
}
346348

347349
private void removeSessionCookie(MockHttpServletRequest request, String sessionid) {
348-
getCookieManager().removeCookie(createCookie(request, sessionid));
350+
this.webClient.getCookieManager().removeCookie(createCookie(request, sessionid));
349351
}
350352

351353
private com.gargoylesoftware.htmlunit.util.Cookie createCookie(MockHttpServletRequest request, String sessionid) {
@@ -354,7 +356,7 @@ private com.gargoylesoftware.htmlunit.util.Cookie createCookie(MockHttpServletRe
354356
}
355357

356358
private void locales(MockHttpServletRequest request) {
357-
String locale = header("Accept-Language");
359+
String locale = getHeader("Accept-Language");
358360
if (locale == null) {
359361
request.addPreferredLocale(Locale.getDefault());
360362
}
@@ -407,36 +409,18 @@ private byte[] readAllBytes(File file) {
407409
}
408410
}
409411

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);
430415
}
431-
else {
432-
request.setRemotePort(serverPort);
416+
if (this.forwardPostProcessor != null) {
417+
request = this.forwardPostProcessor.postProcessRequest(request);
433418
}
419+
return request;
434420
}
435421

436-
private UriComponents uriComponents() {
437-
URL url = this.webRequest.getUrl();
438-
return UriComponentsBuilder.fromUriString(url.toExternalForm()).build();
439-
}
422+
423+
/* Mergeable methods */
440424

441425
@Override
442426
public boolean isMergeEnabled() {
@@ -461,10 +445,6 @@ public Object merge(@Nullable Object parent) {
461445
return this;
462446
}
463447

464-
private CookieManager getCookieManager() {
465-
return this.webClient.getCookieManager();
466-
}
467-
468448

469449
/**
470450
* An extension to {@link MockHttpServletRequest} that ensures that when a

0 commit comments

Comments
 (0)