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

MockMvc: jsonpath with skip prefix [SPR-13577] #18154

Closed
spring-projects-issues opened this issue Oct 16, 2015 · 1 comment
Closed

MockMvc: jsonpath with skip prefix [SPR-13577] #18154

spring-projects-issues opened this issue Oct 16, 2015 · 1 comment
Assignees
Labels
in: test Issues in the test module type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Oct 16, 2015

Francesco Pizzo opened SPR-13577 and commented

It would be nice to configure the use of jsonpath in order to skip a given prefix.

Sometimes for security reason a prefix can be defined in for the json response by configuring the jackson2http bean:

    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        log.info("using configureMessageConverters");
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON));
        converter.setPrefixJson(true);
        converter.setJsonPrefix("qwerty");

        return converter;
    }

But then is no more possible to use the jsonpath for testing due to the malformed json:

mockMvc.perform(get("/myresource")
    .contentType(MediaType.APPLICATION_JSON)
    .accept(MediaType.APPLICATION_JSON))
    .andExpect(status().isOk())
    .andDo(print())
    //the code below will throw an exception
    .andExpect(jsonPath("$.status").value("success"))
java.lang.IllegalArgumentException: Invalid container object at com.jayway.jsonpath.JsonPath.read(JsonPath.java:180) at com.jayway.jsonpath.JsonPath.read(JsonPath.java:211) at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:98) at org.springframework.test.util.JsonPathExpectationsHelper.assertValue(JsonPathExpectationsHelper.java:115) at org.springframework.test.web.servlet.result.JsonPathResultMatchers$2.match(JsonPathResultMatchers.java:68) at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:152)

Affects: 4.1.7

Reference URL: http://stackoverflow.com/questions/33151742/mockmvc-jsonpath-skipping-a-prefix

Referenced from: commits 7ae44c2

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Mar 18, 2016

Brian Clozel commented

I've added a new prefix method that not only "removes" the prefix from the considered JSON payload but also checks for that prefix.

mockMvc.perform(get("/myresource")
    .contentType(MediaType.APPLICATION_JSON)
    .accept(MediaType.APPLICATION_JSON))
    .andExpect(status().isOk())
    .andDo(print())
    //the code below will throw an exception
    .andExpect(jsonPath("$.status").prefix("qwerty").value("success"))

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 type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants