Browse Source

Support Charset for character encoding in MockMvc

To improve the developer experience and avoid the use of String
literals, this commit provides overloaded support via Charset for
character encoding in MockHttpServletRequestBuilder and
ContentResultMatchers.

Closes gh-27231
pull/27234/head
Sam Brannen 3 years ago
parent
commit
bd1f5bd9fc
  1. 14
      spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java
  2. 14
      spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java
  3. 7
      spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java
  4. 4
      spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ResponseBodyTests.java
  5. 10
      spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ContentAssertionTests.java

14
spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
@ -20,6 +20,7 @@ import java.io.ByteArrayInputStream; @@ -20,6 +20,7 @@ import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.Principal;
import java.util.ArrayList;
@ -243,6 +244,17 @@ public class MockHttpServletRequestBuilder @@ -243,6 +244,17 @@ public class MockHttpServletRequestBuilder
return this;
}
/**
* Set the character encoding of the request.
* @param encoding the character encoding
* @since 5.3.10
* @see StandardCharsets
* @see #characterEncoding(String)
*/
public MockHttpServletRequestBuilder characterEncoding(Charset encoding) {
return this.characterEncoding(encoding.name());
}
/**
* Set the character encoding of the request.
* @param encoding the character encoding

14
spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.test.web.servlet.result;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;
@ -43,6 +44,7 @@ import static org.springframework.test.util.AssertionErrors.assertTrue; @@ -43,6 +44,7 @@ import static org.springframework.test.util.AssertionErrors.assertTrue;
* {@link MockMvcResultMatchers#content}.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 3.2
*/
public class ContentResultMatchers {
@ -107,6 +109,16 @@ public class ContentResultMatchers { @@ -107,6 +109,16 @@ public class ContentResultMatchers {
};
}
/**
* Assert the character encoding in the ServletResponse.
* @since 5.3.10
* @see StandardCharsets
* @see #encoding(String)
*/
public ResultMatcher encoding(Charset characterEncoding) {
return encoding(characterEncoding.name());
}
/**
* Assert the character encoding in the ServletResponse.
* @see HttpServletResponse#getCharacterEncoding()

7
spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java

@ -477,11 +477,14 @@ class MockHttpServletRequestBuilderTests { @@ -477,11 +477,14 @@ class MockHttpServletRequestBuilderTests {
@Test
void characterEncoding() {
String encoding = "UTF-8";
this.builder.characterEncoding(encoding);
this.builder.characterEncoding(encoding);
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
assertThat(request.getCharacterEncoding()).isEqualTo(encoding);
this.builder.characterEncoding(StandardCharsets.ISO_8859_1);
request = this.builder.buildRequest(this.servletContext);
assertThat(request.getCharacterEncoding()).isEqualTo(StandardCharsets.ISO_8859_1.name());
}
@Test

4
spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ResponseBodyTests.java

@ -44,10 +44,10 @@ class ResponseBodyTests { @@ -44,10 +44,10 @@ class ResponseBodyTests {
void json() throws Exception {
standaloneSetup(new PersonController()).defaultResponseCharacterEncoding(UTF_8).build()
// We use a name containing an umlaut to test UTF-8 encoding for the request and the response.
.perform(get("/person/Jürgen").characterEncoding(UTF_8.name()).accept(MediaType.APPLICATION_JSON))
.perform(get("/person/Jürgen").characterEncoding(UTF_8).accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"))
.andExpect(content().encoding(UTF_8.name()))
.andExpect(content().encoding(UTF_8))
.andExpect(content().string(containsString("Jürgen")))
.andExpect(jsonPath("$.name").value("Jürgen"))
.andExpect(jsonPath("$.age").value(42))

10
spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ContentAssertionTests.java

@ -16,6 +16,8 @@ @@ -16,6 +16,8 @@
package org.springframework.test.web.servlet.samples.standalone.resultmatchers;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
@ -95,9 +97,17 @@ public class ContentAssertionTests { @@ -95,9 +97,17 @@ public class ContentAssertionTests {
.andExpect(content().encoding("ISO-8859-1"))
.andExpect(content().string(containsString("world")));
this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
.andExpect(content().encoding(StandardCharsets.ISO_8859_1))
.andExpect(content().string(containsString("world")));
this.mockMvc.perform(get("/handleUtf8"))
.andExpect(content().encoding("UTF-8"))
.andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes("UTF-8")));
this.mockMvc.perform(get("/handleUtf8"))
.andExpect(content().encoding(StandardCharsets.UTF_8))
.andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes("UTF-8")));
}

Loading…
Cancel
Save