Access request body and request header in spring mvc test

19,814
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class ApplicationTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void test() {

        mockMvc.perform(post("<<url>>").content("<<jsonStrig>>").header("key","value"));
    }

}

In your case:

   @Autowired
    private MockMvc mockMvc;

    @Test
public void test() throws  Exception {

    String jsonString="{\"country\": \"India\", \"currency\": \"INR\", \"president\": \"Ram Nath Kovind\" } ";
    mockMvc.perform(MockMvcRequestBuilders.post("/test").content(jsonString).header("code","12400f74dc4d8d69b713b1fe53f371c25a28a8c5fac2a91eea1f742ab4567c9c"));
}

output:

JSON STRING {"country": "India", "currency": "INR", "president": "Ram Nath Kovind" }  header value 12400f74dc4d8d69b713b1fe53f371c25a28a8c5fac2a91eea1f742ab4567c9c
Share:
19,814
Admin
Author by

Admin

Updated on July 02, 2022

Comments

  • Admin
    Admin almost 2 years

    I have created a spring boot application and this is how my controller looks like. I am using postman to send json in request body and a string in request header, then further hashing the json and comparing it with the string got by request header. The problem is I am unaware of getting the request body and request header in order to test the respective Controller class using MockMvc.

    Controller Logic

    @RestController
    public class Comparison {
    
        @PostMapping(path = "/test")
        public boolean compareHash(@RequestBody String json, 
                                   @RequestHeader(value = "code") String oldHashValue) {
    
            Hash hashObj = new Hash();
            String newHashValue = hashObj.sha512(json);
            return oldHashValue.equals(newHashValue);
        }
    }
    

    Test Logic

    public class ComparisionTest {
    
        @Autowired
        private WebApplicationContext wac;
    
        private MockMvc mockMvc;
    
        @Before
        public void setup () {
            DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.wac);
            this.mockMvc = builder.build();
        }
    
        @Test
        public void contextLoads() throws Exception {
             RecordedRequest recordedRequest = server.takeRequest();
        }
    }
    

    Please help me out in the above code to retrieve the body and header value from request and equating the hash(body) with header value

  • Admin
    Admin over 6 years
    Will this send the json string to the req hash function to get hash value n then compare it with the header value
  • Barath
    Barath over 6 years
    it will reach the controller form there on your function should handle it
  • Admin
    Admin over 6 years
    and I have a doubt that how does it take the json string from the content() and do i need to mention the header name in the "key" ...please do explain in brief since i am new to this thing
  • Barath
    Barath over 6 years
    check the updated answeer.this will work mockMvc.perform(MockMvcRequestBuilders.post("/test").content‌​("hello").header("co‌​de","hello"));
  • Admin
    Admin over 6 years
    the request body in has the JSON String {"country": "India", "currency": "INR", "president": "Ram Nath Kovind" } and request header is 12400f74dc4d8d69b713b1fe53f371c25a28a8c5fac2a91eea1f742ab456‌​7c9c and the key is pkg_hashcode ....this is been sent as POST request from postman ....can you tell me what is that i need to put in the place of "code"
  • Barath
    Barath over 6 years
    is that json your pojo ? or simply string ?
  • Admin
    Admin over 6 years
    yeah its worked...but the test case is being passed ....i need to check it for the fail case too