From 2e4963fca3647be299fa75383548e23e2448a2ce Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 28 Mar 2018 11:57:29 -0400 Subject: [PATCH] Revert "Always specify charset for form data requests" This reverts commit 1897d8e85843174e18d164b2eebb08c14b988999. Issue: SPR-16613 --- .../converter/FormHttpMessageConverter.java | 16 +++++++-------- .../FormHttpMessageConverterTests.java | 20 ++++++++++++------- .../client/AbstractMockWebServerTestCase.java | 4 ++-- 3 files changed, 22 insertions(+), 18 deletions(-) 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 94422731492..d5d595dd9c8 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,9 +93,6 @@ public class FormHttpMessageConverter implements HttpMessageConverter supportedMediaTypes = new ArrayList(); @@ -282,14 +279,15 @@ public class FormHttpMessageConverter implements HttpMessageConverter form, MediaType contentType, HttpOutputMessage outputMessage) throws IOException { - contentType = (contentType != null ? contentType : DEFAULT_FORM_DATA_MEDIA_TYPE); - Charset charset = contentType.getCharset(); - if (charset == null) { + 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); 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 a7115a48810..aaa03b596d0 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-2018 the original author or authors. + * Copyright 2002-2015 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,6 +22,7 @@ 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; @@ -30,6 +31,7 @@ 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; @@ -45,10 +47,14 @@ import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import static org.hamcrest.CoreMatchers.*; -import static org.hamcrest.CoreMatchers.endsWith; -import static org.hamcrest.CoreMatchers.startsWith; -import static org.junit.Assert.*; -import static org.mockito.BDDMockito.*; +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; /** * @author Arjen Poutsma @@ -116,8 +122,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", "application/x-www-form-urlencoded;charset=UTF-8", - outputMessage.getHeaders().getContentType().toString()); + assertEquals("Invalid content-type", new MediaType("application", "x-www-form-urlencoded"), + outputMessage.getHeaders().getContentType()); 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 eb72354c637..c467dd6f77b 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-2018 the original author or authors. + * Copyright 2002-2017 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;charset=UTF-8", request.getHeader("Content-Type")); + assertEquals("application/x-www-form-urlencoded", 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"));