Always specify charset for form data requests

Issue: SPR-16613
This commit is contained in:
Rossen Stoyanchev
2018-03-27 19:54:48 -04:00
parent e00384a6fd
commit 5861e9685b
5 changed files with 33 additions and 23 deletions

View File

@@ -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.
@@ -22,6 +22,7 @@ import org.junit.Test;
import reactor.core.publisher.Mono;
import org.springframework.core.ResolvableType;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
import org.springframework.util.LinkedMultiValueMap;
@@ -79,8 +80,9 @@ public class FormHttpMessageWriterTests {
String responseBody = response.getBodyAsString().block();
assertEquals("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3", responseBody);
assertEquals(MediaType.APPLICATION_FORM_URLENCODED, response.getHeaders().getContentType());
assertEquals(responseBody.getBytes().length, response.getHeaders().getContentLength());
HttpHeaders headers = response.getHeaders();
assertEquals("application/x-www-form-urlencoded;charset=UTF-8", headers.getContentType().toString());
assertEquals(responseBody.getBytes().length, headers.getContentLength());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 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.
@@ -112,8 +112,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(StandardCharsets.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());
}

View File

@@ -165,7 +165,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"));