29
29
import java .nio .charset .StandardCharsets ;
30
30
import java .text .NumberFormat ;
31
31
import java .util .Arrays ;
32
+ import java .util .Locale ;
32
33
import java .util .logging .Level ;
33
34
import java .util .zip .GZIPInputStream ;
34
35
import java .util .zip .GZIPOutputStream ;
@@ -461,17 +462,37 @@ public LowLevelHttpResponse execute() throws IOException {
461
462
}
462
463
463
464
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 {
464
483
byte [] dataToCompress = "abcd" .getBytes (StandardCharsets .UTF_8 );
465
484
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
+ ) {
468
489
zipStream .write (dataToCompress );
469
490
zipStream .close ();
470
491
mockBytes = byteStream .toByteArray ();
471
492
}
472
493
final MockLowLevelHttpResponse mockResponse = new MockLowLevelHttpResponse ();
473
494
mockResponse .setContent (mockBytes );
474
- mockResponse .setContentEncoding ("gzip" );
495
+ mockResponse .setContentEncoding (contentEncoding );
475
496
mockResponse .setContentType ("text/plain" );
476
497
477
498
HttpTransport transport =
@@ -490,9 +511,35 @@ public LowLevelHttpResponse execute() throws IOException {
490
511
HttpRequest request =
491
512
transport .createRequestFactory ().buildHeadRequest (HttpTesting .SIMPLE_GENERIC_URL );
492
513
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 ();
495
543
assertEquals ("abcd" , response .parseAsString ());
496
- assertTrue (output .isClosed ());
497
544
}
498
545
}
0 commit comments