34
34
import org .springframework .mock .web .MockHttpServletResponse ;
35
35
36
36
import static org .hamcrest .Matchers .equalTo ;
37
+ import static org .junit .Assert .assertFalse ;
37
38
import static org .junit .Assert .assertNotNull ;
38
39
import static org .junit .Assert .assertThat ;
39
40
import static org .junit .Assert .assertTrue ;
@@ -59,6 +60,7 @@ public void notAnError() throws Exception {
59
60
assertThat (this .chain .getRequest (), equalTo ((ServletRequest ) this .request ));
60
61
assertThat (((HttpServletResponseWrapper ) this .chain .getResponse ()).getResponse (),
61
62
equalTo ((ServletResponse ) this .response ));
63
+ assertTrue (this .response .isCommitted ());
62
64
}
63
65
64
66
@ Test
@@ -79,6 +81,7 @@ public void doFilter(ServletRequest request, ServletResponse response)
79
81
equalTo ((ServletResponse ) this .response ));
80
82
assertThat (((HttpServletResponseWrapper ) this .chain .getResponse ()).getStatus (),
81
83
equalTo (400 ));
84
+ assertTrue (this .response .isCommitted ());
82
85
}
83
86
84
87
@ Test
@@ -97,6 +100,7 @@ public void doFilter(ServletRequest request, ServletResponse response)
97
100
equalTo ((ServletResponse ) this .response ));
98
101
assertThat (((HttpServletResponseWrapper ) this .chain .getResponse ()).getStatus (),
99
102
equalTo (400 ));
103
+ assertTrue (this .response .isCommitted ());
100
104
}
101
105
102
106
@ Test
@@ -199,6 +203,62 @@ public void doFilter(ServletRequest request, ServletResponse response)
199
203
equalTo ((Object ) "BAD" ));
200
204
assertThat (this .request .getAttribute (RequestDispatcher .ERROR_EXCEPTION_TYPE ),
201
205
equalTo ((Object ) IllegalStateException .class .getName ()));
206
+ assertTrue (this .response .isCommitted ());
207
+ }
208
+
209
+ @ Test
210
+ public void responseIsNotCommitedWhenRequestIsAsync () throws Exception {
211
+ this .request .setAsyncStarted (true );
212
+
213
+ this .filter .doFilter (this .request , this .response , this .chain );
214
+
215
+ assertThat (this .chain .getRequest (), equalTo ((ServletRequest ) this .request ));
216
+ assertThat (((HttpServletResponseWrapper ) this .chain .getResponse ()).getResponse (),
217
+ equalTo ((ServletResponse ) this .response ));
218
+ assertFalse (this .response .isCommitted ());
219
+ }
220
+
221
+ @ Test
222
+ public void responseIsCommitedWhenRequestIsAsyncAndExceptionIsThrown ()
223
+ throws Exception {
224
+ this .filter .addErrorPages (new ErrorPage ("/error" ));
225
+ this .request .setAsyncStarted (true );
226
+ this .chain = new MockFilterChain () {
227
+ @ Override
228
+ public void doFilter (ServletRequest request , ServletResponse response )
229
+ throws IOException , ServletException {
230
+ super .doFilter (request , response );
231
+ throw new RuntimeException ("BAD" );
232
+ }
233
+ };
234
+
235
+ this .filter .doFilter (this .request , this .response , this .chain );
236
+
237
+ assertThat (this .chain .getRequest (), equalTo ((ServletRequest ) this .request ));
238
+ assertThat (((HttpServletResponseWrapper ) this .chain .getResponse ()).getResponse (),
239
+ equalTo ((ServletResponse ) this .response ));
240
+ assertTrue (this .response .isCommitted ());
241
+ }
242
+
243
+ @ Test
244
+ public void responseIsCommitedWhenRequestIsAsyncAndStatusIs400Plus () throws Exception {
245
+ this .filter .addErrorPages (new ErrorPage ("/error" ));
246
+ this .request .setAsyncStarted (true );
247
+ this .chain = new MockFilterChain () {
248
+ @ Override
249
+ public void doFilter (ServletRequest request , ServletResponse response )
250
+ throws IOException , ServletException {
251
+ super .doFilter (request , response );
252
+ ((HttpServletResponse ) response ).sendError (400 , "BAD" );
253
+ }
254
+ };
255
+
256
+ this .filter .doFilter (this .request , this .response , this .chain );
257
+
258
+ assertThat (this .chain .getRequest (), equalTo ((ServletRequest ) this .request ));
259
+ assertThat (((HttpServletResponseWrapper ) this .chain .getResponse ()).getResponse (),
260
+ equalTo ((ServletResponse ) this .response ));
261
+ assertTrue (this .response .isCommitted ());
202
262
}
203
263
204
264
}
0 commit comments