Nie działa:
private MvcResult login() throws Exception {
String json = String.format("{ \"username\": \"%s\", \"password\": \"%s\"}", this.username, this.password);
return this.mockMvc.perform(post("/api/auth")
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isOk())
.andExpect(header().exists("Authorization"))
.andReturn();
}
@Test
public void changePassword() throws Exception {
String header = this.login().getResponse().getHeader("Authorization");
String passwordJson = String.format("{ \"oldPassword\": \"%s\", \"newPassword\": \"%s\"}", this.password, this.newPassword);
mockMvc.perform(put("/api/user/password/change")
.contentType(MediaType.APPLICATION_JSON)
.content(passwordJson)
.header("Authorization", header))
.andExpect(status().isOk());
Działa:
@Test
public void changePassword() throws Exception {
String json = String.format("{ \"username\": \"%s\", \"password\": \"%s\"}", this.username, this.password);
MvcResult mvcResult = mockMvc.perform(post("/api/auth")
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isOk())
.andExpect(header().exists("Authorization"))
.andReturn();
String header = mvcResult.getResponse().getHeader("Authorization");
String passwordJson = String.format("{ \"oldPassword\": \"%s\", \"newPassword\": \"%s\"}", this.password, this.newPassword);
mockMvc.perform(put("/api/user/password/change")
.contentType(MediaType.APPLICATION_JSON)
.content(passwordJson)
.header("Authorization", header))
.andExpect(status().isOk());
Dlaczego? Zwraca po prostu zły status code.