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

后端 未结 6 850
盖世英雄少女心
盖世英雄少女心 2021-02-12 20:10

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

相关标签:
6条回答
  • 2021-02-12 20:41

    I've investigated this a fair bit and the answer is no (as of today, anyway).

    You need to set up a specific (static) response for each request that requires a unique response.

    0 讨论(0)
  • 2021-02-12 20:42

    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.

    0 讨论(0)
  • 2021-02-12 20:42

    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.

    0 讨论(0)
  • 2021-02-12 20:51

    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/

    0 讨论(0)
  • 2021-02-12 20:53

    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

    0 讨论(0)
  • 2021-02-12 20:57

    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)));
    
    0 讨论(0)
提交回复
热议问题