@@ -289,12 +289,14 @@ public class MockHttpServletRequestBuilder
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the 'Content-Type' header of the request.
|
||||
* Set the 'Content-Type' header of the request as a raw String value,
|
||||
* possibly not even well formed (for testing purposes).
|
||||
* @param contentType the content type
|
||||
* @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;
|
||||
}
|
||||
|
||||
@@ -309,16 +311,14 @@ public class MockHttpServletRequestBuilder
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the 'Accept' header to the given media type(s).
|
||||
* @param mediaTypes one or more media types
|
||||
* Set the 'Accept' header using raw String values, possibly not even well
|
||||
* formed (for testing purposes).
|
||||
* @param mediaTypes one or more media types; internally joined as
|
||||
* comma-separated String
|
||||
*/
|
||||
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));
|
||||
this.headers.set("Accept", String.join(", ", mediaTypes));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -713,9 +713,14 @@ public class MockHttpServletRequestBuilder
|
||||
if (this.content != null && this.content.length > 0) {
|
||||
String requestContentType = request.getContentType();
|
||||
if (requestContentType != null) {
|
||||
MediaType mediaType = MediaType.parseMediaType(requestContentType);
|
||||
if (MediaType.APPLICATION_FORM_URLENCODED.includes(mediaType)) {
|
||||
addRequestParams(request, parseFormData(mediaType));
|
||||
try {
|
||||
MediaType mediaType = MediaType.parseMediaType(requestContentType);
|
||||
if (MediaType.APPLICATION_FORM_URLENCODED.includes(mediaType)) {
|
||||
addRequestParams(request, parseFormData(mediaType));
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// Must be invalid, ignore..
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -337,6 +337,13 @@ public class MockHttpServletRequestBuilderTests {
|
||||
assertThat(result.get(1).toString()).isEqualTo("application/xml");
|
||||
}
|
||||
|
||||
@Test // gh-2079
|
||||
public void acceptHeaderWithInvalidValues() {
|
||||
this.builder.accept("any", "any2");
|
||||
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
|
||||
assertThat(request.getHeader("Accept")).isEqualTo("any, any2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contentType() {
|
||||
this.builder.contentType(MediaType.TEXT_HTML);
|
||||
@@ -363,6 +370,13 @@ public class MockHttpServletRequestBuilderTests {
|
||||
assertThat(contentTypes.get(0)).isEqualTo("text/html");
|
||||
}
|
||||
|
||||
@Test // gh-2079
|
||||
public void contentTypeWithInvalidValue() {
|
||||
this.builder.contentType("any");
|
||||
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
|
||||
assertThat(request.getContentType()).isEqualTo("any");
|
||||
}
|
||||
|
||||
@Test // SPR-11308
|
||||
public void contentTypeViaHeader() {
|
||||
this.builder.header("Content-Type", MediaType.TEXT_HTML_VALUE);
|
||||
@@ -372,6 +386,13 @@ public class MockHttpServletRequestBuilderTests {
|
||||
assertThat(contentType).isEqualTo("text/html");
|
||||
}
|
||||
|
||||
@Test // gh-2079
|
||||
public void contentTypeViaHeaderWithInvalidValue() {
|
||||
this.builder.header("Content-Type", "yaml");
|
||||
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
|
||||
assertThat(request.getContentType()).isEqualTo("yaml");
|
||||
}
|
||||
|
||||
@Test // SPR-11308
|
||||
public void contentTypeViaMultipleHeaderValues() {
|
||||
this.builder.header("Content-Type", MediaType.TEXT_HTML_VALUE, MediaType.ALL_VALUE);
|
||||
|
||||
Reference in New Issue
Block a user