Skip to content

Commit

Permalink
Allow the part headers to be case-insensitive in the model
Browse files Browse the repository at this point in the history
Also add some tests to make sure we can get the size and iterate over
the individual parts
  • Loading branch information
leeturner committed Jun 28, 2024
1 parent c935e63 commit 91eda2f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.github.tomakehurst.wiremock.common.ListOrSingle;
import com.github.tomakehurst.wiremock.http.Body;
import java.util.Map;
import java.util.TreeMap;

public class RequestPartTemplateModel {

Expand All @@ -28,7 +29,8 @@ public class RequestPartTemplateModel {
public RequestPartTemplateModel(
String name, Map<String, ListOrSingle<String>> headers, Body body) {
this.name = name;
this.headers = headers;
this.headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
this.headers.putAll(headers);
this.body = body;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,33 @@ public void multipartRequestPartsAreAvailableViaTemplating() {
+ "file:binary=true:application/octet-stream:QUJDRA=="));
}

@Test
public void multipartRequestPartsHeadersAreCaseInsensitive() {
wm.stubFor(
post("/templated")
.willReturn(
ok(
"multipart:{{request.multipart}}\n"
+ "text:content-type={{request.parts.text.headers.CoNtEnT-TyPe}}\n"
+ "file:content-type={{request.parts.file.headers.cOnTeNt-tYpE}}")));

WireMockResponse response =
client.post(
"/templated",
MultipartEntityBuilder.create()
.addTextBody("text", "hello", ContentType.TEXT_PLAIN)
.addBinaryBody(
"file", "ABCD".getBytes(), ContentType.APPLICATION_OCTET_STREAM, "abcd.bin")
.build());

assertThat(
response.content(),
is(
"multipart:true\n"
+ "text:content-type=text/plain; charset=ISO-8859-1\n"
+ "file:content-type=application/octet-stream"));
}

@Test
public void returnsEmptyPartsInTemplateWhenRequestIsNotMultipart() {
wm.stubFor(
Expand All @@ -85,8 +112,46 @@ public void returnsEmptyPartsInTemplateWhenRequestIsNotMultipart() {
assertThat(response.content(), is("multipart:false\n" + "text::"));
}

// TODO list parts and/or get the count
@Test
public void ableToReturnTheNumberOfParts() {
wm.stubFor(
post("/templated")
.willReturn(
ok("multipart:{{request.multipart}}\n" + "part count = {{size request.parts}}")));
WireMockResponse response =
client.post(
"/templated",
MultipartEntityBuilder.create()
.addTextBody("text", "hello", ContentType.TEXT_PLAIN)
.addBinaryBody(
"file", "ABCD".getBytes(), ContentType.APPLICATION_OCTET_STREAM, "abcd.bin")
.build());

// TODO case-insensitive map for headers or normalisation of key case?
assertThat(response.content(), is("multipart:true\n" + "part count = 2"));
}

@Test
public void ableToIterateOverParts() {
wm.stubFor(
post("/templated")
.willReturn(
ok(
"multipart:{{request.multipart}}\n"
+ "{{#each request.parts as |part|}}{{part.name}}:{{part.headers.content-type}}:{{part.body}}/\n{{/each}}")));
WireMockResponse response =
client.post(
"/templated",
MultipartEntityBuilder.create()
.addTextBody("text", "hello", ContentType.TEXT_PLAIN)
.addBinaryBody(
"file", "ABCD".getBytes(), ContentType.APPLICATION_OCTET_STREAM, "abcd.bin")
.build());

assertThat(
response.content(),
is(
"multipart:true\n"
+ "file:application/octet-stream:ABCD/\n"
+ "text:text/plain; charset=ISO-8859-1:hello/\n"));
}
}

0 comments on commit 91eda2f

Please sign in to comment.