Search in sources :

Example 1 with ResponseDefinition

use of com.github.tomakehurst.wiremock.http.ResponseDefinition in project irontest by zheng-wang.

the class IronTestUtils method updateUnmatchedStubRequest.

/**
 * By default, unmatched WireMock stub request (ServeEvent) does not have the actual response headers or response body.
 * This method update the unmatched serveEvent obtained from the WireMockServer by changing its response headers and body to the actual values.
 * @param serveEvent
 * @return the input serveEvent if it was matched;
 *         a new ServeEvent object with all fields same as the input serveEvent, except for the response headers and body, if it was unmatched.
 */
public static ServeEvent updateUnmatchedStubRequest(ServeEvent serveEvent, WireMockServer wireMockServer) {
    if (serveEvent.getWasMatched()) {
        return serveEvent;
    } else {
        PlainTextStubNotMatchedRenderer renderer = (PlainTextStubNotMatchedRenderer) wireMockServer.getOptions().getNotMatchedRenderer();
        ResponseDefinition responseDefinition = renderer.render(wireMockServer, serveEvent.getRequest());
        LoggedResponse response = serveEvent.getResponse();
        com.github.tomakehurst.wiremock.http.HttpHeaders updatedHeaders = responseDefinition.getHeaders();
        // remove the leading \r\n
        String updatedBody = responseDefinition.getBody().substring(2);
        LoggedResponse updatedResponse = new LoggedResponse(response.getStatus(), updatedHeaders, Encoding.encodeBase64(updatedBody.getBytes()), response.getFault(), null);
        ServeEvent updatedServeEvent = new ServeEvent(serveEvent.getId(), serveEvent.getRequest(), serveEvent.getStubMapping(), serveEvent.getResponseDefinition(), updatedResponse, serveEvent.getWasMatched(), serveEvent.getTiming());
        return updatedServeEvent;
    }
}
Also used : PlainTextStubNotMatchedRenderer(com.github.tomakehurst.wiremock.verification.notmatched.PlainTextStubNotMatchedRenderer) LoggedResponse(com.github.tomakehurst.wiremock.http.LoggedResponse) ResponseDefinition(com.github.tomakehurst.wiremock.http.ResponseDefinition) ServeEvent(com.github.tomakehurst.wiremock.stubbing.ServeEvent)

Aggregations

LoggedResponse (com.github.tomakehurst.wiremock.http.LoggedResponse)1 ResponseDefinition (com.github.tomakehurst.wiremock.http.ResponseDefinition)1 ServeEvent (com.github.tomakehurst.wiremock.stubbing.ServeEvent)1 PlainTextStubNotMatchedRenderer (com.github.tomakehurst.wiremock.verification.notmatched.PlainTextStubNotMatchedRenderer)1