Skip to content

Commit 1897d8e

Browse files
committed
Always specify charset for form data requests
Issue: SPR-16613
1 parent 71126fa commit 1897d8e

File tree

3 files changed

+18
-22
lines changed

3 files changed

+18
-22
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ 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+
9699

97100
private List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
98101

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

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);
285+
contentType = (contentType != null ? contentType : DEFAULT_FORM_DATA_MEDIA_TYPE);
286+
Charset charset = contentType.getCharset();
287+
if (charset == null) {
289288
charset = this.charset;
289+
contentType = new MediaType(contentType, charset);
290290
}
291+
outputMessage.getHeaders().setContentType(contentType);
292+
291293
StringBuilder builder = new StringBuilder();
292294
for (Iterator<String> nameIterator = form.keySet().iterator(); nameIterator.hasNext();) {
293295
String name = nameIterator.next();

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2018 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,7 +22,6 @@
2222
import java.io.StringReader;
2323
import java.nio.charset.Charset;
2424
import java.util.List;
25-
2625
import javax.xml.transform.Source;
2726
import javax.xml.transform.stream.StreamSource;
2827

@@ -31,7 +30,6 @@
3130
import org.apache.commons.fileupload.FileUpload;
3231
import org.apache.commons.fileupload.RequestContext;
3332
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
34-
3533
import org.junit.Before;
3634
import org.junit.Test;
3735

@@ -47,14 +45,10 @@
4745
import org.springframework.util.MultiValueMap;
4846

4947
import static org.hamcrest.CoreMatchers.*;
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;
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.*;
5852

5953
/**
6054
* @author Arjen Poutsma
@@ -122,8 +116,8 @@ public void writeForm() throws IOException {
122116

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

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-2017 the original author or authors.
2+
* Copyright 2002-2018 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", request.getHeader("Content-Type"));
171+
assertEquals("application/x-www-form-urlencoded;charset=UTF-8", 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)