Skip to content

Request Matching

Stef Heyenrath edited this page Aug 10, 2023 · 47 revisions

1️⃣ Request Matching

WireMock.Net supports matching of requests to stubs and verification queries using the following parts:

Generic information on matchers:

Most matchers have 2 extra properties:

  • IgnoreCase = define that the matcher should match ignoring the case
  • RejectOnMatch = define that when the matcher does match successfully, this should be counted as a invalid (non-matching) match

Example Matchings

Path

C# example

server
  .Given(Request
    .Create()
      .WithPath("/test")

JSON example

{
  "Request": {
    "Path": {
      "Matchers": [
        {
          "Name": "WildcardMatcher",
          "Pattern": "/path",
          "IgnoreCase": true
        }
      ]
    }
  }
}

Url

C# example

server
  .Given(Request
    .Create()
      .WithUrl("https://localhost/test")

JSON example

{
  "Request": {
    "Url": {
      "Matchers": [
        {
          "Name": "RegexMatcher",
          "Pattern": "/clients[?]",
          "IgnoreCase": true
        }
      ]
    }
  }
}

Query Parameters

C# example

server
  .Given(Request
    .Create()
      .WithParam("search", "abc")

JSON example

{
     "Request": {
        "Params": [
            {
                "Name": "search",
                "Matchers": [
                    {
                        "Name": "ExactMatcher",
                        "Pattern": "abc"
                    }
                ]
            }
        ]
    }
}

Headers

C#

// todo

JSON

{
  "Request": {
    "Headers": [
      {
        "Name": "api-key",
        "Matchers": [
          {
            "Name": "WildcardMatcher",
            "Pattern": "abc*"
            "IgnoreCase": true
          }
        ]
      }
    ]
  }
}

Note that when you want to match on a missing header, you need to use this mapping:

{
  "Request": {
    "Headers": [
    {
      "Name": "api-key",
      "IgnoreCase": true,
      "RejectOnMatch": true
    }
  ]
}

This means that when the header-key api-key (ignoring the casing) is missing the header mapping will match because RejectOnMatch is true.

2️⃣ Matchers

Content moved to Request Matchers.

Clone this wiki locally