3

I want to understand how priorities work. More specifically, what is the expected output of setting priorities to stub. There's limited documentation on this and the ones available doesn't really explain what the output would look like so I'm unable to verify if I have implemented it correctly.

This is my current code:

    stubFor(post(urlMatching("/user/test\\?(and)\\=(que).*")).atPriority(1)
    .willReturn(aResponse()
              .withStatus(200)
                      .withHeader("Content-Type", "text/plain")
                      .withBody("This stub is testing for Title ")
              )
        );   

   System.out.println("About to execute the second stub");
            stubFor(post(urlMatching("/user/test\\?(and)\\=(que).*")).atPriority(2)
    .willReturn(aResponse()
              .withStatus(200)
                      .withHeader("Content-Type", "text/plain")
                      .withBody("This stub is testing Author ID ")
              )
        );

            System.out.println("Second stub executed");

I'm sending the following request from SOAPUI:

/user/test?and=query 

Therefore both stubs should be executed and I should receive two responses correct?

I'm currently receiving only one response and that is from the stub that has priority 1. I'm not getting any response from the stub that has priority 2/

Can someone please help me on this?

Matt
  • 161
  • 1
  • 6
  • 17

3 Answers3

8

What exactly do you want to achieve? You normally have Wiremock configurations which have different request parameters or overlapping. In your case they are exactly the same. In which case should be the first and which case should the second be shown?

Wiremock will always return only exactly one answer. This answer is normally determined by evaluation of the request parameters you defined in your Wiremock configuration. please refer to this description on how Wiremock and priorities work: Wiremock Stubbing and priorities

In case there are overlapping parameters, Wiremock will choose the configuration you added most recently (in your case the second). Or you can guide Wiremock by setting priorities. A priority which is lower will be used in preference.

Normally you have a more general case (with less request parameters - as catch-up) and a more specific case. The first will get a higher prio (e.g. 9) and the latter a lower one (e.q. 5). So the latter will be choosen in preference if request parameters match and in other cases the second one.

monsIgnore
  • 91
  • 1
  • 4
3

As @monsIgnore stated, for overlapping parameters the most recently added mapping that matches those parameters will be chosen.

When I initially looked at the request matching in wiremock I thought that the most exact match would be chosen.

By 'most exact' I mean the one that matched the most number of elements in the request. For example, given these two mappings (added in this order):

Mapping 1

  "request" : {
    "url" : "/oauth2/rest/consent",
    "method" : "GET",
    "headers" : {
      "Cookie" : {
        "equalTo" : "OAM_ID=VERSION_5"
      }
    }
  } 

Mapping 2

 "request" : {
    "url" : "/oauth2/rest/consent",
    "method" : "GET"
  }

If a request is received for url /oauth2/rest/consent and an OAM_ID cookie of VERSION_5 then the mapping that matches the most elements is Mapping 1.

However Mapping 2 is chosen since it matches and it was the most recently added.

In this case by adding priorities to the Mappings you can ensure that the request with the cookie is matched against Mapping 1.

Priorities are necessary because it's not always obvious what the most exact match is. For example if there was a third mapping:

Mapping 3

  "request" : {
    "url" : "/oauth2/rest/consent",
    "method" : "GET",
    "headers" : {
      "Authorization" : {
        "equalTo" : "Basic dXNlcjpwYXNzd29yZA=="
      }
    },
  } 

If a request arrived that had the cookie from Mapping 1 and the Authorization header from Mapping 3, then it's not possible to determine the most exact match for the request.

They both match 3 elements each. This is where priorities come in.

Joman68
  • 2,248
  • 3
  • 34
  • 36
2

Setting stub priority is supported in WireMock.

By default, WireMock will use the most recently added matching stub to satisfy the request.

You can add priority in Java code by using atPriority:

stubFor(get(urlMatching("/api/.*")).atPriority(5)

or in JSON by adding:

"priority": 1

Read more on this here

Magnus Reftel
  • 967
  • 6
  • 19
Saikat
  • 14,222
  • 20
  • 104
  • 125