-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support default character encoding for response in MockMvc #27230
Comments
…ng() This commit updates the defaultResponseCharacterEncoding() `default` method in ConfigurableMockMvcBuilder so that it throws an UnsupportedOperationException instead of silently ignoring the user's request to set the default response character encoding. Note, however, that AbstractMockMvcBuilder already overrides the default method with a concrete implementation which is used by default in MockMvc. See spring-projectsgh-27230
Commit e4b9b1f introduced support for setting the default character encoding in MockHttpServletResponse. This commit introduces support for configuring the default character encoding in the underlying MockHttpServletResponse used in MockMvc. Closes spring-projectsgh-27230
…ng() This commit updates the defaultResponseCharacterEncoding() `default` method in ConfigurableMockMvcBuilder so that it throws an UnsupportedOperationException instead of silently ignoring the user's request to set the default response character encoding. Note, however, that AbstractMockMvcBuilder already overrides the default method with a concrete implementation which is used by default in MockMvc. See spring-projectsgh-27230
Hi @sbrannen Could you please help to check this test case, it still fails import static java.nio.charset.StandardCharsets.UTF_8;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
import org.junit.jupiter.api.Test;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
class ResponseBodyTests {
@Test
void text() throws Exception {
standaloneSetup(new PersonController()).defaultResponseCharacterEncoding(UTF_8).build()
.perform(post("/person").characterEncoding(UTF_8).content("Jürgen"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string("Jürgen"));
}
@RestController
private static class PersonController {
@PostMapping("/person")
String post(@RequestBody String content) {
return content;
}
}
} |
Hi @lonre, Thanks for providing the failing test case. I took a look at it, and it appears that the Behind the scenes, a default instance of class RestUtf8StringPayloadTests {
@Test
void test() throws Exception {
MockMvc mockMvc = standaloneSetup(new PersonController())
// .defaultResponseCharacterEncoding(StandardCharsets.UTF_8)
.build();
mockMvc.perform(post("/person")
.contentType("text/plain;charset=UTF-8")
.accept("text/plain;charset=UTF-8")
.content("Jürgen")
)
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().encoding(StandardCharsets.UTF_8))
.andExpect(content().string("Jürgen"));
}
@RestController
private static class PersonController {
@PostMapping("/person")
String post(@RequestBody String content) {
return content;
}
}
} As an alternative to specifying class RestUtf8StringPayloadTests {
@Test
void test() throws Exception {
standaloneSetup(new PersonController()).build()
.perform(post("/person")
.contentType("text/plain;charset=UTF-8")
.content("Jürgen")
)
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().encoding(StandardCharsets.UTF_8))
.andExpect(content().string("Jürgen"));
}
@RestController
private static class PersonController {
@PostMapping(path = "/person", produces = "text/plain;charset=UTF-8")
String post(@RequestBody String content) {
return content;
}
}
}
I believe this may be the expected behavior for |
Hi @lonre, I confirmed with @rstoyanchev that this is the expected behavior for Specifically, the message converters don't take the character encoding of the response into account and instead rely on the HTTP headers. That's why you need to specify the content type for the request as well as the content type for the response (either via the Hope that clarifies matters for you. Cheers Sam |
Overview
#27214 introduced support for setting the default character encoding in
MockHttpServletResponse
, and the focus of this issue is to make that configurable when building an instance ofMockMvc
.Deliverables
defaultResponseCharacterEncoding(Charset)
inConfigurableMockMvcBuilder
and use it to configure the default character encoding in the underlyingMockHttpServletResponse
.The text was updated successfully, but these errors were encountered: