MockHttpServletRequestBuilder allows for specifying content type as String value

Issue: SPR-12405
This commit is contained in:
Juergen Hoeller 2014-11-01 23:01:26 +01:00
parent 78459504e0
commit bba38b8862
2 changed files with 62 additions and 37 deletions

View File

@ -186,11 +186,22 @@ public class MockHttpServletRequestBuilder
/** /**
* Set the 'Content-Type' header of the request. * Set the 'Content-Type' header of the request.
* @param mediaType the content type * @param contentType the content type
*/ */
public MockHttpServletRequestBuilder contentType(MediaType mediaType) { public MockHttpServletRequestBuilder contentType(MediaType contentType) {
Assert.notNull(mediaType, "'contentType' must not be null"); Assert.notNull(contentType, "'contentType' must not be null");
this.contentType = mediaType.toString(); 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); this.headers.set("Content-Type", this.contentType);
return this; return this;
} }

View File

@ -15,6 +15,7 @@
*/ */
package org.springframework.test.web.servlet.request; package org.springframework.test.web.servlet.request;
import java.io.IOException;
import java.security.Principal; import java.security.Principal;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -53,7 +54,7 @@ public class MockHttpServletRequestBuilderTests {
@Before @Before
public void setUp() throws Exception { public void setUp() {
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/foo/bar"); this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/foo/bar");
servletContext = new MockServletContext(); servletContext = new MockServletContext();
} }
@ -66,7 +67,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @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)"; 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); this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, uri);
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
@ -81,7 +82,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void requestUriWithEncoding() throws Exception { public void requestUriWithEncoding() {
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/foo bar"); this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/foo bar");
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
@ -89,7 +90,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void contextPathEmpty() throws Exception { public void contextPathEmpty() {
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/foo"); this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/foo");
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
@ -100,7 +101,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void contextPathServletPathEmpty() throws Exception { public void contextPathServletPathEmpty() {
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/travel/hotels/42"); this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/travel/hotels/42");
this.builder.contextPath("/travel"); this.builder.contextPath("/travel");
@ -112,7 +113,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void contextPathServletPath() throws Exception { public void contextPathServletPath() {
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/travel/main/hotels/42"); this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/travel/main/hotels/42");
this.builder.contextPath("/travel"); this.builder.contextPath("/travel");
this.builder.servletPath("/main"); this.builder.servletPath("/main");
@ -125,7 +126,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void contextPathServletPathInfoEmpty() throws Exception { public void contextPathServletPathInfoEmpty() {
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/travel/hotels/42"); this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/travel/hotels/42");
this.builder.contextPath("/travel"); this.builder.contextPath("/travel");
@ -139,7 +140,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void contextPathServletPathInfo() throws Exception { public void contextPathServletPathInfo() {
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/"); this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/");
this.builder.servletPath("/index.html"); this.builder.servletPath("/index.html");
this.builder.pathInfo(null); this.builder.pathInfo(null);
@ -152,7 +153,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void contextPathServletPathInvalid() throws Exception { public void contextPathServletPathInvalid() {
testContextPathServletPathInvalid("/Foo", "", "requestURI [/foo/bar] does not start with contextPath [/Foo]"); testContextPathServletPathInvalid("/Foo", "", "requestURI [/foo/bar] does not start with contextPath [/Foo]");
testContextPathServletPathInvalid("foo", "", "Context path must start with a '/'"); testContextPathServletPathInvalid("foo", "", "Context path must start with a '/'");
@ -175,7 +176,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void requestUriAndFragment() throws Exception { public void requestUriAndFragment() {
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/foo#bar"); this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/foo#bar");
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
@ -193,7 +194,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void requestParameterFromQuery() throws Exception { public void requestParameterFromQuery() {
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo=bar&foo=baz"); this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo=bar&foo=baz");
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
@ -204,7 +205,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void requestParameterFromQueryList() throws Exception { public void requestParameterFromQueryList() {
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo[0]=bar&foo[1]=baz"); this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo[0]=bar&foo[1]=baz");
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
@ -215,7 +216,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void requestParameterFromQueryWithEncoding() throws Exception { public void requestParameterFromQueryWithEncoding() {
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo={value}", "bar=baz"); this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo={value}", "bar=baz");
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
@ -227,7 +228,7 @@ public class MockHttpServletRequestBuilderTests {
// SPR-11043 // SPR-11043
@Test @Test
public void requestParameterFromQueryNull() throws Exception { public void requestParameterFromQueryNull() {
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo"); this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo");
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
@ -238,7 +239,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void acceptHeader() throws Exception { public void acceptHeader() {
this.builder.accept(MediaType.TEXT_HTML, MediaType.APPLICATION_XML); this.builder.accept(MediaType.TEXT_HTML, MediaType.APPLICATION_XML);
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
@ -251,7 +252,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void contentType() throws Exception { public void contentType() {
this.builder.contentType(MediaType.TEXT_HTML); this.builder.contentType(MediaType.TEXT_HTML);
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
@ -263,10 +264,23 @@ public class MockHttpServletRequestBuilderTests {
assertEquals("text/html", contentTypes.get(0)); 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<String> contentTypes = Collections.list(request.getHeaders("Content-Type"));
assertEquals("text/html", contentType);
assertEquals(1, contentTypes.size());
assertEquals("text/html", contentTypes.get(0));
}
// SPR-11308 // SPR-11308
@Test @Test
public void contentTypeViaHeader() throws Exception { public void contentTypeViaHeader() {
this.builder.header("Content-Type", MediaType.TEXT_HTML_VALUE); this.builder.header("Content-Type", MediaType.TEXT_HTML_VALUE);
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
String contentType = request.getContentType(); String contentType = request.getContentType();
@ -277,7 +291,7 @@ public class MockHttpServletRequestBuilderTests {
// SPR-11308 // SPR-11308
@Test @Test
public void contentTypeViaMultipleHeaderValues() throws Exception { public void contentTypeViaMultipleHeaderValues() {
this.builder.header("Content-Type", MediaType.TEXT_HTML_VALUE, MediaType.ALL_VALUE); this.builder.header("Content-Type", MediaType.TEXT_HTML_VALUE, MediaType.ALL_VALUE);
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
String contentType = request.getContentType(); String contentType = request.getContentType();
@ -286,7 +300,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void body() throws Exception { public void body() throws IOException {
byte[] body = "Hello World".getBytes("UTF-8"); byte[] body = "Hello World".getBytes("UTF-8");
this.builder.content(body); this.builder.content(body);
@ -297,7 +311,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void header() throws Exception { public void header() {
this.builder.header("foo", "bar", "baz"); this.builder.header("foo", "bar", "baz");
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
@ -309,7 +323,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void headers() throws Exception { public void headers() {
HttpHeaders httpHeaders = new HttpHeaders(); HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON); httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.put("foo", Arrays.asList("bar", "baz")); httpHeaders.put("foo", Arrays.asList("bar", "baz"));
@ -325,7 +339,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void cookie() throws Exception { public void cookie() {
Cookie cookie1 = new Cookie("foo", "bar"); Cookie cookie1 = new Cookie("foo", "bar");
Cookie cookie2 = new Cookie("baz", "qux"); Cookie cookie2 = new Cookie("baz", "qux");
this.builder.cookie(cookie1, cookie2); this.builder.cookie(cookie1, cookie2);
@ -341,7 +355,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void locale() throws Exception { public void locale() {
Locale locale = new Locale("nl", "nl"); Locale locale = new Locale("nl", "nl");
this.builder.locale(locale); this.builder.locale(locale);
@ -351,7 +365,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void characterEncoding() throws Exception { public void characterEncoding() {
String encoding = "UTF-8"; String encoding = "UTF-8";
this.builder.characterEncoding(encoding); this.builder.characterEncoding(encoding);
@ -361,7 +375,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void requestAttribute() throws Exception { public void requestAttribute() {
this.builder.requestAttr("foo", "bar"); this.builder.requestAttr("foo", "bar");
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
@ -369,7 +383,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void sessionAttribute() throws Exception { public void sessionAttribute() {
this.builder.sessionAttr("foo", "bar"); this.builder.sessionAttr("foo", "bar");
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
@ -377,7 +391,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void sessionAttributes() throws Exception { public void sessionAttributes() {
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
map.put("foo", "bar"); map.put("foo", "bar");
this.builder.sessionAttrs(map); this.builder.sessionAttrs(map);
@ -388,7 +402,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void session() throws Exception { public void session() {
MockHttpSession session = new MockHttpSession(this.servletContext); MockHttpSession session = new MockHttpSession(this.servletContext);
session.setAttribute("foo", "bar"); session.setAttribute("foo", "bar");
this.builder.session(session); this.builder.session(session);
@ -402,7 +416,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void flashAttribute() throws Exception { public void flashAttribute() {
this.builder.flashAttr("foo", "bar"); this.builder.flashAttr("foo", "bar");
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
@ -412,7 +426,7 @@ public class MockHttpServletRequestBuilderTests {
} }
@Test @Test
public void principal() throws Exception { public void principal() {
User user = new User(); User user = new User();
this.builder.principal(user); this.builder.principal(user);
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);