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;
}
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());
}
}
Aggregations