【问题标题】:How to check JSON field when send field not match with expect JSON in wiremock?当发送字段与wiremock中的期望JSON不匹配时如何检查JSON字段?
【发布时间】:2021-10-04 15:57:58
【问题描述】:

我有一些关于再次检查 Wiremock 的问题

我想检查 JSON 所有字段

{"petType" : 1, "wildLevel": 40,"petStatus": 1}

如果我发送无效字段,例如

{"petType" : 1, "wildLevel": 40,"patStatus": 1}

{"catType" : 1, "wildLevel": 40,"patStatus": 1}

{"petType" : 1, "wildLevelxx": 40,"patStatus": 1}

,它应该显示无效请求。

这个我可以写的映射JSON

{
    "mappings":[
        {
            "priority":1,
            "request":{
                "method":"PATCH",
                "urlPath":"/pet/status",
                "bodyPatterns":[
                    {
                        "or":[
                            
                        ]
                    }
                ]
            },
            "response":{
                "status":400,
                "transformers":[
                    "response-template"
                ],
                "bodyFileName":"invalid-request.json",
                "headers":{
                    "Content-Type":"application/json"
                }
            }
        },
        {
            "priority":2,
            "request":{
                "method":"PATCH",
                "urlPath":"/pet/status",
                "bodyPatterns":[
                    {
                        "and":[
                            {
                                "matchesJsonPath":"$[?(@.petType == '404040')]"
                            },
                            {
                                "matchesJsonPath":"$.wildLevel "
                            },
                            {
                                "matchesJsonPath":"$.petStatus "
                            }
                        ]
                    }
                ]
            },
            "response":{
                "status":200,
                "transformers":[
                    "response-template"
                ],
                "bodyFileName":"find-success.json",
                "headers":{
                    "Content-Type":"application/json"
                }
            }
        },
        {
            "priority":3,
            "request":{
                "method":"PATCH",
                "urlPath":"/pet/status",
                "bodyPatterns":[
                    {
                        "and":[
                            {
                                "matchesJsonPath":"$.petType "
                            },
                            {
                                "matchesJsonPath":"$.wildLevel "
                            },
                            {
                                "matchesJsonPath":"$.petStatus "
                            }
                        ]
                    }
                ]
            },
            "response":{
                "status":403,
                "transformers":[
                    "response-template"
                ],
                "bodyFileName":"data-not-found.json",
                "headers":{
                    "Content-Type":"application/json"
                }
            }
        }
    ]
}

现在,我不知道如何找到解决方案,请帮助

【问题讨论】:

    标签: json wiremock


    【解决方案1】:

    如果您的请求正文将只包含这三个字段,您可以使用以下组合:

    • equalToJson
    • ignoreExtraElements = false
    • json-Unit Placeholder

    所有这些信息都可以是found on this page

    解决方案看起来像......

    ...
    "bodyPatterns": [
          {
            "equalToJson": "{ \"petType\": \"${json-unit.any-number}\", \"wildLevel\": \"${json-unit.any-number}\", \"petStatus\": \"${json-unit.any-number}\" }",
            "ignoreExtraElements": false
          }
        ]
    ...
    

    我们在这里检查请求正文是否包含所有这三个字段(petType、wildLevel 和 petStatus),并且只包含这些字段,并且所有这些字段的值都是数字。如果不满足这些条件,那么这将不被视为匹配。我会将其设置为更高的优先级 (1),并在此映射不匹配的情况下使用更通用的“不匹配”映射,您会返回错误。

    【讨论】:

      猜你喜欢
      • 2018-09-10
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-10
      相关资源
      最近更新 更多