Anshu Patel
Anshu Patel

Reputation: 62

Multiple matches for request body in wiremock

Recently learned request matching in wiremock (http://wiremock.org/docs/request-matching/). Curious about what happens when a request's body matches more than one mappings (defined for same url path with different conditions and returns different json response)?

Upvotes: 1

Views: 7281

Answers (1)

agoff
agoff

Reputation: 7125

Technically, WireMock won't ever match twice -- once it finds a singular match, it will return that match. Based on my own testing with using separate mapping files, this usually is the most recently added mapping (I don't know how this works with creating the stubs programmatically, but my guess is that the most recently added stub would be matched and returned).

To avoid this sort of ambiguity, there are a few strategies you can employ, but my personal favorite is to use the priority field along with specific and general mapping.

{
  "priority": 1,
  "request": {
    "url": "/test",
    "queryParameters": {
      "search_term": {
        "equalTo": "WireMock"
      }
    }
  },
  "response": {
    "status": 201
  }
}
{
  "priority": 10,
  "request": {
    "url": "/test",
    "queryParameters": {
      "search_term": {
        "matches": "*"
      }
    }
  },
  "response": {
    "status": 204
  }
}

More information about priority can be found here.

I'd also challenge that you shouldn't have two specific mappings that would both be matched -- the matcher should differ enough to separate the two. If you do need two identical matches, in order to simulate data changing or some other workflow, you can use scenarios to achieve this.

Upvotes: 4

Related Questions