Skip to content

Commit

Permalink
Issue googleapis#842 - Enforce locale on encoding comparison, add uni…
Browse files Browse the repository at this point in the history
…t tests
  • Loading branch information
egsavage committed Oct 27, 2019
1 parent 2cc4fe7 commit beeac46
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Expand Up @@ -26,6 +26,7 @@
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
Expand Down Expand Up @@ -333,12 +334,12 @@ public InputStream getContent() throws IOException {
boolean contentProcessed = false;
try {
// gzip encoding (wrap content with GZipInputStream)
if (!returnRawInputStream
&& this.contentEncoding != null
&& (CONTENT_ENCODING_GZIP.equals(this.contentEncoding.trim())
|| CONTENT_ENCODING_XGZIP.equals(this.contentEncoding.trim()))) {
lowLevelResponseContent =
new ConsumingInputStream(new GZIPInputStream(lowLevelResponseContent));
if (!returnRawInputStream && this.contentEncoding != null) {
final String oontentEncoding = this.contentEncoding.trim().toLowerCase(Locale.ENGLISH);
if (CONTENT_ENCODING_GZIP.equals(oontentEncoding) || CONTENT_ENCODING_XGZIP.equals(oontentEncoding)) {
lowLevelResponseContent =
new ConsumingInputStream(new GZIPInputStream(lowLevelResponseContent));
}
}
// logging (wrap content with LoggingInputStream)
Logger logger = HttpTransport.LOGGER;
Expand Down
Expand Up @@ -29,6 +29,7 @@
import java.nio.charset.StandardCharsets;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.Locale;
import java.util.logging.Level;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
Expand Down Expand Up @@ -461,6 +462,21 @@ public LowLevelHttpResponse execute() throws IOException {
}

public void testGetContent_gzipEncoding_finishReading() throws IOException {
do_testGetContent_gzipEncoding_finishReading("gzip");
}

public void testGetContent_gzipEncoding_finishReadingWithUppercaseContentEncoding() throws IOException {
do_testGetContent_gzipEncoding_finishReading("GZIP");
}

public void testGetContent_gzipEncoding_finishReadingWithDifferentDefaultLocaleAndUppercaseContentEncoding() throws IOException {
Locale originalDefaultLocale = Locale.getDefault();
Locale.setDefault(Locale.JAPAN);
do_testGetContent_gzipEncoding_finishReading("GZIP");
Locale.setDefault(originalDefaultLocale);
}

private void do_testGetContent_gzipEncoding_finishReading(String contentEncoding) throws IOException {
byte[] dataToCompress = "abcd".getBytes(StandardCharsets.UTF_8);
byte[] mockBytes;
try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream(dataToCompress.length)) {
Expand All @@ -471,7 +487,7 @@ public void testGetContent_gzipEncoding_finishReading() throws IOException {
}
final MockLowLevelHttpResponse mockResponse = new MockLowLevelHttpResponse();
mockResponse.setContent(mockBytes);
mockResponse.setContentEncoding("gzip");
mockResponse.setContentEncoding(contentEncoding);
mockResponse.setContentType("text/plain");

HttpTransport transport =
Expand Down

0 comments on commit beeac46

Please sign in to comment.