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

Improper UTF-8 handling in MockMvc for JSON response #23622

Closed
sgybas opened this issue Sep 11, 2019 · 3 comments
Closed

Improper UTF-8 handling in MockMvc for JSON response #23622

sgybas opened this issue Sep 11, 2019 · 3 comments
Assignees
Labels
in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) type: regression A bug that is also a regression
Milestone

Comments

@sgybas
Copy link

sgybas commented Sep 11, 2019

When MediaType.APPLICATION_JSON_UTF8 was deprecated, the content type of JSON that gets sent from a @RestController changed to application/json (without charset).

This breaks MockMvc tests that use .andExpect(content().json()). Here is a sample test:

@Test
public void returnsTheExpectedResponse() throws Exception {
    mockMvc.perform(get("/"))
        .andExpect(status().isOk())
        .andExpect(content().json("{\"name\":\"Jürgen\"}"));
}

The test runs fine with Spring 5.1.9 but fails with Spring 5.2.0.RC2

java.lang.AssertionError: name
Expected: Jürgen
     got: Jürgen

You can find a sample Spring Boot project to reproduce the problem at https://github.com/sgybas/mockmvc-json-utf8

There was a similar issue for jsonPath() (#23219).

@snicoll snicoll changed the title Regression: Improper UTF-8 handling in MockMvc for JSON response Improper UTF-8 handling in MockMvc for JSON response Sep 11, 2019
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 11, 2019
@sdeleuze sdeleuze self-assigned this Sep 11, 2019
@sdeleuze sdeleuze added this to the 5.2 GA milestone Sep 11, 2019
@sdeleuze sdeleuze added in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) type: regression A bug that is also a regression and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 11, 2019
@cbartolome
Copy link

Both overloads of ContentResultMatchers.string() do this, too. Three other functions in that class call getContentAsString() with no arguments, but I'm not sure if that results in the same issue.

@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.9) 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 to go throught all my endpoints todo this.

@rstoyanchev
Copy link
Contributor

rstoyanchev commented Oct 23, 2019

In your tests use ContentResultMatchers#json(String).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) type: regression A bug that is also a regression
Projects
None yet
Development

No branches or pull requests

6 participants