-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2782 from wiremock/multipart-request-template-model
Multipart request template model
- Loading branch information
Showing
8 changed files
with
280 additions
and
13 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
...om/github/tomakehurst/wiremock/extension/responsetemplating/RequestPartTemplateModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (C) 2024 Thomas Akehurst | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.tomakehurst.wiremock.extension.responsetemplating; | ||
|
||
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 { | ||
|
||
private final String name; | ||
private final Map<String, ListOrSingle<String>> headers; | ||
private final Body body; | ||
|
||
public RequestPartTemplateModel( | ||
String name, Map<String, ListOrSingle<String>> headers, Body body) { | ||
this.name = name; | ||
this.headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); | ||
this.headers.putAll(headers); | ||
this.body = body; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Map<String, ListOrSingle<String>> getHeaders() { | ||
return headers; | ||
} | ||
|
||
public String getBody() { | ||
return body.asString(); | ||
} | ||
|
||
public String getBodyAsBase64() { | ||
return body.asBase64(); | ||
} | ||
|
||
public boolean isBinary() { | ||
return body.isBinary(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
src/test/java/com/github/tomakehurst/wiremock/MultipartTemplatingAcceptanceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/* | ||
* Copyright (C) 2024 Thomas Akehurst | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.tomakehurst.wiremock; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.*; | ||
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import com.github.tomakehurst.wiremock.junit5.WireMockExtension; | ||
import com.github.tomakehurst.wiremock.testsupport.WireMockResponse; | ||
import com.github.tomakehurst.wiremock.testsupport.WireMockTestClient; | ||
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder; | ||
import org.apache.hc.core5.http.ContentType; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
public class MultipartTemplatingAcceptanceTest { | ||
|
||
WireMockTestClient client; | ||
|
||
@RegisterExtension | ||
public static WireMockExtension wm = | ||
WireMockExtension.newInstance() | ||
.options(options().dynamicPort().templatingEnabled(true).globalTemplating(true)) | ||
.build(); | ||
|
||
@BeforeEach | ||
void init() { | ||
client = new WireMockTestClient(wm.getPort()); | ||
} | ||
|
||
@Test | ||
public void multipartRequestPartsAreAvailableViaTemplating() { | ||
wm.stubFor( | ||
post("/templated") | ||
.willReturn( | ||
ok( | ||
"multipart:{{request.multipart}}\n" | ||
+ "text:binary={{request.parts.text.binary}}:{{request.parts.text.headers.content-type}}:{{request.parts.text.body}}\n" | ||
+ "file:binary={{request.parts.file.binary}}:{{request.parts.file.headers.content-type}}:{{request.parts.file.bodyAsBase64}}"))); | ||
|
||
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:binary=false:text/plain; charset=ISO-8859-1:hello\n" | ||
+ "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( | ||
post("/templated") | ||
.willReturn( | ||
ok( | ||
"multipart:{{request.multipart}}\n" | ||
+ "text:{{request.parts.text.headers.content-type}}:{{request.parts.text.body}}"))); | ||
|
||
WireMockResponse response = client.postJson("/templated", "{}"); | ||
|
||
assertThat(response.content(), is("multipart:false\n" + "text::")); | ||
} | ||
|
||
@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()); | ||
|
||
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")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters