Skip to content
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

I use getContentAsString() and mockMvc seems to return my content as ISO 8859-1. #23851

Closed
chrisaige opened this issue Oct 23, 2019 · 5 comments
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket)

Comments

@chrisaige
Copy link

chrisaige commented Oct 23, 2019

I use getContentAsString() and mockMvc seems to return my Content as ISO 8859-1.
Before (with Spring Boot 2.1.8) it was proper UTF-8

When I add a produces to my Endpoint like @GetMapping(value = "/test", produces={"application/json; charset=UTF-8"}) it works as indented.
But I dont want todo this on our big project with tons of endpoints

Example:

@RestController
@RequestMapping("test")
public class TestClass {
  @GetMapping
  public List<String> test() {
    return List.of("AEß");
  }
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class})
@AutoConfigureMockMvc()
@ActiveProfiles("test")
@Transactional
public class TestClassTest {
  @Autowired
  private ObjectMapper objectMapper;
  @Autowired
  private MockMvc mvc;
  @Test
  public void test() throws Exception {
    var mvcResult = mvc.perform(MockMvcRequestBuilders
      .get("/test")
      .contentType(MediaType.APPLICATION_JSON)
      .accept(MediaType.APPLICATION_JSON))
      .andExpect(MockMvcResultMatchers.status().isOk())
      .andReturn();

    assertEquals(List.of("AEß"), objectMapper.readValue(mvcResult.getResponse().getContentAsString(),
      new TypeReference<List<String>>() {
      }));
  }
}

Fails with:

java.lang.AssertionError: 
Expected :[AEß]
Actual   :[AE�]

Originally posted by @chrisaige in #23622 (comment)

@chrisaige chrisaige changed the title I use getContentAsString() and mockMvc seems to return my Content as ISO 8859-1. I use getContentAsString() and mockMvc seems to return my content as ISO 8859-1. Oct 23, 2019
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 23, 2019
@sbrannen
Copy link
Member

sbrannen commented Oct 23, 2019

What happens if you do the following?

assertEquals(List.of("AEß"), 
   objectMapper.readValue(mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8),
   new TypeReference<List<String>>() {}));

@sbrannen sbrannen added in: web Issues in web modules (web, webmvc, webflux, websocket) status: waiting-for-feedback We need additional information before we can continue labels Oct 23, 2019
@spring-projects-issues
Copy link
Collaborator

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Oct 30, 2019
@stijnvanbael
Copy link

This issue still exists in Spring Boot 2.2.4. Calling mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8) solves the problem for that one response. But how can I configure UTF8 as the default instead of ISO-8859-1?

@OmarHawk
Copy link

OmarHawk commented Mar 30, 2020

hi,

maybe this is also caused by the issue #24813 I have created.

@rstoyanchev rstoyanchev removed status: feedback-reminder We've sent a reminder that we need additional information before we can continue status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 30, 2020
@sbrannen
Copy link
Member

This issue still exists in Spring Boot 2.2.4. Calling mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8) solves the problem for that one response. But how can I configure UTF8 as the default instead of ISO-8859-1?

@stijnvanbael, you may be interested in the new setDefaultCharacterEncoding(String characterEncoding) method I introduced in MockHttpServletResponse in e4b9b1f.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket)
Projects
None yet
Development

No branches or pull requests

6 participants