Search in sources :

Example 1 with BinaryFile

use of com.github.tomakehurst.wiremock.common.BinaryFile in project wiremock by wiremock.

the class StubResponseRenderer method renderDirectly.

private Response.Builder renderDirectly(ServeEvent serveEvent) {
    ResponseDefinition responseDefinition = serveEvent.getResponseDefinition();
    HttpHeaders headers = responseDefinition.getHeaders();
    StubMapping stubMapping = serveEvent.getStubMapping();
    if (serveEvent.getWasMatched() && stubMapping != null) {
        headers = firstNonNull(headers, new HttpHeaders()).plus(new HttpHeader("Matched-Stub-Id", stubMapping.getId().toString()));
        if (stubMapping.getName() != null) {
            headers = headers.plus(new HttpHeader("Matched-Stub-Name", stubMapping.getName()));
        }
    }
    Response.Builder responseBuilder = response().status(responseDefinition.getStatus()).statusMessage(responseDefinition.getStatusMessage()).headers(headers).fault(responseDefinition.getFault()).configureDelay(globalSettingsHolder.get().getFixedDelay(), globalSettingsHolder.get().getDelayDistribution(), responseDefinition.getFixedDelayMilliseconds(), responseDefinition.getDelayDistribution()).chunkedDribbleDelay(responseDefinition.getChunkedDribbleDelay());
    if (responseDefinition.specifiesBodyFile()) {
        BinaryFile bodyFile = fileSource.getBinaryFileNamed(responseDefinition.getBodyFileName());
        responseBuilder.body(bodyFile);
    } else if (responseDefinition.specifiesBodyContent()) {
        if (responseDefinition.specifiesBinaryBodyContent()) {
            responseBuilder.body(responseDefinition.getByteBody());
        } else {
            responseBuilder.body(responseDefinition.getByteBody());
        }
    }
    return responseBuilder;
}
Also used : StubMapping(com.github.tomakehurst.wiremock.stubbing.StubMapping) BinaryFile(com.github.tomakehurst.wiremock.common.BinaryFile)

Example 2 with BinaryFile

use of com.github.tomakehurst.wiremock.common.BinaryFile in project wiremock by wiremock.

the class RemoteMappingsLoader method convertBodyFromFileIfNecessary.

private void convertBodyFromFileIfNecessary(StubMapping mapping) {
    String bodyFileName = mapping.getResponse().getBodyFileName();
    if (bodyFileName != null) {
        ResponseDefinitionBuilder responseDefinitionBuilder = ResponseDefinitionBuilder.like(mapping.getResponse()).withBodyFile(null);
        String extension = substringAfterLast(bodyFileName, ".");
        String mimeType = getMimeType(mapping);
        if (ContentTypes.determineIsText(extension, mimeType)) {
            TextFile bodyFile = filesFileSource.getTextFileNamed(bodyFileName);
            responseDefinitionBuilder.withBody(bodyFile.readContentsAsString());
        } else {
            BinaryFile bodyFile = filesFileSource.getBinaryFileNamed(bodyFileName);
            responseDefinitionBuilder.withBody(bodyFile.readContents());
        }
        mapping.setResponse(responseDefinitionBuilder.build());
    }
}
Also used : ResponseDefinitionBuilder(com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder) TextFile(com.github.tomakehurst.wiremock.common.TextFile) BinaryFile(com.github.tomakehurst.wiremock.common.BinaryFile)

Aggregations

BinaryFile (com.github.tomakehurst.wiremock.common.BinaryFile)2 ResponseDefinitionBuilder (com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder)1 TextFile (com.github.tomakehurst.wiremock.common.TextFile)1 StubMapping (com.github.tomakehurst.wiremock.stubbing.StubMapping)1