Skip to content

mdrozdovz/wiremock-webhooks-extension

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WireMock Webhooks Extension

This library extends WireMock to add support for asynchronously making arbitrary HTTP call-outs (webhooks).

Installation

Maven

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>

Gradle

Add the following to your dependencies:

testCompile 'com.github.tomakehurst:wiremock:2.2.1'
testCompile 'org.wiremock:wiremock-webhooks-extension:0.0.1'

Using in your project

Java

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\" }"))
);

JSON

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()
    )
);

About

Webhook simulation for WireMock

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%