Skip to content

Commit f61f6f2

Browse files
committed
More defensive check for MockAsyncContext
Avoid automatically unwrapping the request in TestDispatcherServlet, if we find the MockAsyncContext. Issue: SPR-17353
1 parent de1139e commit f61f6f2

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/TestDispatcherServlet.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,22 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
7171
super.service(request, response);
7272

7373
if (request.getAsyncContext() != null) {
74-
MockHttpServletRequest mockRequest = WebUtils.getNativeRequest(request, MockHttpServletRequest.class);
75-
Assert.notNull(mockRequest, "Expected MockHttpServletRequest");
76-
MockAsyncContext mockAsyncContext = ((MockAsyncContext) mockRequest.getAsyncContext());
77-
Assert.notNull(mockAsyncContext, "MockAsyncContext not found. Did request wrapper not delegate startAsync?");
74+
MockAsyncContext asyncContext;
75+
if (request.getAsyncContext() instanceof MockAsyncContext) {
76+
asyncContext = (MockAsyncContext) request.getAsyncContext();
77+
}
78+
else {
79+
MockHttpServletRequest mockRequest = WebUtils.getNativeRequest(request, MockHttpServletRequest.class);
80+
Assert.notNull(mockRequest, "Expected MockHttpServletRequest");
81+
asyncContext = (MockAsyncContext) mockRequest.getAsyncContext();
82+
Assert.notNull(asyncContext, () ->
83+
"Outer request wrapper " + request.getClass().getName() + " has an AsyncContext," +
84+
"but it is not a MockAsyncContext, while the nested " +
85+
mockRequest.getClass().getName() + " does not have an AsyncContext at all.");
86+
}
7887

7988
final CountDownLatch dispatchLatch = new CountDownLatch(1);
80-
mockAsyncContext.addDispatchHandler(new Runnable() {
89+
asyncContext.addDispatchHandler(new Runnable() {
8190
@Override
8291
public void run() {
8392
dispatchLatch.countDown();

0 commit comments

Comments
 (0)