|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2024 the original author or authors. |
| 2 | + * Copyright 2002-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
@@ -924,6 +924,23 @@ void shouldAttemptToResetResponseBufferIfCommitted() throws Exception {
|
924 | 924 | assertThat(response.getHeader("Test-Header")).isEqualTo("spring");
|
925 | 925 | }
|
926 | 926 |
|
| 927 | + @Test |
| 928 | + void shouldResetContentTypeIfNotCommitted() throws Exception { |
| 929 | + StaticWebApplicationContext context = new StaticWebApplicationContext(); |
| 930 | + context.setServletContext(getServletContext()); |
| 931 | + context.registerSingleton("/error", ErrorController.class); |
| 932 | + DispatcherServlet servlet = new DispatcherServlet(context); |
| 933 | + servlet.init(servletConfig); |
| 934 | + |
| 935 | + MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/error"); |
| 936 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 937 | + assertThatThrownBy(() -> servlet.service(request, response)).isInstanceOf(ServletException.class) |
| 938 | + .hasCauseInstanceOf(IllegalArgumentException.class); |
| 939 | + assertThat(response.getContentAsByteArray()).isEmpty(); |
| 940 | + assertThat(response.getStatus()).isEqualTo(400); |
| 941 | + assertThat(response.getHeaderNames()).doesNotContain(HttpHeaders.CONTENT_TYPE); |
| 942 | + } |
| 943 | + |
927 | 944 |
|
928 | 945 | public static class ControllerFromParent implements Controller {
|
929 | 946 |
|
@@ -976,6 +993,7 @@ private static class ErrorController implements Controller {
|
976 | 993 | public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
977 | 994 | response.setStatus(400);
|
978 | 995 | response.setHeader("Test-Header", "spring");
|
| 996 | + response.addHeader("Content-Type", "application/json"); |
979 | 997 | if (request.getAttribute("commit") != null) {
|
980 | 998 | response.flushBuffer();
|
981 | 999 | }
|
|
0 commit comments