提交 bd1f5bd9 编写于 作者: S Sam Brannen

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
上级 4d115eef
/*
* 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
......
/*
* 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()
......
......@@ -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
......
......@@ -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))
......
......@@ -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")));
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册