【问题标题】:Testing a secure REST endpoint with Spring MVC Test Framework使用 Spring MVC 测试框架测试安全的 REST 端点
【发布时间】:2019-02-04 17:48:03
【问题描述】:

我正在尝试使用Spring MVC Test Framework. 测试一种安全的 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 代码。我想关注这个重定向,或者第一时间在安全通道上执行请求。

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

【问题讨论】:

    标签: spring-mvc kotlin spring-test-mvc spring-security-rest


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-03-11
      • 1970-01-01
      • 2019-07-10
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-04
      相关资源
      最近更新 更多