🐺

WireMockでネストしたRequestBodyのmatch

2025/01/31に公開

はじめに

wiremockでネストしたRequestBodyに対してmatchさせる方法を探しました。

結論

wiremockmatchesJsonPathmatchさせる。

{
  "request": {
    ...
    "bodyPatterns" : [ {
      "matchesJsonPath" : {
         "expression": "$..todoItem",
         "contains": "wash"
      }
    } ]
    ...
  },
  ...
}

https://wiremock.org/docs/request-matching/#nested-value-matching

matchesJsonPathは別のmatcherと組み合わせることが可能です。

部分文字列はJsonPathcontainsにより実現しています。

expression

matchさせたいKey名をJsonPathで指定する

ネストした構造のJsonPath指定

valueに対してmatchする

{
    "depth1": [
    {
      "depth2": [
        {
          "depth3": [
            {
              "value": "hoge"
            }
          ]
        }
      ]
    },
    {
      "depth2": [
        {
          "depth3": [
            {
              "value": "fuga"
            }
          ]
        }
      ]
    }
  ]
}

matchesJsonPath

{
    "request": {
    ...
        "bodyPatterns": [{
            "matchesJsonPath": {
                "expression": "$..[?(@.value)]",
                "contains": "hoge"
            }
        }]
        ...
    }
    ...
}

JsonPath構文は下記ドキュメントを参考にしています。

https://www.rfc-editor.org/rfc/rfc9535.html#name-summary

contains

Substring(部分文字列)で評価される

https://wiremock.org/docs/request-matching/#substring-contains

Discussion