Anon21
Anon21

Reputation: 3073

Using wiremock, can I return a body that is dependent on the post request

I am trying to test an openid provider class. The openid consumer class is making an http request. I am mocking the response to this request using wiremock. I am trying to mock a valid openid response. However, the valid response depends on the request parameters. Using wiremock, can I set up a mock request where the body of the response is dependent on the request parameters?

Upvotes: 20

Views: 43977

Answers (5)

Pankaj Jaiswal
Pankaj Jaiswal

Reputation: 739

Yes it is possible to create a stub with the request matching in wiremock. Following attributes are supported by for Request matching request.

  • URL
  • HTTP Method
  • Query parameters
  • Headers
  • Basic authentication (a special case of header matching)
  • Cookies
  • Request body
  • Multipart/form-data

In your scenario if you want to apply matching on the values in the request body you can use the below approach for generating stub for it.

{
  "request": {
    ...
    "bodyPatterns" : [ {
      "equalToJson" : "{ \"total_results\": 4 }"
    } ]
    ...
  },
  ...
}

Follow the link for more details: http://wiremock.org/docs/request-matching/

Upvotes: 0

Hung Tran
Hung Tran

Reputation: 73

Wiremock supports extensions that you can write yourself that act as a middleware used to intercept the request and response bodies so you can format it however you like. It's very flexible and allows you to make up new response bodies dynamically or even no response at all.

As an example, we wrote an extension for this at Opentable and open sourced it on Maven Central. It allows you treat the json attributes as variables and interpolate them into your response body. Check it out. Let us know how it goes or if you have any questions. https://github.com/opentable/wiremock-body-transformer

Upvotes: 5

PiersyP
PiersyP

Reputation: 5233

This is possible, you just have to make use of a ResponseTansformer. In the below example code the responseDefinition is determined by the stubbing given below. Here I mock an encoding service by simply returning the body bytes back to the caller. Although in the transformer I am free to return whatever I like based on the contents of the request.

int port = 8080;
WireMockServer wireMockServer = new WireMockServer(new WireMockConfiguration().port(port).extensions(new ResponseTransformer() {
    @Override
    public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition, FileSource files) {
        return new ResponseDefinitionBuilder().like(responseDefinition)
                .withBody(request.getBodyAsString().getBytes())
                .build();
    }

    @Override
    public String name() {
        return "request body returning request transformer";
    }
}));
wireMockServer.start();
WireMock.configureFor("localhost", port);

stubFor(post(urlEqualTo("/encode"))
        .willReturn(aResponse()
                .withHeader("Content-Type", "application/octet-stream")
                .withStatus(200)));

stubFor(post(urlEqualTo("/decode"))
        .willReturn(aResponse()
                .withHeader("Content-Type", "application/octet-stream")
                .withStatus(200)));

Upvotes: 15

user2944810
user2944810

Reputation: 21

As far as I know and my experience with WireMock, no.

You can't parameterize a response with arguments passed through request. The best you can do is use matchers to make your mocked server respond accordingly.

I would recommend you making some unit or integration tests with plain jUnit in order to test requests/responses in such cases. They should be quicker if you want to test that receipt requests are responding correctly. I see WireMock as an alternative to do acceptance test, to ensure that your interface with other REST services are not getting broken.

Upvotes: 2

dkatzel
dkatzel

Reputation: 31648

I've never used wiremock. But according to their online documentation you can write a mock that matches URL and Request body parameters. So you should be able to return different mocks depending on the parameters in either the URL itself or embedded in the request body.

Upvotes: 0

Related Questions