2929import java .nio .charset .StandardCharsets ;
3030import java .text .NumberFormat ;
3131import java .util .Arrays ;
32+ import java .util .Locale ;
3233import java .util .logging .Level ;
3334import java .util .zip .GZIPInputStream ;
3435import java .util .zip .GZIPOutputStream ;
@@ -461,17 +462,37 @@ public LowLevelHttpResponse execute() throws IOException {
461462 }
462463
463464 public void testGetContent_gzipEncoding_finishReading () throws IOException {
465+ do_testGetContent_gzipEncoding_finishReading ("gzip" );
466+ }
467+
468+ public void testGetContent_gzipEncoding_finishReadingWithUppercaseContentEncoding () throws IOException {
469+ do_testGetContent_gzipEncoding_finishReading ("GZIP" );
470+ }
471+
472+ public void testGetContent_gzipEncoding_finishReadingWithDifferentDefaultLocaleAndUppercaseContentEncoding () throws IOException {
473+ Locale originalDefaultLocale = Locale .getDefault ();
474+ try {
475+ Locale .setDefault (Locale .forLanguageTag ("tr-TR" ));
476+ do_testGetContent_gzipEncoding_finishReading ("GZIP" );
477+ } finally {
478+ Locale .setDefault (originalDefaultLocale );
479+ }
480+ }
481+
482+ private void do_testGetContent_gzipEncoding_finishReading (String contentEncoding ) throws IOException {
464483 byte [] dataToCompress = "abcd" .getBytes (StandardCharsets .UTF_8 );
465484 byte [] mockBytes ;
466- try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream (dataToCompress .length )) {
467- GZIPOutputStream zipStream = new GZIPOutputStream ((byteStream ));
485+ try (
486+ ByteArrayOutputStream byteStream = new ByteArrayOutputStream (dataToCompress .length );
487+ GZIPOutputStream zipStream = new GZIPOutputStream ((byteStream ))
488+ ) {
468489 zipStream .write (dataToCompress );
469490 zipStream .close ();
470491 mockBytes = byteStream .toByteArray ();
471492 }
472493 final MockLowLevelHttpResponse mockResponse = new MockLowLevelHttpResponse ();
473494 mockResponse .setContent (mockBytes );
474- mockResponse .setContentEncoding ("gzip" );
495+ mockResponse .setContentEncoding (contentEncoding );
475496 mockResponse .setContentType ("text/plain" );
476497
477498 HttpTransport transport =
@@ -490,9 +511,35 @@ public LowLevelHttpResponse execute() throws IOException {
490511 HttpRequest request =
491512 transport .createRequestFactory ().buildHeadRequest (HttpTesting .SIMPLE_GENERIC_URL );
492513 HttpResponse response = request .execute ();
493- TestableByteArrayInputStream output = (TestableByteArrayInputStream ) mockResponse .getContent ();
494- assertFalse (output .isClosed ());
514+ try (TestableByteArrayInputStream output = (TestableByteArrayInputStream ) mockResponse .getContent ()) {
515+ assertFalse (output .isClosed ());
516+ assertEquals ("abcd" , response .parseAsString ());
517+ assertTrue (output .isClosed ());
518+ }
519+ }
520+
521+ public void testGetContent_otherEncodingWithgzipInItsName_GzipIsNotUsed () throws IOException {
522+ final MockLowLevelHttpResponse mockResponse = new MockLowLevelHttpResponse ();
523+ mockResponse .setContent ("abcd" );
524+ mockResponse .setContentEncoding ("otherEncodingWithgzipInItsName" );
525+ mockResponse .setContentType ("text/plain" );
526+
527+ HttpTransport transport =
528+ new MockHttpTransport () {
529+ @ Override
530+ public LowLevelHttpRequest buildRequest (String method , final String url )
531+ throws IOException {
532+ return new MockLowLevelHttpRequest () {
533+ @ Override
534+ public LowLevelHttpResponse execute () throws IOException {
535+ return mockResponse ;
536+ }
537+ };
538+ }
539+ };
540+ HttpRequest request = transport .createRequestFactory ().buildHeadRequest (HttpTesting .SIMPLE_GENERIC_URL );
541+ // If gzip was used on this response, an exception would be thrown
542+ HttpResponse response = request .execute ();
495543 assertEquals ("abcd" , response .parseAsString ());
496- assertTrue (output .isClosed ());
497544 }
498545}
0 commit comments