From bba38b886221704c82810da7bd2c87ece9c38ef3 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sat, 1 Nov 2014 23:01:26 +0100 Subject: [PATCH] MockHttpServletRequestBuilder allows for specifying content type as String value Issue: SPR-12405 --- .../MockHttpServletRequestBuilder.java | 19 ++++- .../MockHttpServletRequestBuilderTests.java | 80 +++++++++++-------- 2 files changed, 62 insertions(+), 37 deletions(-) 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 46e5f19390..22b8d0800c 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 @@ -186,11 +186,22 @@ public class MockHttpServletRequestBuilder /** * Set the 'Content-Type' header of the request. - * @param mediaType the content type + * @param contentType the content type */ - public MockHttpServletRequestBuilder contentType(MediaType mediaType) { - Assert.notNull(mediaType, "'contentType' must not be null"); - this.contentType = mediaType.toString(); + public MockHttpServletRequestBuilder contentType(MediaType contentType) { + Assert.notNull(contentType, "'contentType' must not be null"); + this.contentType = contentType.toString(); + this.headers.set("Content-Type", this.contentType); + return this; + } + + /** + * Set the 'Content-Type' header of the request. + * @param contentType the content type + * @since 4.1.2 + */ + public MockHttpServletRequestBuilder contentType(String contentType) { + this.contentType = MediaType.parseMediaType(contentType).toString(); this.headers.set("Content-Type", this.contentType); return this; } 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 97ec99b2bf..5bfd542168 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 @@ -15,6 +15,7 @@ */ package org.springframework.test.web.servlet.request; +import java.io.IOException; import java.security.Principal; import java.util.Arrays; import java.util.Collections; @@ -53,7 +54,7 @@ public class MockHttpServletRequestBuilderTests { @Before - public void setUp() throws Exception { + public void setUp() { this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/foo/bar"); servletContext = new MockServletContext(); } @@ -66,7 +67,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void uri() throws Exception { + public void uri() { String uri = "https://java.sun.com:8080/javase/6/docs/api/java/util/BitSet.html?foo=bar#and(java.util.BitSet)"; this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, uri); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); @@ -81,7 +82,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void requestUriWithEncoding() throws Exception { + public void requestUriWithEncoding() { this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/foo bar"); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); @@ -89,7 +90,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void contextPathEmpty() throws Exception { + public void contextPathEmpty() { this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/foo"); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); @@ -100,7 +101,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void contextPathServletPathEmpty() throws Exception { + public void contextPathServletPathEmpty() { this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/travel/hotels/42"); this.builder.contextPath("/travel"); @@ -112,7 +113,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void contextPathServletPath() throws Exception { + public void contextPathServletPath() { this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/travel/main/hotels/42"); this.builder.contextPath("/travel"); this.builder.servletPath("/main"); @@ -125,7 +126,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void contextPathServletPathInfoEmpty() throws Exception { + public void contextPathServletPathInfoEmpty() { this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/travel/hotels/42"); this.builder.contextPath("/travel"); @@ -139,7 +140,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void contextPathServletPathInfo() throws Exception { + public void contextPathServletPathInfo() { this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/"); this.builder.servletPath("/index.html"); this.builder.pathInfo(null); @@ -152,7 +153,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void contextPathServletPathInvalid() throws Exception { + public void contextPathServletPathInvalid() { testContextPathServletPathInvalid("/Foo", "", "requestURI [/foo/bar] does not start with contextPath [/Foo]"); testContextPathServletPathInvalid("foo", "", "Context path must start with a '/'"); @@ -175,7 +176,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void requestUriAndFragment() throws Exception { + public void requestUriAndFragment() { this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/foo#bar"); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); @@ -189,22 +190,22 @@ public class MockHttpServletRequestBuilderTests { MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); Map parameterMap = request.getParameterMap(); - assertArrayEquals(new String[]{"bar", "baz"}, parameterMap.get("foo")); + assertArrayEquals(new String[] {"bar", "baz"}, parameterMap.get("foo")); } @Test - public void requestParameterFromQuery() throws Exception { + public void requestParameterFromQuery() { this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo=bar&foo=baz"); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); Map parameterMap = request.getParameterMap(); - assertArrayEquals(new String[]{"bar", "baz"}, parameterMap.get("foo")); + assertArrayEquals(new String[] {"bar", "baz"}, parameterMap.get("foo")); assertEquals("foo=bar&foo=baz", request.getQueryString()); } @Test - public void requestParameterFromQueryList() throws Exception { + public void requestParameterFromQueryList() { this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo[0]=bar&foo[1]=baz"); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); @@ -215,7 +216,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void requestParameterFromQueryWithEncoding() throws Exception { + public void requestParameterFromQueryWithEncoding() { this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo={value}", "bar=baz"); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); @@ -227,18 +228,18 @@ public class MockHttpServletRequestBuilderTests { // SPR-11043 @Test - public void requestParameterFromQueryNull() throws Exception { + public void requestParameterFromQueryNull() { this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo"); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); Map parameterMap = request.getParameterMap(); - assertArrayEquals(new String[]{null}, parameterMap.get("foo")); + assertArrayEquals(new String[] {null}, parameterMap.get("foo")); assertEquals("foo", request.getQueryString()); } @Test - public void acceptHeader() throws Exception { + public void acceptHeader() { this.builder.accept(MediaType.TEXT_HTML, MediaType.APPLICATION_XML); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); @@ -251,7 +252,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void contentType() throws Exception { + public void contentType() { this.builder.contentType(MediaType.TEXT_HTML); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); @@ -263,10 +264,23 @@ public class MockHttpServletRequestBuilderTests { assertEquals("text/html", contentTypes.get(0)); } + @Test + public void contentTypeViaString() { + this.builder.contentType("text/html"); + + MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); + String contentType = request.getContentType(); + List contentTypes = Collections.list(request.getHeaders("Content-Type")); + + assertEquals("text/html", contentType); + assertEquals(1, contentTypes.size()); + assertEquals("text/html", contentTypes.get(0)); + } + // SPR-11308 @Test - public void contentTypeViaHeader() throws Exception { + public void contentTypeViaHeader() { this.builder.header("Content-Type", MediaType.TEXT_HTML_VALUE); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); String contentType = request.getContentType(); @@ -277,7 +291,7 @@ public class MockHttpServletRequestBuilderTests { // SPR-11308 @Test - public void contentTypeViaMultipleHeaderValues() throws Exception { + public void contentTypeViaMultipleHeaderValues() { this.builder.header("Content-Type", MediaType.TEXT_HTML_VALUE, MediaType.ALL_VALUE); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); String contentType = request.getContentType(); @@ -286,7 +300,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void body() throws Exception { + public void body() throws IOException { byte[] body = "Hello World".getBytes("UTF-8"); this.builder.content(body); @@ -297,7 +311,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void header() throws Exception { + public void header() { this.builder.header("foo", "bar", "baz"); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); @@ -309,7 +323,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void headers() throws Exception { + public void headers() { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_JSON); httpHeaders.put("foo", Arrays.asList("bar", "baz")); @@ -325,7 +339,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void cookie() throws Exception { + public void cookie() { Cookie cookie1 = new Cookie("foo", "bar"); Cookie cookie2 = new Cookie("baz", "qux"); this.builder.cookie(cookie1, cookie2); @@ -341,7 +355,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void locale() throws Exception { + public void locale() { Locale locale = new Locale("nl", "nl"); this.builder.locale(locale); @@ -351,7 +365,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void characterEncoding() throws Exception { + public void characterEncoding() { String encoding = "UTF-8"; this.builder.characterEncoding(encoding); @@ -361,7 +375,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void requestAttribute() throws Exception { + public void requestAttribute() { this.builder.requestAttr("foo", "bar"); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); @@ -369,7 +383,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void sessionAttribute() throws Exception { + public void sessionAttribute() { this.builder.sessionAttr("foo", "bar"); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); @@ -377,7 +391,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void sessionAttributes() throws Exception { + public void sessionAttributes() { Map map = new HashMap(); map.put("foo", "bar"); this.builder.sessionAttrs(map); @@ -388,7 +402,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void session() throws Exception { + public void session() { MockHttpSession session = new MockHttpSession(this.servletContext); session.setAttribute("foo", "bar"); this.builder.session(session); @@ -402,7 +416,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void flashAttribute() throws Exception { + public void flashAttribute() { this.builder.flashAttr("foo", "bar"); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); @@ -412,7 +426,7 @@ public class MockHttpServletRequestBuilderTests { } @Test - public void principal() throws Exception { + public void principal() { User user = new User(); this.builder.principal(user); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);