Skip to content

Commit 91eda2f

Browse files
committed
Allow the part headers to be case-insensitive in the model
Also add some tests to make sure we can get the size and iterate over the individual parts
1 parent c935e63 commit 91eda2f

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

src/main/java/com/github/tomakehurst/wiremock/extension/responsetemplating/RequestPartTemplateModel.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.github.tomakehurst.wiremock.common.ListOrSingle;
1919
import com.github.tomakehurst.wiremock.http.Body;
2020
import java.util.Map;
21+
import java.util.TreeMap;
2122

2223
public class RequestPartTemplateModel {
2324

@@ -28,7 +29,8 @@ public class RequestPartTemplateModel {
2829
public RequestPartTemplateModel(
2930
String name, Map<String, ListOrSingle<String>> headers, Body body) {
3031
this.name = name;
31-
this.headers = headers;
32+
this.headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
33+
this.headers.putAll(headers);
3234
this.body = body;
3335
}
3436

src/test/java/com/github/tomakehurst/wiremock/MultipartTemplatingAcceptanceTest.java

+67-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,33 @@ public void multipartRequestPartsAreAvailableViaTemplating() {
7171
+ "file:binary=true:application/octet-stream:QUJDRA=="));
7272
}
7373

74+
@Test
75+
public void multipartRequestPartsHeadersAreCaseInsensitive() {
76+
wm.stubFor(
77+
post("/templated")
78+
.willReturn(
79+
ok(
80+
"multipart:{{request.multipart}}\n"
81+
+ "text:content-type={{request.parts.text.headers.CoNtEnT-TyPe}}\n"
82+
+ "file:content-type={{request.parts.file.headers.cOnTeNt-tYpE}}")));
83+
84+
WireMockResponse response =
85+
client.post(
86+
"/templated",
87+
MultipartEntityBuilder.create()
88+
.addTextBody("text", "hello", ContentType.TEXT_PLAIN)
89+
.addBinaryBody(
90+
"file", "ABCD".getBytes(), ContentType.APPLICATION_OCTET_STREAM, "abcd.bin")
91+
.build());
92+
93+
assertThat(
94+
response.content(),
95+
is(
96+
"multipart:true\n"
97+
+ "text:content-type=text/plain; charset=ISO-8859-1\n"
98+
+ "file:content-type=application/octet-stream"));
99+
}
100+
74101
@Test
75102
public void returnsEmptyPartsInTemplateWhenRequestIsNotMultipart() {
76103
wm.stubFor(
@@ -85,8 +112,46 @@ public void returnsEmptyPartsInTemplateWhenRequestIsNotMultipart() {
85112
assertThat(response.content(), is("multipart:false\n" + "text::"));
86113
}
87114

88-
// TODO list parts and/or get the count
115+
@Test
116+
public void ableToReturnTheNumberOfParts() {
117+
wm.stubFor(
118+
post("/templated")
119+
.willReturn(
120+
ok("multipart:{{request.multipart}}\n" + "part count = {{size request.parts}}")));
121+
WireMockResponse response =
122+
client.post(
123+
"/templated",
124+
MultipartEntityBuilder.create()
125+
.addTextBody("text", "hello", ContentType.TEXT_PLAIN)
126+
.addBinaryBody(
127+
"file", "ABCD".getBytes(), ContentType.APPLICATION_OCTET_STREAM, "abcd.bin")
128+
.build());
89129

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

133+
@Test
134+
public void ableToIterateOverParts() {
135+
wm.stubFor(
136+
post("/templated")
137+
.willReturn(
138+
ok(
139+
"multipart:{{request.multipart}}\n"
140+
+ "{{#each request.parts as |part|}}{{part.name}}:{{part.headers.content-type}}:{{part.body}}/\n{{/each}}")));
141+
WireMockResponse response =
142+
client.post(
143+
"/templated",
144+
MultipartEntityBuilder.create()
145+
.addTextBody("text", "hello", ContentType.TEXT_PLAIN)
146+
.addBinaryBody(
147+
"file", "ABCD".getBytes(), ContentType.APPLICATION_OCTET_STREAM, "abcd.bin")
148+
.build());
149+
150+
assertThat(
151+
response.content(),
152+
is(
153+
"multipart:true\n"
154+
+ "file:application/octet-stream:ABCD/\n"
155+
+ "text:text/plain; charset=ISO-8859-1:hello/\n"));
156+
}
92157
}

0 commit comments

Comments
 (0)