Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix class cast exception in RequestPrinter with byte[] body #1491

Merged
merged 2 commits into from Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -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 {
Expand Down