Prevent ISE in the MockMvc PrintingResultHandler

Issue: SPR-12735
This commit is contained in:
Rossen Stoyanchev
2015-02-27 11:30:12 -05:00
parent 4a332bd0b2
commit b4d9fb9e6e
2 changed files with 32 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package org.springframework.test.web.servlet.result;
import java.util.Enumeration;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.HttpHeaders;
@@ -127,8 +128,15 @@ public class PrintingResultHandler implements ResultHandler {
protected void printAsyncResult(MvcResult result) throws Exception {
HttpServletRequest request = result.getRequest();
this.printer.printValue("Was async started", request.isAsyncStarted());
this.printer.printValue("Async result", (request.isAsyncStarted() ? result.getAsyncResult(0) : null));
this.printer.printValue("Async started", request.isAsyncStarted());
Object asyncResult = null;
try {
asyncResult = result.getAsyncResult(0);
}
catch (IllegalStateException ex) {
// Not set
}
this.printer.printValue("Async result", asyncResult);
}
/** Print the handler */

View File

@@ -16,6 +16,7 @@
package org.springframework.test.web.servlet.samples.standalone;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
@@ -56,7 +57,7 @@ public class AsyncTests {
this.asyncController = new AsyncController();
this.mockMvc = standaloneSetup(this.asyncController).build();
}
@Test
public void testCallable() throws Exception {
@@ -112,6 +113,25 @@ public class AsyncTests {
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
}
// SPR-12735
@Test
public void testPrintAsyncResult() throws Exception {
MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResult", "true"))
.andDo(print())
.andExpect(request().asyncStarted())
.andReturn();
this.asyncController.onMessage("Joe");
this.mockMvc.perform(asyncDispatch(mvcResult))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
}
@Controller
private static class AsyncController {