8

I have a simple POST request sending params using application/x-www-form-urlencoded encoding.

Looking in the wiremock docs I can't find a way to match the request by the params values, something like the querystring match I mean.

Furthermore it seems also impossible to contains for the body, nor to match the entire body in clear (just as base64).

Is there a way to match this kind of requests?

rascio
  • 8,968
  • 19
  • 68
  • 108

4 Answers4

8

Another option that I found was to use contains for Stubbing Content-Type: application/x-www-form-urlencoded

{
  "request": {
    "method": "POST",
    "url": "/oauth/token",
    "basicAuthCredentials": {
      ...
    },
    "bodyPatterns": [
      {
        "contains": "username=someuser"
      }
    ]
  },
  "response": {
    ....
  }
}
Harold Castillo
  • 2,006
  • 19
  • 23
3

With classic wiremock you can use bodyPatterns' matchers and regular expressions:

for example:

...
"request": {
   "method": "POST",
   "url": "/api/v1/auth/login",
   "bodyPatterns": [
     {
       "matches": "(.*&|^)username=test($|&.*)"
     },
     {
       "matches": "(.*&|^)password=123($|&.*)"
     }
   ]
},
RadekJ
  • 2,835
  • 1
  • 19
  • 25
3

I had a similar problem - I wanted to check the exact parameters , but without patterm magic (so easier to maintain). As a workaround, I created a helper class :

import java.util.Iterator;
import java.util.LinkedHashMap;

public class WireMockUtil {
    public static String toFormUrlEncoded(LinkedHashMap<String, String> map) {
        if (map == null) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        Iterator<String> it = map.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            String value = map.get(key);
            appendFormUrlEncoded(key,value,sb);
            if (it.hasNext()) {
                sb.append('&');
            }
        }
        return sb.toString();
    }

    public static String toFormUrlEncoded(String key, String value) {
        StringBuilder sb = new StringBuilder();
        appendFormUrlEncoded(key, value,sb);
        return sb.toString();
    }

    public static void appendFormUrlEncoded(String key, String value, StringBuilder sb) {
        sb.append(key).append('=');
        if (value != null) {
            sb.append(value);
        }
    }
}

Inside the Wiremock test you can use it via:

LinkedHashMap<String, String> map = new LinkedHashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");

...

withRequestBody(equalTo(WireMockUtil.toFormUrlEncoded(map))).

Or check only dedicated parts by containing:

withRequestBody(containing(WireMockUtil.toFormUrlEncoded("key","value1"))).
de-jcup
  • 1,402
  • 12
  • 27
  • A problem with this, is that it depends on the exact order of arguments, so may not work for all use cases. – Anders Kreinøe May 08 '23 at 05:59
  • If this is a problem for your usecases, you could use a tree map instead of the linked hash map - so the ordering is always the same. – de-jcup May 08 '23 at 06:46
  • Dont think that would help, as it would still matching on the exact string with `withRequestBody(equalTo(WireMockUtil.toFormUrlEncoded(map)))` – Anders Kreinøe May 08 '23 at 14:24
  • You are right, the tree map was just an idea and would not solve your issue. But my solution does work well in our own wire mock tests where I had the problems mentioned in this Stackoverflow question. To see my usage please look at https://github.com/de-jcup/sechub/blob/ae7197110ac226f9f92c46cde815fb902761ab8a/sechub-adapter-checkmarx/src/test/java/com/mercedesbenz/sechub/adapter/checkmarx/CheckmarxAdapterV1WireMockTest.java#L474-L494 – de-jcup May 10 '23 at 08:53
1

You could try https://github.com/WireMock-Net/WireMock.Net

Matching query parameters and body can be done with this example json:

{
    "Guid": "dae02a0d-8a33-46ed-aab0-afbecc8643e3",
    "Request": {
      "Url": "/testabc",
      "Methods": [
        "put"
      ],
      "Params": [
        {
          "Name": "start",
          "Values": [ "1000", "1001" ]
        },
        {
          "Name": "end",
          "Values": [ "42" ]
        }
      ],
       "Body": {
        "Matcher": {
          "Name": "WildcardMatcher",
          "Pattern": "test*test"
        }
      }
    }
}
Stef Heyenrath
  • 9,335
  • 12
  • 66
  • 121