1

我正在尝试使用Spring MVC 测试框架测试安全的 REST 方法。

  val result = this.mockMvc!!
     .perform(get("/ping").with(SecurityMockMvcRequestPostProcessors.user("user")))
                .andDo(MockMvcResultHandlers.print())
                .andReturn()

  assertThat(result.response.contentAsString).contains("pong")

问题是,在这个请求中,被模拟的服务器正在响应 302 代码,以重定向到安全通道。结果永远不是2xx代码,永远是302代码。我想关注这个重定向,或者第一时间在安全通道上执行请求。

如何直接在模拟的安全通道上执行此测试?

4

1 回答 1

1

最后,我找到了解决方案。我需要secure(true)在 mocked 上调用 a get("/")。令我惊讶的是,这不在文档上,而在 javadocs 上:

  val result = this.mockMvc!!
     .perform(get("/ping").secure(true).with(SecurityMockMvcRequestPostProcessors.user("user")))
                .andDo(MockMvcResultHandlers.print())
                .andReturn()

  assertThat(result.response.contentAsString).contains("pong")
于 2018-08-30T11:05:35.973 回答