Skip to content

Commit

Permalink
Issue googleapis#842 - Added unit test, PR updates
Browse files Browse the repository at this point in the history
  • Loading branch information
egsavage committed Oct 19, 2019
1 parent 08adb6e commit af5b469
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
Expand Up @@ -83,7 +83,7 @@ public final class HttpResponse {
/** Content encoding for GZip */
private static final String CONTENT_ENCODING_GZIP = "gzip";

/** Content encoding for GZip HTTP 1.1 */
/** Content encoding for GZip (legacy) */
private static final String CONTENT_ENCODING_XGZIP = "x-gzip";

/**
Expand Down Expand Up @@ -333,13 +333,12 @@ public InputStream getContent() throws IOException {
boolean contentProcessed = false;
try {
// gzip encoding (wrap content with GZipInputStream)
String contentEncoding = this.contentEncoding;
if (!returnRawInputStream
&& contentEncoding != null
&& (CONTENT_ENCODING_GZIP.equalsIgnoreCase(contentEncoding.trim())
|| CONTENT_ENCODING_XGZIP.equalsIgnoreCase(contentEncoding.trim()))) {
&& this.contentEncoding != null
&& (CONTENT_ENCODING_GZIP.equalsIgnoreCase(this.contentEncoding.trim())
|| CONTENT_ENCODING_XGZIP.equalsIgnoreCase(this.contentEncoding.trim()))) {
lowLevelResponseContent =
new ConsumingInputStream(new GZIPInputStream(lowLevelResponseContent));
new ConsumingInputStream(new GZIPInputStream(lowLevelResponseContent));
}
// logging (wrap content with LoggingInputStream)
Logger logger = HttpTransport.LOGGER;
Expand Down
Expand Up @@ -495,4 +495,30 @@ public LowLevelHttpResponse execute() throws IOException {
assertEquals("abcd", response.parseAsString());
assertTrue(output.isClosed());
}

public void testGetContent_otherEncodingWithgzipInItsName_GzipIsNotUsed() throws IOException {
final MockLowLevelHttpResponse mockResponse = new MockLowLevelHttpResponse();
mockResponse.setContent("abcd");
mockResponse.setContentEncoding("otherEncodingWithgzipInItsName");
mockResponse.setContentType("text/plain");

HttpTransport transport =
new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, final String url)
throws IOException {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
return mockResponse;
}
};
}
};
HttpRequest request =
transport.createRequestFactory().buildHeadRequest(HttpTesting.SIMPLE_GENERIC_URL);
// If gzip was used on this response, an exception would be thrown
HttpResponse response = request.execute();
assertEquals("abcd", response.parseAsString());
}
}

0 comments on commit af5b469

Please sign in to comment.