Print request body in PrintingResultHandler in Spring MVC Test
Prior to this commit, the PrintingResultHandler used by the various print() and log() methods in Spring MVC Test printed the response body but not the request body. Since request bodies are sometimes generated programmatically, however, it can be beneficial to have the dynamically generated request body logged as well. This commit therefore prints the request body in PrintingResultHandler by delegating to the recently introduced getContentAsString() method in MockHttpServletRequest. Issue: SPR-14717
This commit is contained in:
@@ -69,6 +69,10 @@ public class PrintingResultHandlerTests {
|
||||
public void printRequest() throws Exception {
|
||||
this.request.addParameter("param", "paramValue");
|
||||
this.request.addHeader("header", "headerValue");
|
||||
this.request.setCharacterEncoding("UTF-16");
|
||||
String palindrome = "ablE was I ere I saw Elba";
|
||||
byte[] bytes = palindrome.getBytes("UTF-16");
|
||||
this.request.setContent(bytes);
|
||||
|
||||
this.handler.handle(this.mvcResult);
|
||||
|
||||
@@ -78,10 +82,12 @@ public class PrintingResultHandlerTests {
|
||||
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
|
||||
params.add("param", "paramValue");
|
||||
|
||||
|
||||
assertValue("MockHttpServletRequest", "HTTP Method", this.request.getMethod());
|
||||
assertValue("MockHttpServletRequest", "Request URI", this.request.getRequestURI());
|
||||
assertValue("MockHttpServletRequest", "Parameters", params);
|
||||
assertValue("MockHttpServletRequest", "Headers", headers);
|
||||
assertValue("MockHttpServletRequest", "Body", palindrome);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -223,8 +229,9 @@ public class PrintingResultHandlerTests {
|
||||
|
||||
private void assertValue(String heading, String label, Object value) {
|
||||
Map<String, Map<String, Object>> printedValues = this.handler.getPrinter().printedValues;
|
||||
assertTrue("Heading " + heading + " not printed", printedValues.containsKey(heading));
|
||||
assertEquals(value, printedValues.get(heading).get(label));
|
||||
assertTrue("Heading '" + heading + "' not printed", printedValues.containsKey(heading));
|
||||
assertEquals("For label '" + label + "' under heading '" + heading + "' =>", value,
|
||||
printedValues.get(heading).get(label));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -56,7 +56,7 @@ public class PrintingResultHandlerSmokeTests {
|
||||
|
||||
standaloneSetup(new SimpleController())
|
||||
.build()
|
||||
.perform(get("/"))
|
||||
.perform(get("/").content("Hello Request".getBytes()))
|
||||
.andDo(log())
|
||||
.andDo(print())
|
||||
.andDo(print(System.err))
|
||||
@@ -76,7 +76,7 @@ public class PrintingResultHandlerSmokeTests {
|
||||
@ResponseBody
|
||||
public String hello(HttpServletResponse response) {
|
||||
response.addCookie(new Cookie("enigma", "42"));
|
||||
return "Hello world";
|
||||
return "Hello Response";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user