More defensive check for MockAsyncContext
Avoid automatically unwrapping the request in TestDispatcherServlet, if we find the MockAsyncContext. Issue: SPR-17353
This commit is contained in:
@@ -71,13 +71,22 @@ final class TestDispatcherServlet extends DispatcherServlet {
|
|||||||
super.service(request, response);
|
super.service(request, response);
|
||||||
|
|
||||||
if (request.getAsyncContext() != null) {
|
if (request.getAsyncContext() != null) {
|
||||||
MockHttpServletRequest mockRequest = WebUtils.getNativeRequest(request, MockHttpServletRequest.class);
|
MockAsyncContext asyncContext;
|
||||||
Assert.notNull(mockRequest, "Expected MockHttpServletRequest");
|
if (request.getAsyncContext() instanceof MockAsyncContext) {
|
||||||
MockAsyncContext mockAsyncContext = ((MockAsyncContext) mockRequest.getAsyncContext());
|
asyncContext = (MockAsyncContext) request.getAsyncContext();
|
||||||
Assert.notNull(mockAsyncContext, "MockAsyncContext not found. Did request wrapper not delegate startAsync?");
|
}
|
||||||
|
else {
|
||||||
|
MockHttpServletRequest mockRequest = WebUtils.getNativeRequest(request, MockHttpServletRequest.class);
|
||||||
|
Assert.notNull(mockRequest, "Expected MockHttpServletRequest");
|
||||||
|
asyncContext = (MockAsyncContext) mockRequest.getAsyncContext();
|
||||||
|
Assert.notNull(asyncContext, () ->
|
||||||
|
"Outer request wrapper " + request.getClass().getName() + " has an AsyncContext," +
|
||||||
|
"but it is not a MockAsyncContext, while the nested " +
|
||||||
|
mockRequest.getClass().getName() + " does not have an AsyncContext at all.");
|
||||||
|
}
|
||||||
|
|
||||||
CountDownLatch dispatchLatch = new CountDownLatch(1);
|
CountDownLatch dispatchLatch = new CountDownLatch(1);
|
||||||
mockAsyncContext.addDispatchHandler(dispatchLatch::countDown);
|
asyncContext.addDispatchHandler(dispatchLatch::countDown);
|
||||||
getMvcResult(request).setAsyncDispatchLatch(dispatchLatch);
|
getMvcResult(request).setAsyncDispatchLatch(dispatchLatch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user