@@ -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