Mocking multi-part form request

1,505 views
Skip to first unread message

Shankar Raman

unread,
Sep 4, 2015, 10:49:52 AM9/4/15
to wiremock-user
Hi Tom,
I'd like to mock a multipart form request using wiremock. I am running wiremock as a standalone process. Where I am getting lost, is trying to make the request generic using JSONCompareMode, however I am not having much of a luck. Is it possible to mock mulitpart form requests?
Here's a sample request
"request" : {
"url" : "/some-end-point",
"method" : "POST",
"bodyPatterns" : [ {
"equalTo" : "--d62b94e3-c5ae-4b22-a0af-3097bb69e635\r\nContent-Disposition: form-data; name=\"item\"; filename=\"item.json\"\r\nContent-Type: application/json\r\nContent-Length: 940\r\nContent-Transfer-Encoding: binary\r\n\r\n{\"key1\":\"value1\",\"key2\":\"value2\"}\r\n--d62b94e3-c5ae-4b22-a0af-3097bb69e635--\r\n"
} ]
}

Shankar Raman

unread,
Sep 10, 2015, 8:50:41 AM9/10/15
to wiremock-user
Any update?

Zaid Ibrahim

unread,
Sep 10, 2015, 12:59:54 PM9/10/15
to wiremock-user
I'm doing things a bit differently, but also more or less in the same vein.

I'm also running wire mock as a stand-alone process, and encountered some difficulties which are holding me back. I'm trying to record my interaction with websites on multi-part forms using wire mock. When the link that separates parts of the forms from one another is a relative URL, things work ok as the wire mock server still serves as a recording proxy towards the target domain. However, that all breaks with absolute links, where the browser instead goes directly to the target domain instead of aliasing through wire mock.

So, instead of going somewhere like this, where interactions are recorded:

localhost:8080/catsAreCool

I'd get redirected to this, where the interactions are NOT recorded:


I can't really blame wiremock or the browser in this case; they're both doing the correct behavior. The browser goes straight to absolute urls, as it should, and wire mock records, as it also should. I can get around this by making a script or whatever that would just convert absolute links to relative ones, but there are holes in that line of thinking; for one, what if I can only traverse forms via a button, which doesn't have a link attribute in it, but instead references some javascript that in turn links you to the next form?

If I had to take a guess, I'd say that wire mock is more intended for use via curl than it is by using a browser. 

Tom Akehurst

unread,
Sep 10, 2015, 3:00:22 PM9/10/15
to wiremock-user
The problem isn't WireMock per-se. Any proxy will pass 3xx responses back to your user agent, and if your user agent is configured to follow them it will.

Sites that redirect to absolute URLs are a problem in that they'll always take your browser back to them, rather than you staying under the proxy domain. There's a feature coming in WireMock 2.x.x which will help with this, in that it'll allow dynamic transformations of the proxy response, meaning redirects can be rewritten to make them relative to the proxy domain.

Tom Akehurst

unread,
Sep 10, 2015, 3:02:53 PM9/10/15
to wiremock-user
I haven't ever needed to match on multipart requests, so I can't talk from experience.

However, since the body contains more than just JSON, you won't be able use JSONPath or JSON equality matching as WireMock isn't clever enough to discern the different parts and parse them separately.

You can still use bodyPatterns with equalTo or matches (i.e. regexes) though. I'd suggest matching on a regex and keeping it fairly limited in scope e.g. pick out a GUID you know is unique for that request.
Reply all
Reply to author
Forward
0 new messages