This library extends WireMock to add support for asynchronously making arbitrary HTTP call-outs (webhooks).
Add the following to your POM:
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.15.0</version>
<scope>test</test>
</dependency>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-webhooks-extension</artifactId>
<version>0.0.1</version>
<scope>test</test>
</dependency>
Add the following to your dependencies:
testCompile 'com.github.tomakehurst:wiremock:2.2.1'
testCompile 'org.wiremock:wiremock-webhooks-extension:0.0.1'
When constructing the WireMock JUnit rule or server, add a Webhooks
instance as an extension:
@Rule
public WireMockRule rule = new WireMockRule(
options()
.dynamicPort()
.extensions(webhooks));
Then use the DSL provided by the extension to configure webhooks to respond when specific stubs are hit:
import static org.wiremock.webhooks.Webhooks.webhook;
...
rule.stubFor(post(urlPathEqualTo("/something-async"))
.willReturn(aResponse().withStatus(200))
.withPostServeAction("webhook", webhook()
.withMethod(POST)
.withUrl("http://localhost:" + targetServer.port() + "/callback")
.withHeader("Content-Type", "application/json")
.withBody("{ \"result\": \"SUCCESS\" }"))
);
You can also use JSON to configure webhooks:
{
"request" : {
"urlPath" : "/something-async",
"method" : "POST"
},
"response" : {
"status" : 200
},
"postServeActions" : {
"webhook" : {
"headers" : {
"Content-Type" : "application/json"
},
"method" : "POST",
"body" : "{ \"result\": \"SUCCESS\" }",
"url" : "http://localhost:56299/callback"
}
}
}
You can also get the JSON representation of something produced by the DSL:
System.out.println(Json.write(post(urlPathEqualTo("/something"))
.willReturn(aResponse().withStatus(200))
.withPostServeAction("webhook", webhook()
.withMethod(POST)
.withUrl("http://localhost:" + targetServer.port() + "/callback")
.withHeader("Content-Type", "application/json")
.withBody("{ \"result\": \"SUCCESS\" }")).build()
)
);