From 8b35c3ff74347f32194149ec91773c4eb2eb21ea Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 14 Jan 2014 12:11:12 -0500 Subject: [PATCH] Recognize Content-Type as special header in MHSRB When adding headers generically, MockHttpServletRequestBuilder now recognizes Content-Type and updates the contentType field accordingly. Issue: SPR-11308 --- .../MockHttpServletRequestBuilder.java | 10 +++++++- .../MockHttpServletRequestBuilderTests.java | 25 ++++++++++++++++++- 2 files changed, 33 insertions(+), 2 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 46ba948461..025421f874 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -141,6 +141,10 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable * @param values one or more header values */ public MockHttpServletRequestBuilder header(String name, Object... values) { + if ("Content-Type".equalsIgnoreCase(name)) { + List mediaTypes = MediaType.parseMediaTypes(StringUtils.arrayToCommaDelimitedString(values)); + this.contentType = MediaType.toString(mediaTypes); + } addToMultiValueMap(this.headers, name, values); return this; } @@ -150,6 +154,10 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable * @param httpHeaders the headers and values to add */ public MockHttpServletRequestBuilder headers(HttpHeaders httpHeaders) { + MediaType mediaType = httpHeaders.getContentType(); + if (mediaType != null) { + this.contentType = mediaType.toString(); + } for (String name : httpHeaders.keySet()) { Object[] values = ObjectUtils.toObjectArray(httpHeaders.get(name).toArray()); addToMultiValueMap(this.headers, name, values); 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 e33e92260b..c6a335858c 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import javax.print.attribute.standard.Media; import javax.servlet.ServletContext; import javax.servlet.http.Cookie; @@ -267,6 +268,28 @@ public class MockHttpServletRequestBuilderTests { assertEquals("text/html", contentTypes.get(0)); } + // SPR-11308 + + @Test + public void contentTypeViaHeader() throws Exception { + this.builder.header("Content-Type", MediaType.TEXT_HTML_VALUE); + MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); + String contentType = request.getContentType(); + + assertEquals("text/html", contentType); + } + + // SPR-11308 + + @Test + public void contentTypeViaMultipleHeaderValues() throws Exception { + this.builder.header("Content-Type", MediaType.TEXT_HTML_VALUE, MediaType.ALL_VALUE); + MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); + String contentType = request.getContentType(); + + assertEquals("text/html, */*", contentType); + } + @Test public void body() throws Exception { byte[] body = "Hello World".getBytes("UTF-8");