Search in sources :

Example 1 with UrlPattern

use of com.github.tomakehurst.wiremock.matching.UrlPattern in project azure-sdk-for-java by Azure.

the class MockIntegrationTestBase method registerStub.

private void registerStub(String incomingMethod, String incomingUrl) throws Exception {
    int index = 0;
    incomingUrl = removeHost(incomingUrl);
    for (NetworkCallRecord record : testRecord.networkCallRecords) {
        if (record.Method.equalsIgnoreCase(incomingMethod) && removeHost(record.Uri).equalsIgnoreCase(incomingUrl)) {
            break;
        }
        index++;
    }
    if (index >= testRecord.networkCallRecords.size()) {
        System.out.println("NOT FOUND - " + incomingMethod + " " + incomingUrl);
        System.out.println("Remaining records " + testRecord.networkCallRecords.size());
        return;
    }
    NetworkCallRecord networkCallRecord = testRecord.networkCallRecords.remove(index);
    String url = removeHost(networkCallRecord.Uri);
    UrlPattern urlPattern = urlEqualTo(url);
    String method = networkCallRecord.Method;
    MappingBuilder mBuilder;
    if (method.equals("GET")) {
        mBuilder = get(urlPattern);
    } else if (method.equals("POST")) {
        mBuilder = post(urlPattern);
    } else if (method.equals("PUT")) {
        mBuilder = put(urlPattern);
    } else if (method.equals("DELETE")) {
        mBuilder = delete(urlPattern);
    } else if (method.equals("PATCH")) {
        mBuilder = patch(urlPattern);
    } else if (method.equals("HEAD")) {
        mBuilder = head(urlPattern);
    } else {
        throw new Exception("Invalid HTTP method.");
    }
    ResponseDefinitionBuilder rBuilder = aResponse().withStatus(Integer.parseInt(networkCallRecord.Response.get("StatusCode")));
    for (Entry<String, String> header : networkCallRecord.Response.entrySet()) {
        if (!header.getKey().equals("StatusCode") && !header.getKey().equals("Body") && !header.getKey().equals("Content-Length")) {
            String rawHeader = header.getValue();
            for (Entry<String, String> rule : textReplacementRules.entrySet()) {
                if (rule.getValue() != null) {
                    rawHeader = rawHeader.replaceAll(rule.getKey(), rule.getValue());
                }
            }
            rBuilder.withHeader(header.getKey(), rawHeader);
        }
    }
    String rawBody = networkCallRecord.Response.get("Body");
    if (rawBody != null) {
        for (Entry<String, String> rule : textReplacementRules.entrySet()) {
            if (rule.getValue() != null) {
                rawBody = rawBody.replaceAll(rule.getKey(), rule.getValue());
            }
        }
        rBuilder.withBody(rawBody);
        rBuilder.withHeader("Content-Length", String.valueOf(rawBody.getBytes("UTF-8").length));
    }
    mBuilder.willReturn(rBuilder);
    wireMock.register(mBuilder);
}
Also used : ResponseDefinitionBuilder(com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder) MappingBuilder(com.github.tomakehurst.wiremock.client.MappingBuilder) UrlPattern(com.github.tomakehurst.wiremock.matching.UrlPattern) IOException(java.io.IOException)

Aggregations

MappingBuilder (com.github.tomakehurst.wiremock.client.MappingBuilder)1 ResponseDefinitionBuilder (com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder)1 UrlPattern (com.github.tomakehurst.wiremock.matching.UrlPattern)1 IOException (java.io.IOException)1