public static String determineFileExtension(String url, ContentTypeHeader contentTypeHeader, byte[] responseBody) { if (contentTypeHeader.isPresent()) { if (contentTypeHeader.mimeTypePart().contains("json")) { return "json"; } if (contentTypeHeader.mimeTypePart().contains("xml")) { return "xml"; } if (contentTypeHeader.mimeTypePart().contains("text")) { return "txt"; } String extension = COMMON_MIME_TYPES.get(contentTypeHeader.mimeTypePart()); if (extension != null) { return extension; } } String path = URI.create(url).getPath(); String lastPathSegment = substringAfterLast(path, "/"); if (lastPathSegment.indexOf('.') != -1) { return substringAfterLast(lastPathSegment, "."); } return determineTextFileExtension(stringFromBytes(responseBody, contentTypeHeader.charset())); }
@JsonIgnore public String getMimeType() { return headers == null || headers.getContentTypeHeader() == null ? OCTET_STREAM.toString() : headers.getContentTypeHeader().mimeTypePart(); }
public static Body ofBinaryOrText(byte[] content, ContentTypeHeader contentTypeHeader) { return new Body(content, ContentTypes.determineIsTextFromMimeType(contentTypeHeader.mimeTypePart())); }
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; }
private static HttpEntity buildEntityFrom(Request originalRequest) { ContentTypeHeader contentTypeHeader = originalRequest.contentTypeHeader().or("text/plain"); ContentType contentType = ContentType.create(contentTypeHeader.mimeTypePart(), contentTypeHeader.encodingPart().or("utf-8")); if (originalRequest.containsHeader(TRANSFER_ENCODING) && originalRequest.header(TRANSFER_ENCODING).firstValue().equals("chunked")) { return applyGzipWrapperIfRequired( originalRequest, new InputStreamEntity(new ByteArrayInputStream(originalRequest.getBody()), -1, contentType) ); } return applyGzipWrapperIfRequired( originalRequest, new ByteArrayEntity(originalRequest.getBody()) ); }
/** * 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(); } } }