@Override public ContentTypeHeader contentTypeHeader() { if (headers != null) { return headers.getContentTypeHeader(); } return null; }
@Override public ContentTypeHeader contentTypeHeader() { return getHeaders().getContentTypeHeader(); }
@JsonIgnore public String getMimeType() { return headers == null || headers.getContentTypeHeader() == null ? OCTET_STREAM.toString() : headers.getContentTypeHeader().mimeTypePart(); }
@JsonIgnore public Charset getCharset() { return headers == null ? Strings.DEFAULT_CHARSET : headers.getContentTypeHeader().charset(); }
public Response(int status, String statusMessage, String body, HttpHeaders headers, boolean configured, Fault fault, long initialDelay, ChunkedDribbleDelay chunkedDribbleDelay, boolean fromProxy) { this.status = status; this.statusMessage = statusMessage; this.headers = headers; this.bodyStreamSource = StreamSources.forString(body, headers.getContentTypeHeader().charset()); this.configured = configured; this.fault = fault; this.initialDelay = initialDelay; this.chunkedDribbleDelay = chunkedDribbleDelay; this.fromProxy = fromProxy; }
private String getMimeType(StubMapping mapping) { HttpHeaders responseHeaders = mapping.getResponse().getHeaders(); if (responseHeaders != null) { ContentTypeHeader contentTypeHeader = responseHeaders.getContentTypeHeader(); return contentTypeHeader != null ? contentTypeHeader.mimeTypePart() : null; } return null; }
public String getBodyAsString() { return Strings.stringFromBytes(getBody(), headers.getContentTypeHeader().charset()); }
/** * If request body was JSON or XML, use "equalToJson" or "equalToXml" (respectively) in the RequestPattern so it's * easier to read. Otherwise, just use "equalTo" */ @Override public ContentPattern<?> forRequest(Request request) { final String mimeType = request.getHeaders().getContentTypeHeader().mimeTypePart(); if (mimeType != null) { if (mimeType.contains("json")) { return new EqualToJsonPattern(request.getBodyAsString(), ignoreArrayOrder, ignoreExtraElements); } else if (mimeType.contains("xml")) { return new EqualToXmlPattern(request.getBodyAsString()); } else if (mimeType.equals("multipart/form-data")) { // TODO: Need to add a matcher that can handle multipart data properly. For now, just always match return new AnythingPattern(); } else if (!determineIsTextFromMimeType(mimeType)) { return new BinaryEqualToPattern(request.getBody()); } } return new EqualToPattern(request.getBodyAsString(), caseInsensitive); } }
@Override public MatchResult match(ResponseDefinition responseDefinition) { if (!responseDefinition.specifiesBodyContent()) { return MatchResult.noMatch(); } else if ( responseDefinition.getHeaders() != null && ContentTypes.determineIsTextFromMimeType(responseDefinition.getHeaders().getContentTypeHeader().mimeTypePart()) ) { if (responseDefinition.getBody().length() > textSizeThreshold) { return MatchResult.exactMatch(); } else { return MatchResult.noMatch(); } } else { if (responseDefinition.getByteBody().length > binarySizeThreshold) { return MatchResult.exactMatch(); } else { return MatchResult.noMatch(); } } }
private void writeToMappingAndBodyFile(Request request, Response response, RequestPattern requestPattern) { String fileId = idGenerator.generate(); byte[] body = bodyDecompressedIfRequired(response); String mappingFileName = UniqueFilenameGenerator.generate(request.getUrl(), "mapping", fileId); String bodyFileName = UniqueFilenameGenerator.generate( request.getUrl(), "body", fileId, ContentTypes.determineFileExtension( request.getUrl(), response.getHeaders().getContentTypeHeader(), body)); ResponseDefinitionBuilder responseDefinitionBuilder = responseDefinition() .withStatus(response.getStatus()) .withBodyFile(bodyFileName); if (response.getHeaders().size() > 0) { responseDefinitionBuilder.withHeaders(withoutContentEncodingAndContentLength(response.getHeaders())); } ResponseDefinition responseToWrite = responseDefinitionBuilder.build(); StubMapping mapping = new StubMapping(requestPattern, responseToWrite); mapping.setUuid(UUID.nameUUIDFromBytes(fileId.getBytes())); filesFileSource.writeBinaryFile(bodyFileName, body); mappingsFileSource.writeTextFile(mappingFileName, write(mapping)); }
String extension = ContentTypes.determineFileExtension( stubMapping.getRequest().getUrl(), responseHeaders != null ? responseHeaders.getContentTypeHeader() : ContentTypeHeader.absent(), body);