Skip to content

Commit

Permalink
make the Summary Formatter closer to reality. issue #516
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Feb 18, 2024
1 parent 1ffe66e commit 0e7b835
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.junit.jupiter.api.Test;

import static BehaviorTests.TestUtil.rezFile;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

class BodyLogSummaryTest extends BddTest {
Expand All @@ -45,7 +46,7 @@ void forSimpleGets() {
.asString();

assertEquals("GET http://somewhere/beans?fruit=apple\n" +
"Accept=image/raw\n" +
"Accept: image/raw\n" +
"===================================", log);
}

Expand All @@ -60,7 +61,7 @@ void forSimpleBodies() {
.asString();

assertEquals("POST http://somewhere/beans?fruit=apple\n" +
"Accept=image/raw\n" +
"Accept: image/raw\n" +
"===================================\n" +
"this is the body", log);
}
Expand All @@ -76,7 +77,7 @@ void forJsonBodies() {
.asString();

assertEquals("POST http://somewhere/beans?fruit=apple\n" +
"Accept=image/raw\n" +
"Accept: image/raw\n" +
"===================================\n" +
"{\"muppet\":\"Gonzo\"}", log);
}
Expand All @@ -92,7 +93,7 @@ void forObjectBodies() {
.asString();

assertEquals("POST http://somewhere/beans?fruit=apple\n" +
"Accept=image/raw\n" +
"Accept: image/raw\n" +
"===================================\n" +
"{\"bar\":\"zip\"}", log);
}
Expand All @@ -109,36 +110,44 @@ void simpleFormBody() {
.asString();

assertEquals("POST http://somewhere/beans?fruit=apple\n" +
"Accept=image/raw\n" +
"Accept: image/raw\n" +
"===================================\n" +
"album=77&band=Talking+Heads", log);
}

@Test @Disabled
@Test
void multiPart() {
String boundary = "ABC-123-BOUNDARY";
String body = Unirest.post(MockServer.ECHO_RAW)
.header("Accept", "image/raw")
.field("band", "Talking Heads")
.field("album", "77")
.field("file", rezFile("/test.txt"))
.boundary(boundary)
.toSummary()
.asString();

assertEquals("POST http://localhost:4567/raw\n" +
"Accept=image/raw\n" +
"===================================\n" +
"--5798a3ff-ed92-4e9e-a9fe-bd7853cea758\n" +
"Content-Disposition: form-data; name:\"album\"\n" +
"77\n" +
"\n" +
"--0ad6dbf7-c5b6-453e-a28c-4a3a8fab7017\n" +
"Content-Disposition: form-data; name:\"band\"\n" +
"Talking Heads\n" +
"\n" +
"--453a52b3-2811-4bf6-8950-6395efe72ef7\n" +
"Content-Disposition: form-data; name=\"file\"; filename=\"null\"\n" +
"Content-Type: application/octet-stream\n" +
"<BINARY DATA>\n", body);
assertThat(body).isEqualTo(
"POST http://localhost:4567/raw\n" +
"Accept: image/raw\n" +
"Content-Type: multipart/form-data; boundary=ABC-123-BOUNDARY;charset=UTF-8\"\n" +
"===================================\n" +
"--ABC-123-BOUNDARY\n" +
"Content-Disposition: form-data; name:\"album\"\n" +
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8\n" +
"77\n" +
"\n" +
"--ABC-123-BOUNDARY\n" +
"Content-Disposition: form-data; name:\"band\"\n" +
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8\n" +
"Talking Heads\n" +
"\n" +
"--ABC-123-BOUNDARY\n" +
"Content-Disposition: form-data; name=\"file\"; filename=\"test.txt\"\n" +
"Content-Type: application/octet-stream\n" +
"<BINARY DATA>\n"
);

}


Expand Down
15 changes: 10 additions & 5 deletions unirest/src/main/java/kong/unirest/core/SummaryFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
package kong.unirest.core;

import java.util.StringJoiner;
import java.util.UUID;
import java.util.function.Function;

class SummaryFormatter implements Function<HttpRequest<?>, String> {
Expand All @@ -35,7 +34,13 @@ class SummaryFormatter implements Function<HttpRequest<?>, String> {
public String apply(HttpRequest<?> req) {
StringJoiner sb = new StringJoiner(System.lineSeparator());
sb.add(req.getHttpMethod().name() + " " + req.getUrl());
req.getHeaders().all().forEach(h -> sb.add(h.getName() + "=" + h.getValue()));
req.getHeaders().all().forEach(h -> sb.add(h.getName() + ": " + h.getValue()));
req.getBody().ifPresent(body -> {
if(!req.getHeaders().containsKey("content-type") && body.isMultiPart()){
sb.add(String.format("Content-Type: multipart/form-data; boundary=%s;charset=%s\"", body.getBoundary(), body.getCharset()));
}
});

sb.add("===================================");
addBody(req, sb);
return sb.toString();
Expand All @@ -59,14 +64,14 @@ private void addBody(HttpRequest<?> req, StringJoiner sb) {

private String toMultiPartAproximation(Body b, StringJoiner sj) {
b.multiParts().forEach(p -> {
String partid = UUID.randomUUID().toString();
sj.add("--"+partid);
sj.add("--"+b.getBoundary());
if(p.isFile()){
sj.add(String.format("Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"", p.getName(), p.getFileName()));
sj.add("Content-Type: application/octet-stream");
sj.add("Content-Type: " + p.getContentType());
sj.add("<BINARY DATA>");
} else {
sj.add("Content-Disposition: form-data; name:\""+p.getName()+"\"");
sj.add("Content-Type: " + p.getContentType());
sj.add(String.valueOf(p.getValue()));
}
sj.add("");
Expand Down

0 comments on commit 0e7b835

Please sign in to comment.