Skip to content

Commit 2e4963f

Browse files
committed
Revert "Always specify charset for form data requests"
This reverts commit 1897d8e. Issue: SPR-16613
1 parent 4d0adc7 commit 2e4963f

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,6 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
9393

9494
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
9595

96-
private static final MediaType DEFAULT_FORM_DATA_MEDIA_TYPE =
97-
new MediaType(MediaType.APPLICATION_FORM_URLENCODED, DEFAULT_CHARSET);
98-
9996

10097
private List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
10198

@@ -282,14 +279,15 @@ private boolean isMultipart(MultiValueMap<String, ?> map, MediaType contentType)
282279
private void writeForm(MultiValueMap<String, String> form, MediaType contentType,
283280
HttpOutputMessage outputMessage) throws IOException {
284281

285-
contentType = (contentType != null ? contentType : DEFAULT_FORM_DATA_MEDIA_TYPE);
286-
Charset charset = contentType.getCharset();
287-
if (charset == null) {
282+
Charset charset;
283+
if (contentType != null) {
284+
outputMessage.getHeaders().setContentType(contentType);
285+
charset = (contentType.getCharset() != null ? contentType.getCharset() : this.charset);
286+
}
287+
else {
288+
outputMessage.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED);
288289
charset = this.charset;
289-
contentType = new MediaType(contentType, charset);
290290
}
291-
outputMessage.getHeaders().setContentType(contentType);
292-
293291
StringBuilder builder = new StringBuilder();
294292
for (Iterator<String> nameIterator = form.keySet().iterator(); nameIterator.hasNext();) {
295293
String name = nameIterator.next();

spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import java.io.StringReader;
2323
import java.nio.charset.Charset;
2424
import java.util.List;
25+
2526
import javax.xml.transform.Source;
2627
import javax.xml.transform.stream.StreamSource;
2728

@@ -30,6 +31,7 @@
3031
import org.apache.commons.fileupload.FileUpload;
3132
import org.apache.commons.fileupload.RequestContext;
3233
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
34+
3335
import org.junit.Before;
3436
import org.junit.Test;
3537

@@ -45,10 +47,14 @@
4547
import org.springframework.util.MultiValueMap;
4648

4749
import static org.hamcrest.CoreMatchers.*;
48-
import static org.hamcrest.CoreMatchers.endsWith;
49-
import static org.hamcrest.CoreMatchers.startsWith;
50-
import static org.junit.Assert.*;
51-
import static org.mockito.BDDMockito.*;
50+
import static org.junit.Assert.assertEquals;
51+
import static org.junit.Assert.assertFalse;
52+
import static org.junit.Assert.assertNotNull;
53+
import static org.junit.Assert.assertNull;
54+
import static org.junit.Assert.assertThat;
55+
import static org.junit.Assert.assertTrue;
56+
import static org.mockito.BDDMockito.never;
57+
import static org.mockito.BDDMockito.verify;
5258

5359
/**
5460
* @author Arjen Poutsma
@@ -116,8 +122,8 @@ public void writeForm() throws IOException {
116122

117123
assertEquals("Invalid result", "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3",
118124
outputMessage.getBodyAsString(UTF_8));
119-
assertEquals("Invalid content-type", "application/x-www-form-urlencoded;charset=UTF-8",
120-
outputMessage.getHeaders().getContentType().toString());
125+
assertEquals("Invalid content-type", new MediaType("application", "x-www-form-urlencoded"),
126+
outputMessage.getHeaders().getContentType());
121127
assertEquals("Invalid content-length", outputMessage.getBodyAsBytes().length,
122128
outputMessage.getHeaders().getContentLength());
123129
}

spring-web/src/test/java/org/springframework/web/client/AbstractMockWebServerTestCase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -168,7 +168,7 @@ private void assertFilePart(Buffer buffer, String disposition, String boundary,
168168
}
169169

170170
private MockResponse formRequest(RecordedRequest request) {
171-
assertEquals("application/x-www-form-urlencoded;charset=UTF-8", request.getHeader("Content-Type"));
171+
assertEquals("application/x-www-form-urlencoded", request.getHeader("Content-Type"));
172172
String body = request.getBody().readUtf8();
173173
assertThat(body, Matchers.containsString("name+1=value+1"));
174174
assertThat(body, Matchers.containsString("name+2=value+2%2B1"));

0 commit comments

Comments
 (0)