@@ -58,6 +58,8 @@ public void testParseAsString_none() throws Exception {
5858
5959 private static final String SAMPLE = "123\u05D9 \u05e0 \u05D9 \u05D1 " ;
6060 private static final String SAMPLE2 = "123abc" ;
61+ private static final String VALID_CONTENT_TYPE = "text/plain" ;
62+ private static final String INVALID_CONTENT_TYPE = "!!!invalid!!!" ;
6163
6264 public void testParseAsString_utf8 () throws Exception {
6365 HttpTransport transport =
@@ -102,6 +104,56 @@ public LowLevelHttpResponse execute() throws IOException {
102104 assertEquals (SAMPLE2 , response .parseAsString ());
103105 }
104106
107+ public void testParseAsString_validContentType () throws Exception {
108+ HttpTransport transport =
109+ new MockHttpTransport () {
110+ @ Override
111+ public LowLevelHttpRequest buildRequest (String method , String url ) throws IOException {
112+ return new MockLowLevelHttpRequest () {
113+ @ Override
114+ public LowLevelHttpResponse execute () throws IOException {
115+ MockLowLevelHttpResponse result = new MockLowLevelHttpResponse ();
116+ result .setContent (SAMPLE2 );
117+ result .setContentType (VALID_CONTENT_TYPE );
118+ return result ;
119+ }
120+ };
121+ }
122+ };
123+ HttpRequest request =
124+ transport .createRequestFactory ().buildGetRequest (HttpTesting .SIMPLE_GENERIC_URL );
125+
126+ HttpResponse response = request .execute ();
127+ assertEquals (SAMPLE2 , response .parseAsString ());
128+ assertEquals (VALID_CONTENT_TYPE , response .getContentType ());
129+ assertNotNull (response .getMediaType ());
130+ }
131+
132+ public void testParseAsString_invalidContentType () throws Exception {
133+ HttpTransport transport =
134+ new MockHttpTransport () {
135+ @ Override
136+ public LowLevelHttpRequest buildRequest (String method , String url ) throws IOException {
137+ return new MockLowLevelHttpRequest () {
138+ @ Override
139+ public LowLevelHttpResponse execute () throws IOException {
140+ MockLowLevelHttpResponse result = new MockLowLevelHttpResponse ();
141+ result .setContent (SAMPLE2 );
142+ result .setContentType (INVALID_CONTENT_TYPE );
143+ return result ;
144+ }
145+ };
146+ }
147+ };
148+ HttpRequest request =
149+ transport .createRequestFactory ().buildGetRequest (HttpTesting .SIMPLE_GENERIC_URL );
150+
151+ HttpResponse response = request .execute ();
152+ assertEquals (SAMPLE2 , response .parseAsString ());
153+ assertEquals (INVALID_CONTENT_TYPE , response .getContentType ());
154+ assertNull (response .getMediaType ());
155+ }
156+
105157 public void testStatusCode_negative_dontThrowException () throws Exception {
106158 subtestStatusCode_negative (false );
107159 }
0 commit comments