Skip to content

Commit 4b16757

Browse files
committed
Add support for __files mapping
1 parent 98c2db3 commit 4b16757

File tree

4 files changed

+99
-12
lines changed

4 files changed

+99
-12
lines changed

src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.wiremock.integrations.testcontainers;
22

3+
import java.io.File;
34
import java.io.IOException;
45
import java.net.URI;
56
import java.net.URISyntaxException;
@@ -11,6 +12,7 @@
1112
import org.testcontainers.containers.GenericContainer;
1213
import org.testcontainers.images.builder.Transferable;
1314
import org.testcontainers.shaded.com.google.common.io.Resources;
15+
import org.testcontainers.utility.MountableFile;
1416

1517
/**
1618
* Provisions WireMock standalone server as a container.
@@ -19,13 +21,15 @@ public class WireMockContainer extends GenericContainer<WireMockContainer> {
1921
private static final String DEFAULT_IMAGE_NAME = "wiremock/wiremock";
2022
private static final String DEFAULT_TAG = "latest";
2123

22-
private static final String SNIPPETS_DIR = "/home/wiremock/mappings/";
24+
private static final String MAPPINGS_DIR = "/home/wiremock/mappings/";
25+
private static final String FILES_DIR = "/home/wiremock/__files/";
2326

2427
private static final int PORT = 8080;
2528

2629
private final StringBuilder wireMockArgs;
2730

28-
private final Map<String, Stub> stubs = new HashMap<>();
31+
private final Map<String, Stub> mappingStubs = new HashMap<>();
32+
private final Map<String, MountableFile> mappingFiles = new HashMap<>();
2933

3034
public WireMockContainer() {
3135
this(DEFAULT_TAG);
@@ -35,27 +39,67 @@ public WireMockContainer(String version) {
3539
this(DEFAULT_IMAGE_NAME, version);
3640
}
3741

38-
public WireMockContainer(String image,String version) {
42+
public WireMockContainer(String image, String version) {
3943
super(image + ":" + version);
4044
wireMockArgs = new StringBuilder();
4145
}
4246

43-
public WireMockContainer withStub(String name, String json) {
44-
stubs.put(name, new Stub(name, json));
47+
/**
48+
* Adds CLI argument to the WireMock call.
49+
* @param arg Argument
50+
* @return this instance
51+
*/
52+
public WireMockContainer withCliArg(String arg) {
53+
//TODO: Switch to framework with proper CLI escaping
54+
wireMockArgs.append(' ').append(arg);
55+
return this;
56+
}
57+
58+
/**
59+
* Adds a JSON mapping stub to WireMock configuration
60+
* @param name Name of the mapping stub
61+
* @param json Configuration JSON
62+
* @return this instance
63+
*/
64+
public WireMockContainer withMapping(String name, String json) {
65+
mappingStubs.put(name, new Stub(name, json));
4566
// TODO: Prevent duplication
4667
return this;
4768
}
4869

49-
public WireMockContainer withStubResource(String name, Class<?> resource, String resourceFile) {
70+
/**
71+
* Loads mapping stub from the class resource
72+
* @param name Name of the mapping stub
73+
* @param resource Resource class. Name of the class will be appended to the resource path
74+
* @param resourceJson Mapping definition file
75+
* @return this instance
76+
*/
77+
public WireMockContainer withMapping(String name, Class<?> resource, String resourceJson) {
5078
try {
51-
URL url = Resources.getResource(resource, resource.getSimpleName() + "/" + resourceFile);
79+
URL url = Resources.getResource(resource, resource.getSimpleName() + "/" + resourceJson);
5280
String text = Resources.toString(url, StandardCharsets.UTF_8);
53-
return withStub(name, text);
81+
return withMapping(name, text);
5482
} catch (IOException ex) {
5583
throw new IllegalArgumentException(ex);
5684
}
5785
}
5886

87+
public WireMockContainer withFile(String name, File file) {
88+
mappingFiles.put(name, MountableFile.forHostPath(file.getPath()));
89+
// TODO: Prevent duplication
90+
return this;
91+
}
92+
93+
public WireMockContainer withFileFromResource(String name, String classpathResource) {
94+
mappingFiles.put(name, MountableFile.forClasspathResource(classpathResource));
95+
// TODO: Prevent duplication
96+
return this;
97+
}
98+
99+
public WireMockContainer withFileFromResource(String name, Class<?> resource, String filename) {
100+
return withFileFromResource(name, resource.getName().replace('.', '/') + "/" + filename);
101+
}
102+
59103
public String getEndpoint() {
60104
return String.format("http://%s:%d", getHost(), getMappedPort(PORT));
61105
}
@@ -73,8 +117,12 @@ protected void configure() {
73117
super.configure();
74118
withExposedPorts(PORT);
75119
withCommand(wireMockArgs.toString());
76-
for (Stub stub : stubs.values()) {
77-
withCopyToContainer(Transferable.of(stub.json), SNIPPETS_DIR + stub.name + ".json");
120+
for (Stub stub : mappingStubs.values()) {
121+
withCopyToContainer(Transferable.of(stub.json), MAPPINGS_DIR + stub.name + ".json");
122+
}
123+
124+
for (Map.Entry<String, MountableFile> mount : mappingFiles.entrySet()) {
125+
withCopyToContainer(mount.getValue(), FILES_DIR + mount.getKey());
78126
}
79127
}
80128

src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerTest.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ public class WireMockContainerTest {
1414

1515
@Rule
1616
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
17-
.withStubResource("hello", WireMockContainerTest.class, "hello-world.json");
17+
.withMapping("hello", WireMockContainerTest.class, "hello-world.json")
18+
.withMapping("hello-resource", WireMockContainerTest.class, "hello-world-resource.json")
19+
.withFileFromResource("hello-world-resource-response.xml", WireMockContainerTest.class, "hello-world-resource-response.xml");
1820

1921
@Test
20-
public void smokeTest() throws Exception {
22+
public void helloWorld() throws Exception {
2123
final HttpClient client = HttpClient.newBuilder()
2224
.version(HttpClient.Version.HTTP_1_1)
2325
.build();
@@ -36,4 +38,25 @@ public void smokeTest() throws Exception {
3638
.as("Wrong response body")
3739
.contains("Hello, world!");
3840
}
41+
42+
@Test
43+
public void helloWorldFromFile() throws Exception {
44+
final HttpClient client = HttpClient.newBuilder()
45+
.version(HttpClient.Version.HTTP_1_1)
46+
.build();
47+
48+
HttpRequest request = HttpRequest.newBuilder()
49+
.uri(wiremockServer.getRequestURI("hello-from-file"))
50+
.timeout(Duration.ofSeconds(10))
51+
.header("Content-Type", "application/json")
52+
.GET()
53+
.build();
54+
55+
HttpResponse<String> response =
56+
client.send(request, HttpResponse.BodyHandlers.ofString());
57+
58+
assertThat(response.body())
59+
.as("Wrong response body")
60+
.contains("Hello, world!");
61+
}
3962
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<note>
2+
<to>you</to>
3+
<from>WireMock</from>
4+
<heading>Response</heading>
5+
<body>Hello, world!</body>
6+
</note>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"request": {
3+
"method": "GET",
4+
"url": "/hello-from-file"
5+
},
6+
"response": {
7+
"status": 200,
8+
"bodyFileName": "hello-world-resource-response.xml"
9+
}
10+
}

0 commit comments

Comments
 (0)