如何使用wiremock(使用JSON)验证请求中是否不包含参数

问题描述 投票:0回答:1

我正在为需要返回错误场景的 /token 端点实现模拟。

{
  "mappings": [
    {
      "name": "Mock error 400: 'code' is not provided",
      "requet": {
        "method": "POST",
        "url": "/as/token.oauth2",
        "bodyPatterns": [
          {
            "contains":"grant_type=client_credentials",
            "notContain":"code="//how to make this??
          }
        ]
      },
      "response": {
        "bodyFileName": "token-400-response.json",
        "status": 400
      }
    }
  ]
}

我不知道这是否重要,但这些参数是使用以下方式传递的:

x-www-form-urlencoded

场景: 如果“grant_type”参数等于authorization_code并且“code”参数不存在,则必须以以下格式返回错误模拟:

我尝试的是这样制作 bodyPatterns:

"bodyPatterns": [
          {
            "matches": {
              "grant_type": "authorization_code",
              "code": "^$"
            }
          }
        ]

但是我收到以下错误:

matches operand must be a non-null string

wiremock
1个回答
0
投票

bodyPatterns
需要的数组具有隐式 AND 连词,这意味着数组中的每个 object 都是必须满足的条件。因此,在本例中,我们将使用一个对象
contains
和一个
doesNotContain

"bodyPatterns": [
      {
        "contains": "grant_type=authorization_code"
      },
      {
        "doesNotContain": "code="
      }
    ]

如果通过查询参数传递值,我们将需要使用

queryParameters
字段,它也是隐式的 AND 连词。为了指定缺少
code
查询参数,我们可以使用
"absent": true

"queryParameters": {
      "grant_type": {
        "equalTo": "authorization_code"
      },
      "code": {
        "absent": true
      }
    }
© www.soinside.com 2019 - 2024. All rights reserved.