I am trying to unit-test login and security in my REST API, so I try to mock real-life request sequences as close as possible.
My first request would be:
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).
addFilters(springSecurityFilterChain).build();
this.mapper = new ObjectMapper();
....
MvcResult result=mockMvc.perform(get("/login/csrf")).andExpect(status().is(200)).andReturn();
Cookie[] cookies = result.getResponse().getCookies();
(See full class on pastebin).
I try to get the cookie here to be able to login with the received CSRF token later, but the cookies
array is empty!
However, if I run my application and call
curl -i http://localhost:8080/login/csrf
I do get back a Set-Cookie header and can use that cookie (and the CSRF token) later to authenticate.
So the question is: How do I get MockMvc to return a cookie to me?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…