Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wiremock java api - stub request body in form of form-data (not string, nor json)

Tags:

java

wiremock

As the title says I have to stub a post request with request body in the form of form data. No json, string, or xml. I am using junit and wiremock's java api.

I am doing something like the following:

 wireMockServer.stubFor(post(urlEqualTo(MY_URI))
               .withRequestBody(
          // how do I stub a body in form data format??                  
          ).willReturn(aResponse().withStatus(200).withHeader("content-type", "application/json").withBody(expectedBody))
        );

any ideas of what to put in the code instead of the comment?

Thank you!

like image 656
Yorgos Lamprakis Avatar asked Jan 25 '23 21:01

Yorgos Lamprakis


1 Answers

There isn't a specific form matcher in WireMock right now (there should be and I keep meaning to work on it). However, you can do something like this:

wireMockServer.stubFor(post(MY_URI)
            .withRequestBody(containing("key1=value1"))
            .withRequestBody(containing("key2=value2"))
            .willReturn(okJson(expectedResponse));

Note, you'll need to URL encode value1, value2 etc. if there are any metacharacters in there.

like image 93
Tom Avatar answered Jan 30 '23 05:01

Tom