diff --git a/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java index d5d595dd9c..9442273149 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java @@ -93,6 +93,9 @@ public class FormHttpMessageConverter implements HttpMessageConverter supportedMediaTypes = new ArrayList(); @@ -279,15 +282,14 @@ public class FormHttpMessageConverter implements HttpMessageConverter form, MediaType contentType, HttpOutputMessage outputMessage) throws IOException { - Charset charset; - if (contentType != null) { - outputMessage.getHeaders().setContentType(contentType); - charset = (contentType.getCharset() != null ? contentType.getCharset() : this.charset); - } - else { - outputMessage.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED); + contentType = (contentType != null ? contentType : DEFAULT_FORM_DATA_MEDIA_TYPE); + Charset charset = contentType.getCharset(); + if (charset == null) { charset = this.charset; + contentType = new MediaType(contentType, charset); } + outputMessage.getHeaders().setContentType(contentType); + StringBuilder builder = new StringBuilder(); for (Iterator nameIterator = form.keySet().iterator(); nameIterator.hasNext();) { String name = nameIterator.next(); diff --git a/spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java index aaa03b596d..a7115a4881 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2018 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. @@ -22,7 +22,6 @@ import java.io.InputStream; import java.io.StringReader; import java.nio.charset.Charset; import java.util.List; - import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; @@ -31,7 +30,6 @@ import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.FileUpload; import org.apache.commons.fileupload.RequestContext; import org.apache.commons.fileupload.disk.DiskFileItemFactory; - import org.junit.Before; import org.junit.Test; @@ -47,14 +45,10 @@ import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.mockito.BDDMockito.never; -import static org.mockito.BDDMockito.verify; +import static org.hamcrest.CoreMatchers.endsWith; +import static org.hamcrest.CoreMatchers.startsWith; +import static org.junit.Assert.*; +import static org.mockito.BDDMockito.*; /** * @author Arjen Poutsma @@ -122,8 +116,8 @@ public class FormHttpMessageConverterTests { assertEquals("Invalid result", "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3", outputMessage.getBodyAsString(UTF_8)); - assertEquals("Invalid content-type", new MediaType("application", "x-www-form-urlencoded"), - outputMessage.getHeaders().getContentType()); + assertEquals("Invalid content-type", "application/x-www-form-urlencoded;charset=UTF-8", + outputMessage.getHeaders().getContentType().toString()); assertEquals("Invalid content-length", outputMessage.getBodyAsBytes().length, outputMessage.getHeaders().getContentLength()); } diff --git a/spring-web/src/test/java/org/springframework/web/client/AbstractMockWebServerTestCase.java b/spring-web/src/test/java/org/springframework/web/client/AbstractMockWebServerTestCase.java index c467dd6f77..eb72354c63 100644 --- a/spring-web/src/test/java/org/springframework/web/client/AbstractMockWebServerTestCase.java +++ b/spring-web/src/test/java/org/springframework/web/client/AbstractMockWebServerTestCase.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -168,7 +168,7 @@ public class AbstractMockWebServerTestCase { } private MockResponse formRequest(RecordedRequest request) { - assertEquals("application/x-www-form-urlencoded", request.getHeader("Content-Type")); + assertEquals("application/x-www-form-urlencoded;charset=UTF-8", request.getHeader("Content-Type")); String body = request.getBody().readUtf8(); assertThat(body, Matchers.containsString("name+1=value+1")); assertThat(body, Matchers.containsString("name+2=value+2%2B1"));