1
votes

I am using spock and wiremock for functional testing in a Spring Boot 2.1.5 application. The incoming url is:

/mockedserver/v1/Items('1010873195')/Products?$format=json

yes, its a pretty weird url, but I can't do anything about that.

I have set up a path matcher in JSON with:

{
  "request": {
    "method": "GET",
    "url": "/mockedserver/v1/Items('1010873195')/Products.*",
    "queryParams" : {
      "$format": "json"
    },
    "headers": {
      "x-csrf-token": "",
      "Accept": "application/json",
      "Content-Type": "application/json"
    }
  },
  "response": {
    "headers": {
      "Content-Type": "application/json"
    },
    "status": 200,
    "bodyFileName": "/../responses/mockedserver/get-products/1010873195-success-200.json"
  }
}

As you might be able to see, I modelled the strange looking request parameter in the "queryParams" block. This didn't work, so I then put the regex, .*, on the end of the url.

Both gave me the result:

                                               Request was not matched
                                               =======================

-----------------------------------------------------------------------------------------------------------------------
| Closest stub                                             | Request                                                  |
-----------------------------------------------------------------------------------------------------------------------
                                                           |
GET                                                        | GET
/mockedserver/v1/Items('1010873195')/Produ                 | /mockedserver/v1/Items('1010873195')/Produ<<<<< URL does not match
cts                                                        | cts?$format=json
                                                           |
                                                           |
-----------------------------------------------------------------------------------------------------------------------

As you can see, it matches, except the param part at the end, basically, I don't care about the param, so I was hoping to just wildcard it out.

One possibility is that the $ sign is making a mess of things, should I be escaping it instead? if so, how?

The other is that its the question mark, ?. I have tried escapting that with \? and \\?, both of which were unsuccessful.

Just concerning the part of the request block that looks important here:

    "method": "GET",
    "url": "/mockedserver/v1/Items('1010873195')/Products.*",
    "queryParams" : {
      "$format": "json"
    },

I have seen it said elsewhere that if you use urlPath in place of url that WireMock treats extra headers or query parameters without match parameters as "don't care, match anyway". But this doesn't seem to be the case here as the error output shows.

FURTHER DISCUSSION I have since tried various combinations putting urlPath, urlPattern and even urlPathPattern as suggested, in place of url. All of these result in:

                                               Request was not matched
                                               =======================

-----------------------------------------------------------------------------------------------------------------------
| Closest stub                                             | Request                                                  |
-----------------------------------------------------------------------------------------------------------------------
                                                           |
GET                                                        | GET
                                                           | /mockedserver/v1/Items('1010873195')/Produ<<<<< URL does not match. URLs must start with a /
                                                           | cts?$format=json
                                                           |
                                                           |
-----------------------------------------------------------------------------------------------------------------------

I have also tried this with and without the queryParams block, just in case this contributed to the matching, but it does not seem to have any effect. I am beginning to wonder if there is some other flag i need to set in the stubFor call?

2

2 Answers

2
votes

Fixed: Basically, you need urlPath, no queryParams, and a string-based regex pattern, which I solved with:

/^\\/mockedserver\\/v1\\/Items\('[0-9]{10}'\)\\/Products/

Basically, it came down to the right combination of things.

1
votes

I believe that you need to try "urlPathPattern" or "urlPath" instead of "url" in your mapping as the following:

"urlPathPattern": "/mockedserver/v1/Items('1010873195')/Products.*"