Skip to content

Commit

Permalink
Polishing of contribution
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Dec 13, 2019
1 parent f4509d6 commit 542297b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 58 deletions.
Expand Up @@ -289,7 +289,8 @@ 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
*/
Expand All @@ -310,14 +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<String> result = new ArrayList<>(mediaTypes.length);
result.addAll(Arrays.asList(mediaTypes));
this.headers.set("Accept", String.join(", ", result));
this.headers.set("Accept", String.join(", ", mediaTypes));
return this;
}

Expand Down Expand Up @@ -711,10 +712,15 @@ public final MockHttpServletRequest buildRequest(ServletContext servletContext)

if (this.content != null && this.content.length > 0) {
String requestContentType = request.getContentType();
if (requestContentType != null && isValidContentType(requestContentType)) {
MediaType mediaType = MediaType.parseMediaType(requestContentType);
if (MediaType.APPLICATION_FORM_URLENCODED.includes(mediaType)) {
addRequestParams(request, parseFormData(mediaType));
if (requestContentType != null) {
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 All @@ -741,26 +747,6 @@ public final MockHttpServletRequest buildRequest(ServletContext servletContext)
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}.
Expand Down
Expand Up @@ -29,8 +29,6 @@
import java.util.Locale;
import java.util.Map;

import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;

Expand Down Expand Up @@ -339,19 +337,11 @@ public void acceptHeader() {
assertThat(result.get(1).toString()).isEqualTo("application/xml");
}

@Test
public void anyAcceptMultipleContentTypeViaStringArray() {
@Test // gh-2079
public void acceptHeaderWithInvalidValues() {
this.builder.accept("any", "any2");

MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
List<String> accept = Collections.list(request.getHeaders("Accept"));
List<String> result = Stream.of(accept.get(0).split(","))
.map(String::trim)
.collect(Collectors.toList());

assertThat(result.get(0)).isEqualTo("any");
assertThat(result).hasSize(2);
assertThat(result.get(1)).isEqualTo("any2");
assertThat(request.getHeader("Accept")).isEqualTo("any, any2");
}

@Test
Expand Down Expand Up @@ -380,17 +370,11 @@ public void contentTypeViaString() {
assertThat(contentTypes.get(0)).isEqualTo("text/html");
}

@Test
public void anyContentTypeViaString() {
@Test // gh-2079
public void contentTypeWithInvalidValue() {
this.builder.contentType("any");

MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
String contentType = request.getContentType();
List<String> contentTypes = Collections.list(request.getHeaders("Content-Type"));

assertThat(contentType).isEqualTo("any");
assertThat(contentTypes).hasSize(1);
assertThat(contentTypes.get(0)).isEqualTo("any");
assertThat(request.getContentType()).isEqualTo("any");
}

@Test // SPR-11308
Expand All @@ -402,14 +386,11 @@ public void contentTypeViaHeader() {
assertThat(contentType).isEqualTo("text/html");
}

@Test // SPR-17643
public void invalidContentTypeViaHeader() {
@Test // gh-2079
public void contentTypeViaHeaderWithInvalidValue() {
this.builder.header("Content-Type", "yaml");
this.builder.content("some content");
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
String contentType = request.getContentType();

assertThat(contentType).isEqualTo("yaml");
assertThat(request.getContentType()).isEqualTo("yaml");
}

@Test // SPR-11308
Expand Down

0 comments on commit 542297b

Please sign in to comment.