Skip to content

Commit 487bc75

Browse files
committed
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
1 parent 04b8ae9 commit 487bc75

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ protected void printRequest(MockHttpServletRequest request) throws Exception {
107107
this.printer.printValue("Request URI", request.getRequestURI());
108108
this.printer.printValue("Parameters", getParamsMultiValueMap(request));
109109
this.printer.printValue("Headers", getRequestHeaders(request));
110+
this.printer.printValue("Body", request.getContentAsString());
110111
}
111112

112113
protected final HttpHeaders getRequestHeaders(MockHttpServletRequest request) {

spring-test/src/test/java/org/springframework/test/web/servlet/result/PrintingResultHandlerTests.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public boolean isAsyncStarted() {
6969
public void printRequest() throws Exception {
7070
this.request.addParameter("param", "paramValue");
7171
this.request.addHeader("header", "headerValue");
72+
this.request.setCharacterEncoding("UTF-16");
73+
String palindrome = "ablE was I ere I saw Elba";
74+
byte[] bytes = palindrome.getBytes("UTF-16");
75+
this.request.setContent(bytes);
7276

7377
this.handler.handle(this.mvcResult);
7478

@@ -78,10 +82,12 @@ public void printRequest() throws Exception {
7882
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
7983
params.add("param", "paramValue");
8084

85+
8186
assertValue("MockHttpServletRequest", "HTTP Method", this.request.getMethod());
8287
assertValue("MockHttpServletRequest", "Request URI", this.request.getRequestURI());
8388
assertValue("MockHttpServletRequest", "Parameters", params);
8489
assertValue("MockHttpServletRequest", "Headers", headers);
90+
assertValue("MockHttpServletRequest", "Body", palindrome);
8591
}
8692

8793
@Test
@@ -223,8 +229,9 @@ public void flashMap() throws Exception {
223229

224230
private void assertValue(String heading, String label, Object value) {
225231
Map<String, Map<String, Object>> printedValues = this.handler.getPrinter().printedValues;
226-
assertTrue("Heading " + heading + " not printed", printedValues.containsKey(heading));
227-
assertEquals(value, printedValues.get(heading).get(label));
232+
assertTrue("Heading '" + heading + "' not printed", printedValues.containsKey(heading));
233+
assertEquals("For label '" + label + "' under heading '" + heading + "' =>", value,
234+
printedValues.get(heading).get(label));
228235
}
229236

230237

spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resulthandlers/PrintingResultHandlerSmokeTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -56,7 +56,7 @@ public void testPrint() throws Exception {
5656

5757
standaloneSetup(new SimpleController())
5858
.build()
59-
.perform(get("/"))
59+
.perform(get("/").content("Hello Request".getBytes()))
6060
.andDo(log())
6161
.andDo(print())
6262
.andDo(print(System.err))
@@ -76,7 +76,7 @@ private static class SimpleController {
7676
@ResponseBody
7777
public String hello(HttpServletResponse response) {
7878
response.addCookie(new Cookie("enigma", "42"));
79-
return "Hello world";
79+
return "Hello Response";
8080
}
8181
}
8282
}

0 commit comments

Comments
 (0)