@@ -58,6 +58,10 @@ 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 VALID_CONTENT_TYPE_WITH_PARAMS =
63+ "application/vnd.com.google.datastore.entity+json; charset=utf-8; version=v1; q=0.9" ;
64+ private static final String INVALID_CONTENT_TYPE = "!!!invalid!!!" ;
6165
6266 public void testParseAsString_utf8 () throws Exception {
6367 HttpTransport transport =
@@ -102,6 +106,81 @@ public LowLevelHttpResponse execute() throws IOException {
102106 assertEquals (SAMPLE2 , response .parseAsString ());
103107 }
104108
109+ public void testParseAsString_validContentType () throws Exception {
110+ HttpTransport transport =
111+ new MockHttpTransport () {
112+ @ Override
113+ public LowLevelHttpRequest buildRequest (String method , String url ) throws IOException {
114+ return new MockLowLevelHttpRequest () {
115+ @ Override
116+ public LowLevelHttpResponse execute () throws IOException {
117+ MockLowLevelHttpResponse result = new MockLowLevelHttpResponse ();
118+ result .setContent (SAMPLE2 );
119+ result .setContentType (VALID_CONTENT_TYPE );
120+ return result ;
121+ }
122+ };
123+ }
124+ };
125+ HttpRequest request =
126+ transport .createRequestFactory ().buildGetRequest (HttpTesting .SIMPLE_GENERIC_URL );
127+
128+ HttpResponse response = request .execute ();
129+ assertEquals (SAMPLE2 , response .parseAsString ());
130+ assertEquals (VALID_CONTENT_TYPE , response .getContentType ());
131+ assertNotNull (response .getMediaType ());
132+ }
133+
134+ public void testParseAsString_validContentTypeWithParams () throws Exception {
135+ HttpTransport transport =
136+ new MockHttpTransport () {
137+ @ Override
138+ public LowLevelHttpRequest buildRequest (String method , String url ) throws IOException {
139+ return new MockLowLevelHttpRequest () {
140+ @ Override
141+ public LowLevelHttpResponse execute () throws IOException {
142+ MockLowLevelHttpResponse result = new MockLowLevelHttpResponse ();
143+ result .setContent (SAMPLE2 );
144+ result .setContentType (VALID_CONTENT_TYPE_WITH_PARAMS );
145+ return result ;
146+ }
147+ };
148+ }
149+ };
150+ HttpRequest request =
151+ transport .createRequestFactory ().buildGetRequest (HttpTesting .SIMPLE_GENERIC_URL );
152+
153+ HttpResponse response = request .execute ();
154+ assertEquals (SAMPLE2 , response .parseAsString ());
155+ assertEquals (VALID_CONTENT_TYPE_WITH_PARAMS , response .getContentType ());
156+ assertNotNull (response .getMediaType ());
157+ }
158+
159+ public void testParseAsString_invalidContentType () throws Exception {
160+ HttpTransport transport =
161+ new MockHttpTransport () {
162+ @ Override
163+ public LowLevelHttpRequest buildRequest (String method , String url ) throws IOException {
164+ return new MockLowLevelHttpRequest () {
165+ @ Override
166+ public LowLevelHttpResponse execute () throws IOException {
167+ MockLowLevelHttpResponse result = new MockLowLevelHttpResponse ();
168+ result .setContent (SAMPLE2 );
169+ result .setContentType (INVALID_CONTENT_TYPE );
170+ return result ;
171+ }
172+ };
173+ }
174+ };
175+ HttpRequest request =
176+ transport .createRequestFactory ().buildGetRequest (HttpTesting .SIMPLE_GENERIC_URL );
177+
178+ HttpResponse response = request .execute ();
179+ assertEquals (SAMPLE2 , response .parseAsString ());
180+ assertEquals (INVALID_CONTENT_TYPE , response .getContentType ());
181+ assertNull (response .getMediaType ());
182+ }
183+
105184 public void testStatusCode_negative_dontThrowException () throws Exception {
106185 subtestStatusCode_negative (false );
107186 }
0 commit comments