Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

junit

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?

like image 380
Anon21 Avatar asked Sep 02 '13 22:09

Anon21


People also ask

What is WireMock rule?

WireMock is a library for stubbing and mocking web services. It constructs an HTTP server that we can connect to as we would to an actual web service. When a WireMock server is in action, we can set up expectations, call the service and then verify its behaviors.

How do you pass a body request in WireMock?

Request Body:Click on the Send button now and in the Response, you can see the following Response Body with the status code 201 Created. And you are going to get the following response in the Response body.

What is the difference between WireMock and Mockito?

Wiremock provides a simulator for HTTP-based APIs while Mockito provides the mock implementation of the method/object.

What is stubbing in WireMock?

WireMock is an HTTP stubbing tool. This means that it can be configured to return specific canned responses depending on the request. This can be a simple as just matching the URL, right up to a combination of URL, header and body matches using regexes, JSONPath, XPath and others.


2 Answers

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)));
like image 144
PiersyP Avatar answered Oct 12 '22 19:10

PiersyP


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

like image 24
Hung Tran Avatar answered Oct 12 '22 20:10

Hung Tran