1

I am trying to test a secure REST method, using Spring MVC Test Framework.

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

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

The problem is that on this request, the mocked server is responding a 302 code, to redirect to a secure channel. The result is never 2xx code, is always a 302 code. I would like to follow this redirect, or perform the request on secure channel at the first time.

How can I perform this test to do directly on a mocked secure channel?

John John Pichler
  • 4,427
  • 8
  • 43
  • 72

1 Answers1

1

Finally, I found the solution. I need to call a secure(true) on the mocked get("/"). It surprised me that this is not on documentation, just on javadocs:

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

  assertThat(result.response.contentAsString).contains("pong")
John John Pichler
  • 4,427
  • 8
  • 43
  • 72