wiremock-snapshot

Wiremock API extension to record stub mappings

License

License

The Apache Software License, Version 2.0
Categories

Categories

Wire Data Data Structures
GroupId

GroupId

com.github.masonm
ArtifactId

ArtifactId

wiremock-snapshot
Last Version

Last Version

0.3a
Release Date

Release Date

Type

Type

jar
Description

Description

wiremock-snapshot
Wiremock API extension to record stub mappings
Project URL

Project URL

https://github.com/MasonM/wiremock-snapshot
Source Code Management

Source Code Management

https://github.com/MasonM/wiremock-snapshot

Download wiremock-snapshot

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.masonm/wiremock-snapshot/ -->
<dependency>
    <groupId>com.github.masonm</groupId>
    <artifactId>wiremock-snapshot</artifactId>
    <version>0.3a</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.masonm/wiremock-snapshot/
implementation 'com.github.masonm:wiremock-snapshot:0.3a'
// https://jarcasting.com/artifacts/com.github.masonm/wiremock-snapshot/
implementation ("com.github.masonm:wiremock-snapshot:0.3a")
'com.github.masonm:wiremock-snapshot:jar:0.3a'
<dependency org="com.github.masonm" name="wiremock-snapshot" rev="0.3a">
  <artifact name="wiremock-snapshot" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.masonm', module='wiremock-snapshot', version='0.3a')
)
libraryDependencies += "com.github.masonm" % "wiremock-snapshot" % "0.3a"
[com.github.masonm/wiremock-snapshot "0.3a"]

Dependencies

compile (4)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.21
com.github.tomakehurst : wiremock jar 2.6.0
org.apache.commons : commons-lang3 jar 3.5
com.google.guava : guava jar 18.0

test (6)

Group / Artifact Type Version
junit : junit jar 4.12
org.hamcrest : hamcrest-all jar 1.3
org.jmock : jmock jar 2.5.1
org.jmock : jmock-junit4 jar 2.5.1
org.skyscreamer : jsonassert jar 1.2.3
com.toomuchcoding.jsonassert : jsonassert jar 0.4.7

Project Modules

There are no modules declared in this project.

DEPRECATED

An improved version of this extension was integrated into Wiremock 2.7, so it's no longer needed.

Overview

Build Status Maven Central

wiremock-snapshot is an admin extension for WireMock that adds a new endpoint, /__admin/recordings/snapshot, for creating stub mappings from recorded requests. It's an alternative to the Record and Playback feature that doesn't require restarting the server, and provides more customization options.

WARNING: This is currently alpha. Backwards compatibility is not guaranteed.

Building

Run gradle jar to build the JAR without dependencies or gradle fatJar to build a standalone JAR. These will be placed in build/libs/.

Running

Standalone server:

java -jar build/libs/wiremock-snapshot-standalone-0.3a.jar

With WireMock standalone JAR:

java \
        -cp wiremock-standalone.jar:build/libs/wiremock-snapshot-0.3a.jar \
        com.github.tomakehurst.wiremock.standalone.WireMockServerRunner \
        --extensions="com.github.masonm.wiremock.SnapshotExtension"

Programmatically in Java:

new WireMockServer(wireMockConfig()
    .extensions("com.github.masonm.wiremock.SnapshotExtension"))

Usage

Creating proxy for recording

If you're using this as a replacement for the Record and Playback feature, you'll need to manually create the proxy mapping that's normally done automatically with the --proxy-all option. This can be done with by calling /__admin/mappings with the following stub mapping:

curl -d '{
    "response": {
        "proxyBaseUrl": "http://www.example.com"
    }
}' http://localhost:8080/__admin/mappings

Replace http://www.example.com with the proxy base URL and http://localhost:8080 with the Wiremock base URL.

Calling the Snapshot API

The /__admin/recordings/snapshot endpoint can be accessed via POST and creates stub mappings from the requests and responses in the request journal. It accepts the following options:

  • "filters" - Request patterns and IDs to use for determining which requests for which to create stub mappings.
    • Possible values: Same request patterns accepted by /__admin/requests/find. See Request Matching for details. Also accepts an array of IDs to match against.
    • Default: no filtering.
  • "persist" - If set to true, persist stub mappings to disk. Otherwise, just output
    • Possible values: true, false
    • Default: true
  • "captureHeaders" - Header matchers for including headers in the StubMapping. The request is matched against each matcher, and the associated header is added to the stub mapping if there's a match.
  • "outputFormat" - Determines response body.
    • Possible values: "ids" to return array of stub mapping IDs, "full" to return array of stub mapping objects
    • Default: "full"
  • "repeatsAsScenarios" - Whether to record duplicate requests as scenarios, or just ignore them.
    • Possible values: true to use scenarios for duplicate requests, false to discard them
    • Default: false

Examples

  • Record mappings with defaults: curl -X POST http://localhost:8080/__admin/recordings/snapshot

  • Filter by URL and header values (i.e. only create stub mappings for matching requests) and output array of stub mappings:

      curl -d '{
          "outputFormat": "full",
          "filters": {
              "urlPattern": "/foo/(bar|baz)",
              "headers": {
                  "Content-Type": {
                      "equalTo": "application/json"
                  }
              }
          }
      }' http://localhost:8080/__admin/recordings/snapshot`
    
  • Filter by URL and IDs, and output array of stub mappings:

      curl -d '{
          "outputFormat": "full",
          "filters": {
      "ids": [
          "bff18359-a74e-4c3e-95f0-dab304cd3a5a",
          "e88ab645-69d5-34d1-8e4a-382ad56be0e4"
      ],
              "urlPattern": "/foo"
          }
      }' http://localhost:8080/__admin/recordings/snapshot`
    
  • Always include "Content-Type" header in stub mapping, and include "Accept" header if it's equal to "bar".

       curl -d '{
          "captureHeaders": {
              "url": "/foo",
              "method": "ANY",
              "headers": {
                  "Content-Type": { "anything": true },
                  "Accept": { "equalTo": "Bar" }
               }
          }
       }' http://localhost:8080/__admin/recordings/snapshot`
    
  • Output an array IDs, without persisting.

       curl -d '{
          "persist": false,
          "outputFormat": "ids"
       }' http://localhost:8080/__admin/recordings/snapshot`
    

Todo

Versions

Version
0.3a