I'm using Spring boot and have Spring Security setup to use token authorization. I have my test is setup like so:
@RunWith(SpringRunner.class)
@SpringBootTest(properties = {
})
@AutoConfigureMockMvc()
@EnableAutoConfiguration(exclude = {
})
public class ApplicationTests {
@Test
public void shouldReturnRepositoryIndex() throws Exception {
mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk()).andExpect(
jsonPath("$._links.people").exists());
}
}
I need get("/")
to send a header "X-AUTH: SOMETOKEN"
How do I do that?
.header("X-AUTH" , "SOMETOKEN")
should work.
Here is the modified mockMvc code:
mockMvc.perform(get("/").header("X-AUTH" , "SOMETOKEN")).andDo(print()).andExpect(status().isOk()).andExpect(
jsonPath("$._links.people").exists());
The output is:
MockHttpServletRequest:
HTTP Method = GET
Request URI = /
Parameters = {}
Headers = {X-AUTH=[SOMETOKEN]}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With