Spring Boot MockMvc.如何使用KotlinDSL传递自定义请求头?

5f0d552i  于 2023-10-16  发布在  Spring
关注(0)|答案(1)|浏览(111)

我有以下工作示例

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()

我想用KotlinDSL重写它

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

我不知道如何传递自定义头。有办法吗?

tvokkenx

tvokkenx1#

特别是从问题的情况下,这种方法将工作:

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

相关问题