@@ -50,10 +50,8 @@ public class WireMockContainerTest {
5050
5151 @Test
5252 public void helloWorld () throws Exception {
53- final HttpClient client = HttpClient . newBuilder()
54- .version(HttpClient . Version . HTTP_1_1 ). build();
55-
56- HttpRequest request = HttpRequest . newBuilder()
53+ final HttpClient client = HttpClient . newBuilder(). build();
54+ final HttpRequest request = HttpRequest . newBuilder()
5755 .uri(wiremockServer. getRequestURI(" hello" ))
5856 .timeout(Duration . ofSeconds(10 ))
5957 .header(" Content-Type" , " application/json" )
@@ -69,16 +67,101 @@ public class WireMockContainerTest {
6967}
7068```
7169
72- ### Using extensions
70+ ### Using WireMock extensions
7371
7472The API supports adding [ WireMock extensions] ( https://wiremock.org/docs/extending-wiremock/ )
7573to the test container.
7674The extension can be sourced from the classpath for bundled extensions,
7775or added from the JAR file in the initializer.
7876
79- #### Using external extension
77+ #### Using external extensions
78+
79+ For the external extensions,
80+ an extension Jar should be pulled to the test directory before running the test.
81+ [ Apache Maven Dependency Plugin] ( https://maven.apache.org/plugins/maven-dependency-plugin/ ) can be used for this purpose.
82+ Make sure that all dependencies of the extension JAR, if any,
83+ are also included.
84+
85+ Below you can see an examples of using the _ JSON Body Transformer_ extension
86+ from the [ 9cookies/wiremock-extensions] ( https://github.com/9cookies/wiremock-extensions ) .
87+
88+ Copying the dependency:
89+
90+ ``` xml
91+ <plugin >
92+ <groupId >org.apache.maven.plugins</groupId >
93+ <artifactId >maven-dependency-plugin</artifactId >
94+ <version >3.5.0</version >
95+ <executions >
96+ <execution >
97+ <id >copy</id >
98+ <phase >package</phase >
99+ <goals >
100+ <goal >copy</goal >
101+ </goals >
102+ <configuration >
103+ <artifactItems >
104+ <artifactItem >
105+ <groupId >com.ninecookies.wiremock.extensions</groupId >
106+ <artifactId >wiremock-extensions</artifactId >
107+ <version >0.4.1</version >
108+ <classifier >jar-with-dependencies</classifier >
109+ </artifactItem >
110+ </artifactItems >
111+ <outputDirectory >${project.build.directory}/test-wiremock-extension</outputDirectory >
112+ </configuration >
113+ </execution >
114+ </executions >
115+ </plugin >
116+ ```
117+
118+ Mapping definition:
119+
120+ ``` json
121+ {
122+ "request" : {
123+ "method" : " POST" ,
124+ "url" : " /json-body-transformer"
125+ },
126+ "response" : {
127+ "status" : 201 ,
128+ "headers" : {
129+ "content-type" : " application/json"
130+ },
131+ "jsonBody" : {
132+ "message" : " Hello, $(name)!"
133+ },
134+ "transformers" : [" json-body-transformer" ]
135+ }
136+ }
137+ ```
138+
139+ Test sample:
80140
81- For the , an extension Jar should be
141+ ``` java
142+ public class WireMockContainerExtensionTest {
143+ @Rule
144+ public WireMockContainer wiremockServer = new WireMockContainer (" 2.35.0" )
145+ .withMapping(" json-body-transformer" , WireMockContainerExtensionTest . class, " json-body-transformer.json" )
146+ .withExtension(" JSON Body Transformer" , Collections . singleton(" com.ninecookies.wiremock.extensions.JsonBodyTransformer" ),
147+ Collections . singleton(Paths . get(" target" , " test-wiremock-extension" , " 9cookies-wiremock-extensions.jar" ). toFile()));
148+
149+ @Test
150+ public void testJSONBodyTransformer () throws Exception {
151+ final HttpClient client = HttpClient . newBuilder(). build();
152+ final HttpRequest request = HttpRequest . newBuilder()
153+ .uri(wiremockServer. getRequestURI(" json-body-transformer" ))
154+ .timeout(Duration . ofSeconds(10 ))
155+ .header(" Content-Type" , " application/json" )
156+ . POST (HttpRequest . BodyPublishers . ofString(" {\" name\" :\" John Doe\" }" )). build();
157+
158+ HttpResponse<String > response = client. send(request, HttpResponse . BodyHandlers . ofString());
159+
160+ assertThat(response. body()). as(" Wrong response body" )
161+ .contains(" Hello, John Doe!" );
162+ }
163+ }
164+ ```
82165
83166## Contributing
84167
0 commit comments