spring-bootkotlinspring-mvcspring-boot-testmockmvc

MockMvc. How to pass custom request header using kotlin DSL?


I have following working example

val headers = HttpHeaders()
headers.add("Content-Type", "application/merge-patch+json")  

mockMvc.perform(
        patch(path)
            .headers(headers)
            .content(objectMapper.writeValueAsString(patchRequest)),
    ).andExpect(status().isOk)
        .andReturn()

And I want to rewrite it using kotlin DSL

    val result = mockMvc.patch(path) {
        //this.header = headers
        content = objectMapper.writeValueAsString(patchRequest)
    }.andExpect{ status { isOk() } }
        .andReturn()

I don't see how to pass custom header. Is there way to do it ?


Solution

  • Particular for the case from the question this approach will work:

    val result = mockMvc.patch(path) {
         contentType = MediaType.valueOf("application/merge-patch+json")
         content = objectMapper.writeValueAsString(patchRequest)
     }.andExpect{ status { isOk() } }
         .andReturn()