Skip to content

Commit bba38b8

Browse files
committed
MockHttpServletRequestBuilder allows for specifying content type as String value
Issue: SPR-12405
1 parent 7845950 commit bba38b8

File tree

2 files changed

+62
-37
lines changed

2 files changed

+62
-37
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,22 @@ public MockHttpServletRequestBuilder headers(HttpHeaders httpHeaders) {
186186

187187
/**
188188
* Set the 'Content-Type' header of the request.
189-
* @param mediaType the content type
189+
* @param contentType the content type
190190
*/
191-
public MockHttpServletRequestBuilder contentType(MediaType mediaType) {
192-
Assert.notNull(mediaType, "'contentType' must not be null");
193-
this.contentType = mediaType.toString();
191+
public MockHttpServletRequestBuilder contentType(MediaType contentType) {
192+
Assert.notNull(contentType, "'contentType' must not be null");
193+
this.contentType = contentType.toString();
194+
this.headers.set("Content-Type", this.contentType);
195+
return this;
196+
}
197+
198+
/**
199+
* Set the 'Content-Type' header of the request.
200+
* @param contentType the content type
201+
* @since 4.1.2
202+
*/
203+
public MockHttpServletRequestBuilder contentType(String contentType) {
204+
this.contentType = MediaType.parseMediaType(contentType).toString();
194205
this.headers.set("Content-Type", this.contentType);
195206
return this;
196207
}

spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.test.web.servlet.request;
1717

18+
import java.io.IOException;
1819
import java.security.Principal;
1920
import java.util.Arrays;
2021
import java.util.Collections;
@@ -53,7 +54,7 @@ public class MockHttpServletRequestBuilderTests {
5354

5455

5556
@Before
56-
public void setUp() throws Exception {
57+
public void setUp() {
5758
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/foo/bar");
5859
servletContext = new MockServletContext();
5960
}
@@ -66,7 +67,7 @@ public void method() {
6667
}
6768

6869
@Test
69-
public void uri() throws Exception {
70+
public void uri() {
7071
String uri = "https://java.sun.com:8080/javase/6/docs/api/java/util/BitSet.html?foo=bar#and(java.util.BitSet)";
7172
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, uri);
7273
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
@@ -81,15 +82,15 @@ public void uri() throws Exception {
8182
}
8283

8384
@Test
84-
public void requestUriWithEncoding() throws Exception {
85+
public void requestUriWithEncoding() {
8586
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/foo bar");
8687
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
8788

8889
assertEquals("/foo%20bar", request.getRequestURI());
8990
}
9091

9192
@Test
92-
public void contextPathEmpty() throws Exception {
93+
public void contextPathEmpty() {
9394
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/foo");
9495

9596
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
@@ -100,7 +101,7 @@ public void contextPathEmpty() throws Exception {
100101
}
101102

102103
@Test
103-
public void contextPathServletPathEmpty() throws Exception {
104+
public void contextPathServletPathEmpty() {
104105
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/travel/hotels/42");
105106
this.builder.contextPath("/travel");
106107

@@ -112,7 +113,7 @@ public void contextPathServletPathEmpty() throws Exception {
112113
}
113114

114115
@Test
115-
public void contextPathServletPath() throws Exception {
116+
public void contextPathServletPath() {
116117
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/travel/main/hotels/42");
117118
this.builder.contextPath("/travel");
118119
this.builder.servletPath("/main");
@@ -125,7 +126,7 @@ public void contextPathServletPath() throws Exception {
125126
}
126127

127128
@Test
128-
public void contextPathServletPathInfoEmpty() throws Exception {
129+
public void contextPathServletPathInfoEmpty() {
129130
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/travel/hotels/42");
130131

131132
this.builder.contextPath("/travel");
@@ -139,7 +140,7 @@ public void contextPathServletPathInfoEmpty() throws Exception {
139140
}
140141

141142
@Test
142-
public void contextPathServletPathInfo() throws Exception {
143+
public void contextPathServletPathInfo() {
143144
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/");
144145
this.builder.servletPath("/index.html");
145146
this.builder.pathInfo(null);
@@ -152,7 +153,7 @@ public void contextPathServletPathInfo() throws Exception {
152153
}
153154

154155
@Test
155-
public void contextPathServletPathInvalid() throws Exception {
156+
public void contextPathServletPathInvalid() {
156157

157158
testContextPathServletPathInvalid("/Foo", "", "requestURI [/foo/bar] does not start with contextPath [/Foo]");
158159
testContextPathServletPathInvalid("foo", "", "Context path must start with a '/'");
@@ -175,7 +176,7 @@ private void testContextPathServletPathInvalid(String contextPath, String servle
175176
}
176177

177178
@Test
178-
public void requestUriAndFragment() throws Exception {
179+
public void requestUriAndFragment() {
179180
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/foo#bar");
180181
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
181182

@@ -189,22 +190,22 @@ public void requestParameter() {
189190
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
190191
Map<String, String[]> parameterMap = request.getParameterMap();
191192

192-
assertArrayEquals(new String[]{"bar", "baz"}, parameterMap.get("foo"));
193+
assertArrayEquals(new String[] {"bar", "baz"}, parameterMap.get("foo"));
193194
}
194195

195196
@Test
196-
public void requestParameterFromQuery() throws Exception {
197+
public void requestParameterFromQuery() {
197198
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo=bar&foo=baz");
198199

199200
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
200201
Map<String, String[]> parameterMap = request.getParameterMap();
201202

202-
assertArrayEquals(new String[]{"bar", "baz"}, parameterMap.get("foo"));
203+
assertArrayEquals(new String[] {"bar", "baz"}, parameterMap.get("foo"));
203204
assertEquals("foo=bar&foo=baz", request.getQueryString());
204205
}
205206

206207
@Test
207-
public void requestParameterFromQueryList() throws Exception {
208+
public void requestParameterFromQueryList() {
208209
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo[0]=bar&foo[1]=baz");
209210

210211
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
@@ -215,7 +216,7 @@ public void requestParameterFromQueryList() throws Exception {
215216
}
216217

217218
@Test
218-
public void requestParameterFromQueryWithEncoding() throws Exception {
219+
public void requestParameterFromQueryWithEncoding() {
219220
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo={value}", "bar=baz");
220221

221222
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
@@ -227,18 +228,18 @@ public void requestParameterFromQueryWithEncoding() throws Exception {
227228
// SPR-11043
228229

229230
@Test
230-
public void requestParameterFromQueryNull() throws Exception {
231+
public void requestParameterFromQueryNull() {
231232
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo");
232233

233234
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
234235
Map<String, String[]> parameterMap = request.getParameterMap();
235236

236-
assertArrayEquals(new String[]{null}, parameterMap.get("foo"));
237+
assertArrayEquals(new String[] {null}, parameterMap.get("foo"));
237238
assertEquals("foo", request.getQueryString());
238239
}
239240

240241
@Test
241-
public void acceptHeader() throws Exception {
242+
public void acceptHeader() {
242243
this.builder.accept(MediaType.TEXT_HTML, MediaType.APPLICATION_XML);
243244

244245
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
@@ -251,7 +252,7 @@ public void acceptHeader() throws Exception {
251252
}
252253

253254
@Test
254-
public void contentType() throws Exception {
255+
public void contentType() {
255256
this.builder.contentType(MediaType.TEXT_HTML);
256257

257258
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
@@ -263,10 +264,23 @@ public void contentType() throws Exception {
263264
assertEquals("text/html", contentTypes.get(0));
264265
}
265266

267+
@Test
268+
public void contentTypeViaString() {
269+
this.builder.contentType("text/html");
270+
271+
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
272+
String contentType = request.getContentType();
273+
List<String> contentTypes = Collections.list(request.getHeaders("Content-Type"));
274+
275+
assertEquals("text/html", contentType);
276+
assertEquals(1, contentTypes.size());
277+
assertEquals("text/html", contentTypes.get(0));
278+
}
279+
266280
// SPR-11308
267281

268282
@Test
269-
public void contentTypeViaHeader() throws Exception {
283+
public void contentTypeViaHeader() {
270284
this.builder.header("Content-Type", MediaType.TEXT_HTML_VALUE);
271285
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
272286
String contentType = request.getContentType();
@@ -277,7 +291,7 @@ public void contentTypeViaHeader() throws Exception {
277291
// SPR-11308
278292

279293
@Test
280-
public void contentTypeViaMultipleHeaderValues() throws Exception {
294+
public void contentTypeViaMultipleHeaderValues() {
281295
this.builder.header("Content-Type", MediaType.TEXT_HTML_VALUE, MediaType.ALL_VALUE);
282296
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
283297
String contentType = request.getContentType();
@@ -286,7 +300,7 @@ public void contentTypeViaMultipleHeaderValues() throws Exception {
286300
}
287301

288302
@Test
289-
public void body() throws Exception {
303+
public void body() throws IOException {
290304
byte[] body = "Hello World".getBytes("UTF-8");
291305
this.builder.content(body);
292306

@@ -297,7 +311,7 @@ public void body() throws Exception {
297311
}
298312

299313
@Test
300-
public void header() throws Exception {
314+
public void header() {
301315
this.builder.header("foo", "bar", "baz");
302316

303317
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
@@ -309,7 +323,7 @@ public void header() throws Exception {
309323
}
310324

311325
@Test
312-
public void headers() throws Exception {
326+
public void headers() {
313327
HttpHeaders httpHeaders = new HttpHeaders();
314328
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
315329
httpHeaders.put("foo", Arrays.asList("bar", "baz"));
@@ -325,7 +339,7 @@ public void headers() throws Exception {
325339
}
326340

327341
@Test
328-
public void cookie() throws Exception {
342+
public void cookie() {
329343
Cookie cookie1 = new Cookie("foo", "bar");
330344
Cookie cookie2 = new Cookie("baz", "qux");
331345
this.builder.cookie(cookie1, cookie2);
@@ -341,7 +355,7 @@ public void cookie() throws Exception {
341355
}
342356

343357
@Test
344-
public void locale() throws Exception {
358+
public void locale() {
345359
Locale locale = new Locale("nl", "nl");
346360
this.builder.locale(locale);
347361

@@ -351,7 +365,7 @@ public void locale() throws Exception {
351365
}
352366

353367
@Test
354-
public void characterEncoding() throws Exception {
368+
public void characterEncoding() {
355369
String encoding = "UTF-8";
356370
this.builder.characterEncoding(encoding);
357371

@@ -361,23 +375,23 @@ public void characterEncoding() throws Exception {
361375
}
362376

363377
@Test
364-
public void requestAttribute() throws Exception {
378+
public void requestAttribute() {
365379
this.builder.requestAttr("foo", "bar");
366380
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
367381

368382
assertEquals("bar", request.getAttribute("foo"));
369383
}
370384

371385
@Test
372-
public void sessionAttribute() throws Exception {
386+
public void sessionAttribute() {
373387
this.builder.sessionAttr("foo", "bar");
374388
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
375389

376390
assertEquals("bar", request.getSession().getAttribute("foo"));
377391
}
378392

379393
@Test
380-
public void sessionAttributes() throws Exception {
394+
public void sessionAttributes() {
381395
Map<String, Object> map = new HashMap<String, Object>();
382396
map.put("foo", "bar");
383397
this.builder.sessionAttrs(map);
@@ -388,7 +402,7 @@ public void sessionAttributes() throws Exception {
388402
}
389403

390404
@Test
391-
public void session() throws Exception {
405+
public void session() {
392406
MockHttpSession session = new MockHttpSession(this.servletContext);
393407
session.setAttribute("foo", "bar");
394408
this.builder.session(session);
@@ -402,7 +416,7 @@ public void session() throws Exception {
402416
}
403417

404418
@Test
405-
public void flashAttribute() throws Exception {
419+
public void flashAttribute() {
406420
this.builder.flashAttr("foo", "bar");
407421
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
408422

@@ -412,7 +426,7 @@ public void flashAttribute() throws Exception {
412426
}
413427

414428
@Test
415-
public void principal() throws Exception {
429+
public void principal() {
416430
User user = new User();
417431
this.builder.principal(user);
418432
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);

0 commit comments

Comments
 (0)