Browse Source

Revert "Always specify charset for form data requests"

This reverts commit 1897d8e858.

Issue: SPR-16613
pull/1775/head
Rossen Stoyanchev 8 years ago
parent
commit
2e4963fca3
  1. 16
      spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java
  2. 20
      spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java
  3. 4
      spring-web/src/test/java/org/springframework/web/client/AbstractMockWebServerTestCase.java

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

@ -93,9 +93,6 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private static final MediaType DEFAULT_FORM_DATA_MEDIA_TYPE =
new MediaType(MediaType.APPLICATION_FORM_URLENCODED, DEFAULT_CHARSET);
private List<MediaType> supportedMediaTypes = new ArrayList<MediaType>(); private List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
@ -282,14 +279,15 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
private void writeForm(MultiValueMap<String, String> form, MediaType contentType, private void writeForm(MultiValueMap<String, String> form, MediaType contentType,
HttpOutputMessage outputMessage) throws IOException { HttpOutputMessage outputMessage) throws IOException {
contentType = (contentType != null ? contentType : DEFAULT_FORM_DATA_MEDIA_TYPE); Charset charset;
Charset charset = contentType.getCharset(); if (contentType != null) {
if (charset == null) { outputMessage.getHeaders().setContentType(contentType);
charset = (contentType.getCharset() != null ? contentType.getCharset() : this.charset);
}
else {
outputMessage.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED);
charset = this.charset; charset = this.charset;
contentType = new MediaType(contentType, charset);
} }
outputMessage.getHeaders().setContentType(contentType);
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
for (Iterator<String> nameIterator = form.keySet().iterator(); nameIterator.hasNext();) { for (Iterator<String> nameIterator = form.keySet().iterator(); nameIterator.hasNext();) {
String name = nameIterator.next(); String name = nameIterator.next();

20
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.io.StringReader;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.List; import java.util.List;
import javax.xml.transform.Source; import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource; 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.FileUpload;
import org.apache.commons.fileupload.RequestContext; import org.apache.commons.fileupload.RequestContext;
import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -45,10 +47,14 @@ import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.CoreMatchers.endsWith; import static org.junit.Assert.assertEquals;
import static org.hamcrest.CoreMatchers.startsWith; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.*; import static org.junit.Assert.assertNotNull;
import static org.mockito.BDDMockito.*; 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 * @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", assertEquals("Invalid result", "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3",
outputMessage.getBodyAsString(UTF_8)); outputMessage.getBodyAsString(UTF_8));
assertEquals("Invalid content-type", "application/x-www-form-urlencoded;charset=UTF-8", assertEquals("Invalid content-type", new MediaType("application", "x-www-form-urlencoded"),
outputMessage.getHeaders().getContentType().toString()); outputMessage.getHeaders().getContentType());
assertEquals("Invalid content-length", outputMessage.getBodyAsBytes().length, assertEquals("Invalid content-length", outputMessage.getBodyAsBytes().length,
outputMessage.getHeaders().getContentLength()); outputMessage.getHeaders().getContentLength());
} }

4
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) { 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(); String body = request.getBody().readUtf8();
assertThat(body, Matchers.containsString("name+1=value+1")); assertThat(body, Matchers.containsString("name+1=value+1"));
assertThat(body, Matchers.containsString("name+2=value+2%2B1")); assertThat(body, Matchers.containsString("name+2=value+2%2B1"));

Loading…
Cancel
Save