javamockingwiremockwiremock-standalonewiremock-record

Ignoring certain elements in request during wiremock record mappings


I am using wiremock to record mappings. I am using proxyBaseUrl, so that when mapping is not there it gets routed to proxy and the request and response is recorded. In my request there are a few timestamp related request object items I want the mapping to ignore. This way when the mapping is created, I already have timestamp wild carded like \"timestamp\": \"${json-unit.any-string}\". My tests create different timestamp, and I want wiremock to ignore those request objects when the mapping is created

When I run tests, there are 100s of mappings created and going through them 1 by 1 and updating them will be painful. So I want to train record-mappings in advance to ignore certain request objects. How can I do it?

{
  "priority": 1000000,
  "request": {
    "method": "POST",
    "url": "/v1/computeserv/execution",
  },
  "response": {
    "proxyBaseUrl": "https://actual.computeserv.com:12345"
  }
}

Solution

  • You'll need to write a StubMappingTransformer to handle automatically modifying your saved requests. You can read more about that in the Transforming Generated Stubs documentation on WireMock.

    Additionally, many modern IDEs contain some global find-and-replace functionality, including searching by regex. If you don't anticipate having to constantly record and replace stubs, you might have more success by simply running the recording, generating your stubs with the specific timestamps, and then using the global find-and-replace functionality in your IDE. For example, here's the documentation on VSCode's Search and Replace.

    Depends on what regex matching your IDE uses, but I'd imagine it'd be something like

    \"timestamp\": \"(.*)\"