Skip to content

Commit fc2f3ec

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 d551710 commit fc2f3ec

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
CountDownLatch dispatchLatch = new CountDownLatch(1);
80-
mockAsyncContext.addDispatchHandler(dispatchLatch::countDown);
89+
asyncContext.addDispatchHandler(dispatchLatch::countDown);
8190
getMvcResult(request).setAsyncDispatchLatch(dispatchLatch);
8291
}
8392
}

0 commit comments

Comments
 (0)