Spring Boot Test MockMvc 获取自定义标头值

问题描述

我使用的是 Spring Boot v2.4.2。我已经设置了 Spring Security 和 Session Management,所以它在响应头中返回“x-auth-token”。我用 Postman 和浏览器对此进行了测试。它有这个标题,这是完美的。 然后在我的 Springboot 集成测试中,我尝试在响应中读取此标头。但它没有这个标题。我收到所有其他标题,除了这个。我想这是因为自定义标题。任何的想法?这是我的代码

MockHttpServletResponse response =
            mockmvc.perform(post("/api/user-management/auth/login").contentType(APPLICATION_JSON)
                    .content(objectMapper.writeValueAsstring(myDataObject)))
                    .andExpect(status().isOk())                        
                    .andReturn().getResponse();
    System.out.println("Token: " + response.getHeaderValue("x-auth-token"));

我已经按如下方式设置了 mockmvc

@BeforeEach
public void setUp(WebApplicationContext webApplicationContext,RestDocumentationContextProvider restDocumentation) {
    this.mockmvc = mockmvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity())         
                   .build();
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)