URL Path Pattern Matching in WireMocks (pattern matching)

非 Y 不嫁゛ 提交于 2019-12-13 16:05:08

问题


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

REQUEST 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"
    }
  }
}

My Get Request for the above wiremock looks something like this

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

Request 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

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

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

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.

any suggestions ?


回答1:


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" in the first stub and

"urlPath": "/api/v1/user/2158634832/ishop/status" in the second

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".




回答2:


A better solution is to use stub priority:

http://wiremock.org/docs/stubbing/#stub-priority

This way you can still use urlPatten

{
    "priority" : 2,
    "request": {
        "method": "GET",
        "urlPattern": "/api/v1/user/2158634832/ishop.*",
        "headers": {
        ...

and:

{
    "priority" : 1,
    "request": {
        "method": "GET",
        "urlPattern": "/api/v1/user/2158634832/ishop/status.*",
        "headers": {
        ...



回答3:


Well i fixed it in the following way

Request 1 changed from

"urlPattern": "/api/v1/user/2158634832/ishop.*",

to

"urlPath": "/api/v1/user/2158634832/ishop",

Request 2 changed from

"urlPattern": "/api/v1/user/2158634832/ishop/status.*",

to

"urlPath": "/api/v1/user/2158634832/ishop/status",


来源:https://stackoverflow.com/questions/44998429/url-path-pattern-matching-in-wiremocks-pattern-matching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!