From d5f4fefe8ccad7a1b74e4b8830d56b1c0d96be57 Mon Sep 17 00:00:00 2001 From: Andreas Grub Date: Fri, 23 Jul 2021 10:19:00 +0200 Subject: [PATCH 1/2] Reproduce class cast error in request printing with byte[] body Only happens if shouldPrettyPrint is false, otherwise the groovy code handles non-String bodies more gracefully. Exception is: java.lang.ClassCastException: class [B cannot be cast to class java.lang.String ([B and java.lang.String are in module java.base of loader 'bootstrap') at io.restassured.module.mockmvc.RequestLoggingTest.can_supply_byte_array_as_body_for_post(RequestLoggingTest.java:161) --- .../module/mockmvc/RequestLoggingTest.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/modules/spring-mock-mvc/src/test/java/io/restassured/module/mockmvc/RequestLoggingTest.java b/modules/spring-mock-mvc/src/test/java/io/restassured/module/mockmvc/RequestLoggingTest.java index 7bd4d4183..b9e985de8 100644 --- a/modules/spring-mock-mvc/src/test/java/io/restassured/module/mockmvc/RequestLoggingTest.java +++ b/modules/spring-mock-mvc/src/test/java/io/restassured/module/mockmvc/RequestLoggingTest.java @@ -33,6 +33,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.emptyString; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.fail; // @formatter:off @@ -150,6 +151,31 @@ public class RequestLoggingTest { "a string%n"))); } + @Test public void + can_supply_byte_array_as_body_for_post() { + RestAssuredMockMvc.given(). + standaloneSetup(new PostController()). + log().all(false). + body(new byte[] {'B', 'O', 'D', 'Y'}). + when(). + post("/stringBody"). + then(). + body(equalTo("BODY")); + + assertThat(writer.toString(), startsWith(String.format("Request method:\tPOST%n" + + "Request URI:\thttp://localhost:8080/stringBody%n" + + "Proxy:\t\t\t%n" + + "Request params:\t%n" + + "Query params:\t%n" + + "Form params:\t%n" + + "Path params:\t%n" + + "Headers:\t\t%n" + + "Cookies:\t\t%n" + + "Multiparts:\t\t%n" + + "Body:%n" + + "a string%n"))); + } + @Test public void base_path_is_prepended_to_path_when_logging() { RestAssuredMockMvc.basePath = "/my-path"; From 93fdebd5404fa230a53f798f45ce904b14be0fed Mon Sep 17 00:00:00 2001 From: Andreas Grub Date: Fri, 23 Jul 2021 10:27:47 +0200 Subject: [PATCH 2/2] Fix class cast error in request printing with byte[] body Only happens if shouldPrettyPrint is false, otherwise the groovy code handles non-String bodies more gracefully. The test only checks if the body is [B@, as the hash of the object changes on every test run. Exception is: java.lang.ClassCastException: class [B cannot be cast to class java.lang.String ([B and java.lang.String are in module java.base of loader 'bootstrap') at io.restassured.module.mockmvc.RequestLoggingTest.can_supply_byte_array_as_body_for_post(RequestLoggingTest.java:161) --- .../java/io/restassured/module/mockmvc/RequestLoggingTest.java | 2 +- .../java/io/restassured/internal/print/RequestPrinter.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/spring-mock-mvc/src/test/java/io/restassured/module/mockmvc/RequestLoggingTest.java b/modules/spring-mock-mvc/src/test/java/io/restassured/module/mockmvc/RequestLoggingTest.java index b9e985de8..3921a207f 100644 --- a/modules/spring-mock-mvc/src/test/java/io/restassured/module/mockmvc/RequestLoggingTest.java +++ b/modules/spring-mock-mvc/src/test/java/io/restassured/module/mockmvc/RequestLoggingTest.java @@ -173,7 +173,7 @@ public class RequestLoggingTest { "Cookies:\t\t%n" + "Multiparts:\t\t%n" + "Body:%n" + - "a string%n"))); + "[B@"))); } @Test public void diff --git a/rest-assured/src/main/java/io/restassured/internal/print/RequestPrinter.java b/rest-assured/src/main/java/io/restassured/internal/print/RequestPrinter.java index eb5f3ee30..abe22607f 100644 --- a/rest-assured/src/main/java/io/restassured/internal/print/RequestPrinter.java +++ b/rest-assured/src/main/java/io/restassured/internal/print/RequestPrinter.java @@ -103,7 +103,8 @@ private static void addProxy(FilterableRequestSpecification requestSpec, StringB private static void addBody(FilterableRequestSpecification requestSpec, StringBuilder builder, boolean shouldPrettyPrint) { builder.append("Body:"); if (requestSpec.getBody() != null) { - final String body; + // Note: requestSpec.getBody() below is generic and may not always return a String! + final Object body; if (shouldPrettyPrint) { body = new Prettifier().getPrettifiedBodyIfPossible(requestSpec); } else {