22
22
import com .google .api .client .testing .util .LogRecordingHandler ;
23
23
import com .google .api .client .testing .util .TestableByteArrayInputStream ;
24
24
import com .google .api .client .util .Key ;
25
+ import com .google .common .io .ByteStreams ;
26
+ import java .io .BufferedInputStream ;
25
27
import java .io .ByteArrayInputStream ;
26
28
import java .io .ByteArrayOutputStream ;
29
+ import java .io .FilterInputStream ;
27
30
import java .io .IOException ;
31
+ import java .io .InputStream ;
28
32
import java .lang .reflect .Type ;
29
33
import java .nio .charset .StandardCharsets ;
30
34
import java .text .NumberFormat ;
@@ -59,6 +63,8 @@ public void testParseAsString_none() throws Exception {
59
63
private static final String SAMPLE = "123\u05D9 \u05e0 \u05D9 \u05D1 " ;
60
64
private static final String SAMPLE2 = "123abc" ;
61
65
private static final String JSON_SAMPLE = "{\" foo\" : \" ßar\" }" ;
66
+ private static final String ERROR_SAMPLE =
67
+ "{domain:'global',reason:'domainPolicy',message:'msg'}" ;
62
68
private static final String VALID_CONTENT_TYPE = "text/plain" ;
63
69
private static final String VALID_CONTENT_TYPE_WITH_PARAMS =
64
70
"application/vnd.com.google.datastore.entity+json; charset=utf-8; version=v1; q=0.9" ;
@@ -543,7 +549,8 @@ public LowLevelHttpResponse execute() throws IOException {
543
549
};
544
550
HttpRequest request =
545
551
transport .createRequestFactory ().buildHeadRequest (HttpTesting .SIMPLE_GENERIC_URL );
546
- request .execute ().getContent ();
552
+ InputStream noContent = request .execute ().getContent ();
553
+ assertNull (noContent );
547
554
}
548
555
549
556
public void testGetContent_gzipEncoding_ReturnRawStream () throws IOException {
@@ -570,6 +577,9 @@ public LowLevelHttpResponse execute() throws IOException {
570
577
assertFalse (
571
578
"it should not decompress stream" ,
572
579
request .execute ().getContent () instanceof GZIPInputStream );
580
+ assertFalse (
581
+ "it should not buffer stream" ,
582
+ request .execute ().getContent () instanceof BufferedInputStream );
573
583
}
574
584
575
585
public void testGetContent_gzipEncoding_finishReading () throws IOException {
@@ -665,4 +675,71 @@ public LowLevelHttpResponse execute() throws IOException {
665
675
HttpResponse response = request .execute ();
666
676
assertEquals ("abcd" , response .parseAsString ());
667
677
}
678
+
679
+ public void testGetContent_bufferedContent () throws IOException {
680
+ HttpTransport transport =
681
+ new MockHttpTransport () {
682
+ @ Override
683
+ public LowLevelHttpRequest buildRequest (String method , String url ) throws IOException {
684
+ return new MockLowLevelHttpRequest () {
685
+ @ Override
686
+ public LowLevelHttpResponse execute () throws IOException {
687
+ // have to use gzip here because MockLowLevelHttpResponse.setContent()
688
+ // returns BufferedStream by itself, so test always success
689
+ byte [] dataToCompress = ERROR_SAMPLE .getBytes (StandardCharsets .UTF_8 );
690
+ ByteArrayOutputStream content = new ByteArrayOutputStream (dataToCompress .length );
691
+ try (GZIPOutputStream zipStream = new GZIPOutputStream ((content ))) {
692
+ zipStream .write (dataToCompress );
693
+ }
694
+
695
+ MockLowLevelHttpResponse result = new MockLowLevelHttpResponse ();
696
+ result .setStatusCode (403 );
697
+ result .setContentType (JSON_CONTENT_TYPE );
698
+ result .setContentEncoding ("gzip" );
699
+ result .setContent (content .toByteArray ());
700
+
701
+ return result ;
702
+ }
703
+ };
704
+ }
705
+ };
706
+ HttpRequest request =
707
+ transport
708
+ .createRequestFactory ()
709
+ .buildGetRequest (HttpTesting .SIMPLE_GENERIC_URL )
710
+ .setThrowExceptionOnExecuteError (false );
711
+
712
+ HttpResponse response = request .execute ();
713
+ InputStream content = response .getContent ();
714
+ assertTrue (content .markSupported ());
715
+
716
+ // inspect content like in HttpUnsuccessfulResponseHandler
717
+ try (RollbackInputStream is = new RollbackInputStream (content )) {
718
+ byte [] bytes = ByteStreams .toByteArray (is );
719
+ String text = new String (bytes , response .getContentCharset ());
720
+ assertEquals (ERROR_SAMPLE , text );
721
+ }
722
+
723
+ // original response still parsable by HttpResponseException
724
+ HttpResponseException exception = new HttpResponseException (response );
725
+ assertEquals (exception .getStatusCode (), 403 );
726
+ assertEquals (exception .getContent (), ERROR_SAMPLE );
727
+ }
728
+
729
+ static class RollbackInputStream extends FilterInputStream {
730
+ private boolean closed ;
731
+
732
+ RollbackInputStream (InputStream in ) {
733
+ super (in );
734
+ in .mark (8192 ); // big enough to keep most error messages
735
+ }
736
+
737
+ @ Override
738
+ public void close () throws IOException {
739
+ if (!closed ) {
740
+ closed = true ;
741
+ in .reset ();
742
+ }
743
+ }
744
+ }
668
745
}
0 commit comments