rascio
rascio

Reputation: 9279

Wiremock match request POST by params

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?

Upvotes: 10

Views: 14465

Answers (4)

de-jcup
de-jcup

Reputation: 1865

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

Upvotes: 5

Harold Castillo
Harold Castillo

Reputation: 2416

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": {
    ....
  }
}

Upvotes: 9

RadekJ
RadekJ

Reputation: 3043

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($|&.*)"
     }
   ]
},

Upvotes: 3

Stef Heyenrath
Stef Heyenrath

Reputation: 9830

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

Upvotes: 1

Related Questions