Skip to content

Captures HTTP request/responses interactions with your WireMocks and generates Pact files.

Notifications You must be signed in to change notification settings

dev-rgupta/wiremock-pact-gen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WireMock Pact Generator

Captures HTTP request/responses interactions with your WireMocks and generates Pact files so you can verify, via contract testing, that your mocks are consistent with the real provider.

What is Wiremock Pact Generator

  • A WireMock listener that plugs into existing WireMock instances.
  • Will capture every request and response made to a WireMock server and save them in Pact format.

Requirements

  • Java 1.8 or higher
  • WireMock 1.57 or higher

Installation

Add the wiremock-pact-generator dependency.

Maven

<dependency>
    <groupId>com.atlassian.ta</groupId>
    <artifactId>wiremock-pact-generator</artifactId>
    <scope>test</scope>
</dependency>

Gradle

testCompile group: 'com.atlassian.ta', name: 'wiremock-pact-generator'

Usage

Add the PactGenerator listener to your WireMock server, specifying who the consumer and the provider are.

// 1. Create your WireMock server
wireMockServer = new WireMockServer(...options);

// 2. Add the WireMockPactGenerator listener
wireMockServer.addMockServiceRequestListener(
    WireMockPactGenerator
        .builder("the-consumer", "the-provider")
        .build()
);

// 3. That's it!.. create your endpoint stubs and use them.
wireMockServer.addStubMapping(get(urlEqualTo("/path/resource/123"))
                .willReturn(aResponse().withStatus(200))
                .build()
);
...
myClient.getResource("123")

After running your tests, Maven users will find pact files in the target/pacts directory and Gradle users will find pact files in the build/pacts directory.

Filtering interactions

You can provide request path Regex matchers to tell WireMockPactGenerator which pact interactions should be saved (whitelist) or skipped (blacklist).

myWireMockServer.addMockServiceRequestListener(
    WireMockPactGenerator
        .builder("myConsumer", "myProvider")
        .withRequestPathWhitelist(
            "/rest/.*",
            "/v2/api/.*"
        )
        .withRequestPathBlacklist(
            "/rest/experimental/.*"
        )
        .build()
);
...
// Pact interaction will be generated: Whitelisted
myClient.getResource("/rest/resource/123");

// Pact interaction will be generated: Whitelisted.
myClient.getResource("/v2/api/resources/123");

// NO pact interaction will be generated: Not in whitelist.
myClient.getResource("/internal/api/"); 

// NO pact interaction will be generated: Whitelisted but also blacklisted.
myClient.getResource("/rest/experimental/resource"); 

You can combine whitelist and blacklist values to match your needs. By default, when no whitelist or blacklist values are provided, all interactions will be saved.

Non strict application/json serialization

According to RFC-4627 Section 2. A valid application/json payload must include as root element either a JSON object or a JSON array.

By default WireMock Pact Generator sticks to this rule. This means that when saving pact files for captured request or response bodies with contents such as true, null, 33, "a quoted string", the values will be saved as strings (keeping the quotes in the last example).

However some APIs are not that strict (although this is not recommended), so the default behavior is not desired. In those cases you can configure WireMock Pact Generator to allow serialization of every JSON element:

    WireMockPactGenerator
        .builder("myConsumer", "myProvider")
        .withStrictApplicationJson(false)

When strict application/json is disabled, the above examples will be serialized as a boolean, null, a number, a string without the quotes.

Whitelist request/response headers

You can define what request/response headers should be recorded in the interactions by providing a whitelist of header names (case insensitive).

By default, if no whitelist is provided, all headers are captured.

    WireMockPactGenerator
        .builder("myConsumer", "myProvider")
        .withRequestHeaderWhitelist("header-1", "header-2")
        .withResponseHeaderWhitelist("header-3")
        .build()

About

Captures HTTP request/responses interactions with your WireMocks and generates Pact files.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages