WireMocks中的URL路径模式匹配(模式匹配) [英] URL Path Pattern Matching in WireMocks (pattern matching)

查看:1234
本文介绍了WireMocks中的URL路径模式匹配(模式匹配)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Wiremock进行REST API测试,并且面临以下问题. 我创建了以下2个json请求.

I am using wiremock for REST API testing and I am facing the below problem. I have the following 2 json requests created.

请求1:

{
  "request": {
    "method": "GET",
    "urlPattern": "/api/v1/user/2158634832/ishop.*",
    "headers": {
      "Accept": {
        "contains": "application/json"
      },
      "X-RequestId": {
        "matches": ".*"
      },
      "X-SecurityToken": {
        "matches": "valid_secure_token"
      }
    }
  },
  "response": {
    "status": 200,
    "jsonBody": {
      "user": {
        "ID": "5cc1b131-8858-4870-a7ce-fd37aa805ebf",
        "info": [
          {
            "kid": "2f0ad36a-9f23-427c-9a15-16d240385b87",
            "cid": 10962435,
            "pid": "V",
            "contactid": 11812026,
            "created": "2017-04-26T00:08:25.926",
            "lastModified": "2017-04-27T00:01:45.047",
          }
        ]
      }
    },
    "headers": {
      "Content-Type": "application/json"
    }
  }
}

我对上述Wiremock的获取请求看起来像这样

My Get Request for the above wiremock looks something like this

**/api/v1/user/2158634832/ishop?ID=9879cfde-e353-4722-a91e-f22d29d8195c**

请求2:

{
  "request": {
    "method": "GET",
    "urlPattern": "/api/v1/user/2158634832/ishop/status.*",
    "headers": {
      "Accept": {
        "contains": "application/json"
      },
      "X-RequestId": {
        "matches": ".*"
      },
      "X-SecurityToken": {
        "matches": "valid_secure_token"
      }
    }
  },
  "response": {
    "status": 200,
    "jsonBody": {
      "userstatus": {
        "id": "5cc1b131-8858-4870-a7ce-fd37aa805ebf",
        "type": "ishop",
        "status" : "active",
        "lastModifiedTime": "2017-04-27T00:01:45.047",
        "allowedChange": "true"
      }
    },
    "headers": {
      "Content-Type": "application/json"
    }
  }
}

我对上述内容的获取请求看起来像这样

my Get request for the above looks something like this

/api/v1/user/2158634832/ishop/status?ID=9879cfde-e353-4722-a91e-f22d29d8195c

/api/v1/user/2158634832/ishop/status?ID=9879cfde-e353-4722-a91e-f22d29d8195c

这里的问题是,有时由于请求1和请求2的URL模式相似,有时会发生

the problem here is that at times due to the similar URL Pattern for Request 1 and Request 2 what is happening is at times

当我提交请求1的请求时,我得到的是请求2的响应.

When I submit a request for Request 1, I am getting the response which is of Request 2.

任何人都可以建议我一种解决方法,该如何解决呢?我希望与火柴和urlPattern有关,并且我可以在第一个请求中消除状态调用,但是我不确定该怎么做.

Can anyone suggest me a workaround how can i fix this ? I am hoping something to do with matches and urlPattern and somehow i can eleminate the status call in the first request i can do it, but not sure how.

有什么建议吗?

推荐答案

由于您的一个请求URL包含另一个URL,因此匹配较短的正则表达式也将匹配较长的正则表达式.我怀疑这就是为什么当您更喜欢较长的那一种时,会匹配较短的那一种的原因.

Since one of your request URLs contains the other, a regex matching the shorter one will also match the longer one. I suspect this is why the shorter one is being matched when you'd prefer the longer one.

我建议更具体地说明您的网址匹配项,例如

I suggest being more specific with your URL matches e.g.

"urlPath": "/api/v1/user/2158634832/ishop"在第一个存根中,并且

"urlPath": "/api/v1/user/2158634832/ishop" in the first stub and

"urlPath": "/api/v1/user/2158634832/ishop/status"在第二个

即使您未指定ID查询参数,这些内容仍将匹配. WireMock将多余的标头或没有匹配参数的查询参数视为无关紧要,仍然匹配".

These will still match despite you not specifying the ID query param. WireMock treats extra headers or query parameters without match parameters as "don't care, match anyway".

这篇关于WireMocks中的URL路径模式匹配(模式匹配)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆