|
|
@ -28,7 +28,6 @@ import java.util.LinkedHashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Locale; |
|
|
|
import java.util.Locale; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletContext; |
|
|
|
import javax.servlet.ServletContext; |
|
|
|
import javax.servlet.ServletRequest; |
|
|
|
import javax.servlet.ServletRequest; |
|
|
@ -101,9 +100,6 @@ public class MockHttpServletRequestBuilder |
|
|
|
@Nullable |
|
|
|
@Nullable |
|
|
|
private Boolean secure; |
|
|
|
private Boolean secure; |
|
|
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
|
|
|
|
private String queryString = ""; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
@Nullable |
|
|
|
private Principal principal; |
|
|
|
private Principal principal; |
|
|
|
|
|
|
|
|
|
|
@ -123,6 +119,8 @@ public class MockHttpServletRequestBuilder |
|
|
|
|
|
|
|
|
|
|
|
private final MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>(); |
|
|
|
private final MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>(); |
|
|
|
|
|
|
|
|
|
|
|
private final List<Cookie> cookies = new ArrayList<>(); |
|
|
|
private final List<Cookie> cookies = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
|
|
private final List<Locale> locales = new ArrayList<>(); |
|
|
|
private final List<Locale> locales = new ArrayList<>(); |
|
|
@ -252,6 +250,10 @@ public class MockHttpServletRequestBuilder |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Set the request body. |
|
|
|
* Set the request body. |
|
|
|
|
|
|
|
* <p>If content is provided and {@link #contentType(MediaType)} is set to |
|
|
|
|
|
|
|
* {@code application/x-www-form-urlencoded}, the content will be parsed |
|
|
|
|
|
|
|
* and used to populate the {@link #param(String, String...) request |
|
|
|
|
|
|
|
* parameters} map. |
|
|
|
* @param content the body content |
|
|
|
* @param content the body content |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public MockHttpServletRequestBuilder content(byte[] content) { |
|
|
|
public MockHttpServletRequestBuilder content(byte[] content) { |
|
|
@ -261,6 +263,10 @@ public class MockHttpServletRequestBuilder |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Set the request body as a UTF-8 String. |
|
|
|
* Set the request body as a UTF-8 String. |
|
|
|
|
|
|
|
* <p>If content is provided and {@link #contentType(MediaType)} is set to |
|
|
|
|
|
|
|
* {@code application/x-www-form-urlencoded}, the content will be parsed |
|
|
|
|
|
|
|
* and used to populate the {@link #param(String, String...) request |
|
|
|
|
|
|
|
* parameters} map. |
|
|
|
* @param content the body content |
|
|
|
* @param content the body content |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public MockHttpServletRequestBuilder content(String content) { |
|
|
|
public MockHttpServletRequestBuilder content(String content) { |
|
|
@ -270,6 +276,10 @@ public class MockHttpServletRequestBuilder |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Set the 'Content-Type' header of the request. |
|
|
|
* Set the 'Content-Type' header of the request. |
|
|
|
|
|
|
|
* <p>If content is provided and {@code contentType} is set to |
|
|
|
|
|
|
|
* {@code application/x-www-form-urlencoded}, the content will be parsed |
|
|
|
|
|
|
|
* and used to populate the {@link #param(String, String...) request |
|
|
|
|
|
|
|
* parameters} map. |
|
|
|
* @param contentType the content type |
|
|
|
* @param contentType the content type |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public MockHttpServletRequestBuilder contentType(MediaType contentType) { |
|
|
|
public MockHttpServletRequestBuilder contentType(MediaType contentType) { |
|
|
@ -332,8 +342,18 @@ public class MockHttpServletRequestBuilder |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Add a request parameter to the {@link MockHttpServletRequest}. |
|
|
|
* Add a request parameter to {@link MockHttpServletRequest#getParameterMap()}. |
|
|
|
* <p>If called more than once, new values get added to existing ones. |
|
|
|
* <p>In the Servlet API, a request parameter may be parsed from the query |
|
|
|
|
|
|
|
* string and/or from the body of an {@code application/x-www-form-urlencoded} |
|
|
|
|
|
|
|
* request. This method simply adds to the request parameter map. You may |
|
|
|
|
|
|
|
* also use add Servlet request parameters by specifying the query or form |
|
|
|
|
|
|
|
* data through one of the following: |
|
|
|
|
|
|
|
* <ul> |
|
|
|
|
|
|
|
* <li>Supply a URL with a query to {@link MockMvcRequestBuilders}. |
|
|
|
|
|
|
|
* <li>Add query params via {@link #queryParam} or {@link #queryParams}. |
|
|
|
|
|
|
|
* <li>Provide {@link #content} with {@link #contentType} |
|
|
|
|
|
|
|
* {@code application/x-www-form-urlencoded}. |
|
|
|
|
|
|
|
* </ul> |
|
|
|
* @param name the parameter name |
|
|
|
* @param name the parameter name |
|
|
|
* @param values one or more values |
|
|
|
* @param values one or more values |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -343,9 +363,7 @@ public class MockHttpServletRequestBuilder |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Add a map of request parameters to the {@link MockHttpServletRequest}, |
|
|
|
* Variant of {@link #param(String, String...)} with a {@link MultiValueMap}. |
|
|
|
* for example when testing a form submission. |
|
|
|
|
|
|
|
* <p>If called more than once, new values get added to existing ones. |
|
|
|
|
|
|
|
* @param params the parameters to add |
|
|
|
* @param params the parameters to add |
|
|
|
* @since 4.2.4 |
|
|
|
* @since 4.2.4 |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -359,36 +377,29 @@ public class MockHttpServletRequestBuilder |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Add a query parameter to the {@link MockHttpServletRequest}. |
|
|
|
* Append to the query string and also add to the |
|
|
|
* <p>If called more than once, new values get added to existing ones. |
|
|
|
* {@link #param(String, String...) request parameters} map. The parameter |
|
|
|
|
|
|
|
* name and value are encoded when they are added to the query string. |
|
|
|
* @param name the parameter name |
|
|
|
* @param name the parameter name |
|
|
|
* @param values one or more values |
|
|
|
* @param values one or more values |
|
|
|
|
|
|
|
* @since 5.2.2 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public MockHttpServletRequestBuilder queryParam(String name, String... values) { |
|
|
|
public MockHttpServletRequestBuilder queryParam(String name, String... values) { |
|
|
|
param(name, values); |
|
|
|
param(name, values); |
|
|
|
String builder = Arrays.stream(values).map(value -> UriUtils.encode(name, StandardCharsets.UTF_8) + |
|
|
|
this.queryParams.addAll(name, Arrays.asList(values)); |
|
|
|
((value != null) ? ("=" + UriUtils.encode(value, StandardCharsets.UTF_8)) : "") + "&" |
|
|
|
|
|
|
|
).collect(Collectors.joining()); |
|
|
|
|
|
|
|
queryString += builder; |
|
|
|
|
|
|
|
return this; |
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Add a map of query parameters to the {@link MockHttpServletRequest}, |
|
|
|
* Append to the query string and also add to the |
|
|
|
* for example when testing a form submission. |
|
|
|
* {@link #params(MultiValueMap)} request parameters} map. The parameter |
|
|
|
* <p>If called more than once, new values get added to existing ones. |
|
|
|
* name and value are encoded when they are added to the query string. |
|
|
|
* @param params the parameters to add |
|
|
|
* @param params the parameters to add |
|
|
|
* @since 4.2.4 |
|
|
|
* @since 5.2.2 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public MockHttpServletRequestBuilder queryParams(MultiValueMap<String, String> params) { |
|
|
|
public MockHttpServletRequestBuilder queryParams(MultiValueMap<String, String> params) { |
|
|
|
params(params); |
|
|
|
params(params); |
|
|
|
StringBuilder builder = new StringBuilder(); |
|
|
|
this.queryParams.addAll(params); |
|
|
|
params.forEach((key, values) -> values.forEach(value -> { |
|
|
|
|
|
|
|
builder.append(UriUtils.encode(key, StandardCharsets.UTF_8)) |
|
|
|
|
|
|
|
.append(((value != null) ? ("=" + UriUtils.encode(value, StandardCharsets.UTF_8)) : "")) |
|
|
|
|
|
|
|
.append("&"); |
|
|
|
|
|
|
|
})); |
|
|
|
|
|
|
|
queryString += builder.toString(); |
|
|
|
|
|
|
|
return this; |
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -581,6 +592,12 @@ public class MockHttpServletRequestBuilder |
|
|
|
this.parameters.put(paramName, entry.getValue()); |
|
|
|
this.parameters.put(paramName, entry.getValue()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
for (Map.Entry<String, List<String>> entry : parentBuilder.queryParams.entrySet()) { |
|
|
|
|
|
|
|
String paramName = entry.getKey(); |
|
|
|
|
|
|
|
if (!this.queryParams.containsKey(paramName)) { |
|
|
|
|
|
|
|
this.queryParams.put(paramName, entry.getValue()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
for (Cookie cookie : parentBuilder.cookies) { |
|
|
|
for (Cookie cookie : parentBuilder.cookies) { |
|
|
|
if (!containsCookie(cookie)) { |
|
|
|
if (!containsCookie(cookie)) { |
|
|
|
this.cookies.add(cookie); |
|
|
|
this.cookies.add(cookie); |
|
|
@ -670,28 +687,22 @@ public class MockHttpServletRequestBuilder |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (this.url.getRawQuery() != null) { |
|
|
|
String query = this.url.getRawQuery(); |
|
|
|
request.setQueryString(this.url.getRawQuery()); |
|
|
|
if (!this.queryParams.isEmpty()) { |
|
|
|
|
|
|
|
String s = UriComponentsBuilder.newInstance().queryParams(this.queryParams).build().encode().getQuery(); |
|
|
|
|
|
|
|
query = StringUtils.isEmpty(query) ? s : query + "&" + s; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (query != null) { |
|
|
|
|
|
|
|
request.setQueryString(query); |
|
|
|
} |
|
|
|
} |
|
|
|
addRequestParams(request, UriComponentsBuilder.fromUri(this.url).build().getQueryParams()); |
|
|
|
addRequestParams(request, UriComponentsBuilder.fromUri(this.url).build().getQueryParams()); |
|
|
|
|
|
|
|
|
|
|
|
this.parameters.forEach((name, values) -> { |
|
|
|
this.parameters.forEach((name, values) -> { |
|
|
|
for (String value : values) { |
|
|
|
for (String value : values) { |
|
|
|
request.addParameter(name, value); |
|
|
|
request.addParameter(name, value); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
StringBuilder queryBuilder = new StringBuilder(); |
|
|
|
|
|
|
|
if (request.getQueryString() != null) { |
|
|
|
|
|
|
|
queryBuilder.append(request.getQueryString()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (this.queryString != null && !"".equals(this.queryString)) { |
|
|
|
|
|
|
|
if (queryBuilder.length() > 0) { |
|
|
|
|
|
|
|
queryBuilder.append("&"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
queryBuilder.append(this.queryString, 0, this.queryString.length() - 1); |
|
|
|
|
|
|
|
request.setQueryString(queryBuilder.toString()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (this.content != null && this.content.length > 0) { |
|
|
|
if (this.content != null && this.content.length > 0) { |
|
|
|
String requestContentType = request.getContentType(); |
|
|
|
String requestContentType = request.getContentType(); |
|
|
|
if (requestContentType != null) { |
|
|
|
if (requestContentType != null) { |
|
|
|