Fixes #22304

Comment From: rstoyanchev

There are some issues. First, the PR does not update all methods with side effects, e.g. setLocale, addCookie, setCookie, etc. Those would be easy to change. The bigger issue is that I've found some test failures that point to the fact Servlet containers are more sophisticated in terms of when exactly they commit the response.

To see the failures you have to also update the MockHttpServletResponse copy in the spring-web/src/test and then run tests in spring-webmvc and spring-test.

As one example, DefaultHandlerExceptionResolverTests#handleHttpMediaTypeNotSupported() fails because in this code sendError commits the response and doesn't allow the subsequent setHeader but in a real Servlet container that does work.

There are other failures too that would need to be investigated further before this can be accepted. As it stands this does not look promising. The original goal was for MockMvc to behave just like a server with regards to committing the response, but replicating that exact behavior seems more nuanced and probably not feasible to align them completely.

Comment From: rstoyanchev

I'm resetting to waiting-to-triage. Until there is a better answer to the questions above, I don't think this is workable.

Comment From: rstoyanchev

Closing for now as it does not seem possible or straight forward to align with actual Servlet container behavior. It would require a deeper look at container vs MockHttpServletResponse behavior with regards to committing and even then there might remain differences by container.

Comment From: amit-chauhan

I know this is closed, but I would expect MockHttpServletRespnse to behave as per the specs, i.e. if committed, then don't allow changes to headers. If that's not possible because you are trying to mimic how other servlet contains handle this, then may be provide an option to enable/disable this feature as needed by the user, e.g. MockHttpServletRespnse.setStrictCommitted (boolean)