wiremock / wiremock

A tool for mocking HTTP services
https://wiremock.org/
Apache License 2.0
6.37k stars 1.44k forks source link

Request was not matched for GET URL with query parameters #2841

Closed 7robertodantas closed 2 months ago

7robertodantas commented 2 months ago

Proposal

Hi, I'm trying to stub a GET endpoint that expects a few query parameters.

For some reason, WireMock is not matching the URL, even though I have specified the query parameters.

Initially, I tried using equalTo for the query parameters, but since that didn’t work, I tried using contains.

However, it is still not matching.

Reproduction steps

I have started a new instance of WireMock.

docker run -it --rm \
  -p 8080:8080 \
  --name wiremock \
  wiremock/wiremock:3.9.1

Then, I created a new stub mapping as shown below

curl --location 'localhost:8080/__admin/mappings' --header 'Content-Type: application/json' --data '{
  "request" : {
    "url" : "/test",
    "method" : "GET",
    "queryParameters" : {
      "a" : {
        "contains" : "wiremock"
      },
      "b" : {
        "contains" : "all"
      },
      "c" : {
        "contains" : "test"
      }
    }
  },
  "response" : {
    "status" : 200,
    "jsonBody" : []
  }
}'

Then, I attempted to hit the stubbed endpoint

curl --location 'localhost:8080/test?a=wiremock&b=all&c=test'

It returned the following response.


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

-----------------------------------------------------------------------------------------------------------------------
| Closest stub                                             | Request                                                  |
-----------------------------------------------------------------------------------------------------------------------
                                                           |
GET                                                        | GET
/test                                                      | /test?a=wiremock&b=all&c=test                       <<<<< URL does not match
                                                           |
Query: a [contains] wiremock                               | a: wiremock
Query: b [contains] all                                    | b: all
Query: c [contains] test                                   | c: test
                                                           |
                                                           |
-----------------------------------------------------------------------------------------------------------------------

References

No response

leeturner commented 2 months ago

When you specify url in the request, you are telling WireMock to match the path and query exactly. Therefore what you are seeing is the expected behaviour because /test?a=wiremock&b=all&c=test does not equal /test

If you change url to urlPath you should see the behavior you are looking for

7robertodantas commented 2 months ago

Ahh @leeturner, got it! Thanks! It worked by replacing the url to urlPath.

Closing the issue now.