Skip to content

Commit

Permalink
Merge pull request #2079
Browse files Browse the repository at this point in the history
Closes gh-2079
  • Loading branch information
rstoyanchev committed Dec 13, 2019
2 parents f8d6896 + 542297b commit 5f91780
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
Expand Up @@ -289,12 +289,14 @@ public MockHttpServletRequestBuilder contentType(MediaType contentType) {
}

/**
* 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;
}

Expand All @@ -309,16 +311,14 @@ public MockHttpServletRequestBuilder accept(MediaType... mediaTypes) {
}

/**
* 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;
}

Expand Down Expand Up @@ -713,9 +713,14 @@ public final MockHttpServletRequest buildRequest(ServletContext servletContext)
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..
}
}
}
Expand Down
Expand Up @@ -337,6 +337,13 @@ public void acceptHeader() {
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);
Expand All @@ -363,6 +370,13 @@ public void contentTypeViaString() {
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);
Expand All @@ -372,6 +386,13 @@ public void contentTypeViaHeader() {
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);
Expand Down

0 comments on commit 5f91780

Please sign in to comment.