Fix MockHttpServletResponse HTTP status update

Prior to this commit, one could call the setStatus method on
this Mock object and update the response's status,
even though the sendError method had already been called.

According to the HttpServletResponse Javadoc, sendError() methods
commit the response; so the response can't be written after that.

This commit fixes MockHttpServletResponse's behavior; setStatus
methods do not update the status once the response has been
committed.

Issue: SPR-10414
This commit is contained in:
Brian Clozel 2013-10-04 15:18:48 +02:00 committed by Rossen Stoyanchev
parent 824cb9f8cd
commit e4479c8669
2 changed files with 25 additions and 3 deletions

View File

@ -533,13 +533,17 @@ public class MockHttpServletResponse implements HttpServletResponse {
@Override
public void setStatus(int status) {
this.status = status;
if(!this.isCommitted()) {
this.status = status;
}
}
@Override
public void setStatus(int status, String errorMessage) {
this.status = status;
this.errorMessage = errorMessage;
if(!this.isCommitted()) {
this.status = status;
this.errorMessage = errorMessage;
}
}
@Override

View File

@ -221,4 +221,22 @@ public class MockHttpServletResponseTests {
assertEquals(redirectUrl, response.getRedirectedUrl());
}
// SPR-10414
@Test
public void modifyStatusAfterSendError() throws IOException {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
response.setStatus(HttpServletResponse.SC_OK);
assertEquals(response.getStatus(),HttpServletResponse.SC_NOT_FOUND);
}
// SPR-10414
@Test
public void modifyStatusMessageAfterSendError() throws IOException {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"Server Error");
assertEquals(response.getStatus(),HttpServletResponse.SC_NOT_FOUND);
}
}