Skip to content

Commit

Permalink
Fix class cast exception in RequestPrinter with byte[] body (#1491)
Browse files Browse the repository at this point in the history
* 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)

* 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)
  • Loading branch information
neiser committed Jun 10, 2022
1 parent 434d59b commit d081c7c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Expand Up @@ -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
Expand Down Expand Up @@ -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<none>%n" +
"Request params:\t<none>%n" +
"Query params:\t<none>%n" +
"Form params:\t<none>%n" +
"Path params:\t<none>%n" +
"Headers:\t\t<none>%n" +
"Cookies:\t\t<none>%n" +
"Multiparts:\t\t<none>%n" +
"Body:%n" +
"[B@")));
}

@Test public void
base_path_is_prepended_to_path_when_logging() {
RestAssuredMockMvc.basePath = "/my-path";
Expand Down
Expand Up @@ -143,7 +143,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 {
Expand Down

0 comments on commit d081c7c

Please sign in to comment.