diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java index 7a73e731f5..c4ec1cd917 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java @@ -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; 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 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 diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java index 87232ae45b..f2d9360107 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java @@ -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 @@ 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; * {@link MockMvcResultMatchers#content}. * * @author Rossen Stoyanchev + * @author Sam Brannen * @since 3.2 */ 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() diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java index 734e24c26f..b493cbbca4 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java @@ -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 diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ResponseBodyTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ResponseBodyTests.java index e6cbe17338..26b90936df 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ResponseBodyTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ResponseBodyTests.java @@ -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)) diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ContentAssertionTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ContentAssertionTests.java index 3b2c8347fa..2efc51e4af 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ContentAssertionTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ContentAssertionTests.java @@ -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 { .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"))); }