Allow any Accept and Content-Type raw values
See gh-2079
This commit is contained in:
committed by
Rossen Stoyanchev
parent
f8d6896e40
commit
f4509d6e3a
@@ -294,7 +294,8 @@ public class MockHttpServletRequestBuilder
|
||||
* @since 4.1.2
|
||||
*/
|
||||
public MockHttpServletRequestBuilder contentType(String contentType) {
|
||||
this.contentType = MediaType.parseMediaType(contentType).toString();
|
||||
Assert.notNull(contentType, "'contentType' must not be null");
|
||||
this.contentType = contentType;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -314,11 +315,9 @@ public class MockHttpServletRequestBuilder
|
||||
*/
|
||||
public MockHttpServletRequestBuilder accept(String... mediaTypes) {
|
||||
Assert.notEmpty(mediaTypes, "'mediaTypes' must not be empty");
|
||||
List<MediaType> result = new ArrayList<>(mediaTypes.length);
|
||||
for (String mediaType : mediaTypes) {
|
||||
result.add(MediaType.parseMediaType(mediaType));
|
||||
}
|
||||
this.headers.set("Accept", MediaType.toString(result));
|
||||
List<String> result = new ArrayList<>(mediaTypes.length);
|
||||
result.addAll(Arrays.asList(mediaTypes));
|
||||
this.headers.set("Accept", String.join(", ", result));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -712,7 +711,7 @@ public class MockHttpServletRequestBuilder
|
||||
|
||||
if (this.content != null && this.content.length > 0) {
|
||||
String requestContentType = request.getContentType();
|
||||
if (requestContentType != null) {
|
||||
if (requestContentType != null && isValidContentType(requestContentType)) {
|
||||
MediaType mediaType = MediaType.parseMediaType(requestContentType);
|
||||
if (MediaType.APPLICATION_FORM_URLENCODED.includes(mediaType)) {
|
||||
addRequestParams(request, parseFormData(mediaType));
|
||||
@@ -742,6 +741,26 @@ public class MockHttpServletRequestBuilder
|
||||
return request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Some validation checks to find out if processing of a string value make sense.
|
||||
*/
|
||||
private boolean isValidContentType(String mimeType) {
|
||||
if (!StringUtils.hasLength(mimeType)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int index = mimeType.indexOf(';');
|
||||
String fullType = (index >= 0 ? mimeType.substring(0, index) : mimeType).trim();
|
||||
if (fullType.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
int subIndex = fullType.indexOf('/');
|
||||
if (subIndex == -1) {
|
||||
return false;
|
||||
}
|
||||
return subIndex != fullType.length() - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link MockHttpServletRequest} based on the supplied
|
||||
* {@code ServletContext}.
|
||||
|
||||
Reference in New Issue
Block a user