diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java index c5b527b4adab..ef178c394bb6 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java @@ -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; } @@ -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 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 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.. } } } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java index ae111d7877b6..17fb123acec3 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java @@ -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); @@ -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); @@ -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);