@Test public void getResponseBody_addExMachinaCharsetToContentTypeHeader() throws Exception { Charset exMachinaCharset = UTF_8; SstoehrHarBridge bridge = new SstoehrHarBridge(exMachinaCharset); ParsedRequest request = ParsedRequest.inMemory(io.github.mike10004.vhs.harbridge.HttpMethod.GET, URI.create("http: MediaType entryResponseContentType = MediaType.PLAIN_TEXT_UTF_8.withoutParameters(); HarEntry entry = createHarEntry(request.url, entryResponseContentType, "foo", 3); TypedContent content = bridge.getResponseBody(entry); MediaType expectedResponseContentType = entryResponseContentType.withCharset(exMachinaCharset); assertEquals("content type", expectedResponseContentType, content.getContentType()); }
@VisibleForTesting TypedContent getResponseBody(HarEntry entry) throws IOException { HarResponse rsp = entry.getResponse(); if (rsp == null) { return TypedContent.identity(ByteSource.empty(), MediaType.OCTET_STREAM); } HarContent content = requireNonNull(rsp.getContent(), "response.content"); @Nullable Long harContentSize = nullIfNegative(content.getSize()); @Nullable Long bodySize = nullIfNegative(rsp.getBodySize()); List<HarHeader> headers = MoreObjects.firstNonNull(rsp.getHeaders(), Collections.emptyList()); @Nullable String contentEncodingHeaderValue = headers.stream() .filter(h -> HttpHeaders.CONTENT_ENCODING.equalsIgnoreCase(h.getName())) .map(HarHeader::getValue) .findFirst().orElse(null); @Nullable String harContentEncoding = Strings.emptyToNull(content.getEncoding()); @Nullable String contentType = content.getMimeType(); @Nullable String comment = content.getComment(); @Nullable String text = content.getText(); return Hars.translateResponseContent(contentType, text, bodySize, harContentSize, contentEncodingHeaderValue, harContentEncoding, comment, exMachinaCharset); }
SstoehrHarBridge implements HarBridge<HarEntry> { @VisibleForTesting TypedContent getResponseBody(HarEntry entry) throws IOException { HarResponse rsp = entry.getResponse(); if (rsp == null) { return TypedContent.identity(ByteSource.empty(), MediaType.OCTET_STREAM); } HarContent content = requireNonNull(rsp.getContent(), "response.content"); @Nullable Long harContentSize = nullIfNegative(content.getSize()); @Nullable Long bodySize = nullIfNegative(rsp.getBodySize()); List<HarHeader> headers = MoreObjects.firstNonNull(rsp.getHeaders(), Collections.emptyList()); @Nullable String contentEncodingHeaderValue = headers.stream() .filter(h -> HttpHeaders.CONTENT_ENCODING.equalsIgnoreCase(h.getName())) .map(HarHeader::getValue) .findFirst().orElse(null); @Nullable String harContentEncoding = Strings.emptyToNull(content.getEncoding()); @Nullable String contentType = content.getMimeType(); @Nullable String comment = content.getComment(); @Nullable String text = content.getText(); return Hars.translateResponseContent(contentType, text, bodySize, harContentSize, contentEncodingHeaderValue, harContentEncoding, comment, exMachinaCharset); } }
SstoehrHarBridge implements HarBridge<HarEntry> { @VisibleForTesting TypedContent getResponseBody(HarEntry entry) throws IOException { HarResponse rsp = entry.getResponse(); if (rsp == null) { return TypedContent.identity(ByteSource.empty(), MediaType.OCTET_STREAM); } HarContent content = requireNonNull(rsp.getContent(), "response.content"); @Nullable Long harContentSize = nullIfNegative(content.getSize()); @Nullable Long bodySize = nullIfNegative(rsp.getBodySize()); List<HarHeader> headers = MoreObjects.firstNonNull(rsp.getHeaders(), Collections.emptyList()); @Nullable String contentEncodingHeaderValue = headers.stream() .filter(h -> HttpHeaders.CONTENT_ENCODING.equalsIgnoreCase(h.getName())) .map(HarHeader::getValue) .findFirst().orElse(null); @Nullable String harContentEncoding = Strings.emptyToNull(content.getEncoding()); @Nullable String contentType = content.getMimeType(); @Nullable String comment = content.getComment(); @Nullable String text = content.getText(); return Hars.translateResponseContent(contentType, text, bodySize, harContentSize, contentEncodingHeaderValue, harContentEncoding, comment, exMachinaCharset); } SstoehrHarBridge();  SstoehrHarBridge(Charset exMachinaCharset); }
SstoehrHarBridge implements HarBridge<HarEntry> { @VisibleForTesting TypedContent getResponseBody(HarEntry entry) throws IOException { HarResponse rsp = entry.getResponse(); if (rsp == null) { return TypedContent.identity(ByteSource.empty(), MediaType.OCTET_STREAM); } HarContent content = requireNonNull(rsp.getContent(), "response.content"); @Nullable Long harContentSize = nullIfNegative(content.getSize()); @Nullable Long bodySize = nullIfNegative(rsp.getBodySize()); List<HarHeader> headers = MoreObjects.firstNonNull(rsp.getHeaders(), Collections.emptyList()); @Nullable String contentEncodingHeaderValue = headers.stream() .filter(h -> HttpHeaders.CONTENT_ENCODING.equalsIgnoreCase(h.getName())) .map(HarHeader::getValue) .findFirst().orElse(null); @Nullable String harContentEncoding = Strings.emptyToNull(content.getEncoding()); @Nullable String contentType = content.getMimeType(); @Nullable String comment = content.getComment(); @Nullable String text = content.getText(); return Hars.translateResponseContent(contentType, text, bodySize, harContentSize, contentEncodingHeaderValue, harContentEncoding, comment, exMachinaCharset); } SstoehrHarBridge();  SstoehrHarBridge(Charset exMachinaCharset); @Override String getRequestMethod(HarEntry entry); @Override String getRequestUrl(HarEntry entry); @Override Stream<Map.Entry<String, String>> getRequestHeaders(HarEntry entry); @Nullable @Override ByteSource getRequestPostData(HarEntry entry); @Override HarResponseData getResponseData(ParsedRequest request, HarEntry entry, HarResponseEncoding encoding); @Override int getResponseStatus(HarEntry entry); }
SstoehrHarBridge implements HarBridge<HarEntry> { @VisibleForTesting TypedContent getResponseBody(HarEntry entry) throws IOException { HarResponse rsp = entry.getResponse(); if (rsp == null) { return TypedContent.identity(ByteSource.empty(), MediaType.OCTET_STREAM); } HarContent content = requireNonNull(rsp.getContent(), "response.content"); @Nullable Long harContentSize = nullIfNegative(content.getSize()); @Nullable Long bodySize = nullIfNegative(rsp.getBodySize()); List<HarHeader> headers = MoreObjects.firstNonNull(rsp.getHeaders(), Collections.emptyList()); @Nullable String contentEncodingHeaderValue = headers.stream() .filter(h -> HttpHeaders.CONTENT_ENCODING.equalsIgnoreCase(h.getName())) .map(HarHeader::getValue) .findFirst().orElse(null); @Nullable String harContentEncoding = Strings.emptyToNull(content.getEncoding()); @Nullable String contentType = content.getMimeType(); @Nullable String comment = content.getComment(); @Nullable String text = content.getText(); return Hars.translateResponseContent(contentType, text, bodySize, harContentSize, contentEncodingHeaderValue, harContentEncoding, comment, exMachinaCharset); } SstoehrHarBridge();  SstoehrHarBridge(Charset exMachinaCharset); @Override String getRequestMethod(HarEntry entry); @Override String getRequestUrl(HarEntry entry); @Override Stream<Map.Entry<String, String>> getRequestHeaders(HarEntry entry); @Nullable @Override ByteSource getRequestPostData(HarEntry entry); @Override HarResponseData getResponseData(ParsedRequest request, HarEntry entry, HarResponseEncoding encoding); @Override int getResponseStatus(HarEntry entry); static final Charset DEFAULT_EX_MACHINA_CHARSET; }