@@ -58,10 +58,12 @@ 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 JSON_SAMPLE = "{\" foo\" : \" ßar\" }" ;
6162 private static final String VALID_CONTENT_TYPE = "text/plain" ;
6263 private static final String VALID_CONTENT_TYPE_WITH_PARAMS =
6364 "application/vnd.com.google.datastore.entity+json; charset=utf-8; version=v1; q=0.9" ;
6465 private static final String INVALID_CONTENT_TYPE = "!!!invalid!!!" ;
66+ private static final String JSON_CONTENT_TYPE = "application/json" ;
6567
6668 public void testParseAsString_utf8 () throws Exception {
6769 HttpTransport transport =
@@ -83,6 +85,7 @@ public LowLevelHttpResponse execute() throws IOException {
8385 transport .createRequestFactory ().buildGetRequest (HttpTesting .SIMPLE_GENERIC_URL );
8486 HttpResponse response = request .execute ();
8587 assertEquals (SAMPLE , response .parseAsString ());
88+ assertEquals ("UTF-8" , response .getContentCharset ().name ());
8689 }
8790
8891 public void testParseAsString_noContentType () throws Exception {
@@ -104,6 +107,7 @@ public LowLevelHttpResponse execute() throws IOException {
104107 transport .createRequestFactory ().buildGetRequest (HttpTesting .SIMPLE_GENERIC_URL );
105108 HttpResponse response = request .execute ();
106109 assertEquals (SAMPLE2 , response .parseAsString ());
110+ assertEquals ("ISO-8859-1" , response .getContentCharset ().name ());
107111 }
108112
109113 public void testParseAsString_validContentType () throws Exception {
@@ -129,6 +133,7 @@ public LowLevelHttpResponse execute() throws IOException {
129133 assertEquals (SAMPLE2 , response .parseAsString ());
130134 assertEquals (VALID_CONTENT_TYPE , response .getContentType ());
131135 assertNotNull (response .getMediaType ());
136+ assertEquals ("ISO-8859-1" , response .getContentCharset ().name ());
132137 }
133138
134139 public void testParseAsString_validContentTypeWithParams () throws Exception {
@@ -154,6 +159,7 @@ public LowLevelHttpResponse execute() throws IOException {
154159 assertEquals (SAMPLE2 , response .parseAsString ());
155160 assertEquals (VALID_CONTENT_TYPE_WITH_PARAMS , response .getContentType ());
156161 assertNotNull (response .getMediaType ());
162+ assertEquals ("UTF-8" , response .getContentCharset ().name ());
157163 }
158164
159165 public void testParseAsString_invalidContentType () throws Exception {
@@ -179,6 +185,32 @@ public LowLevelHttpResponse execute() throws IOException {
179185 assertEquals (SAMPLE2 , response .parseAsString ());
180186 assertEquals (INVALID_CONTENT_TYPE , response .getContentType ());
181187 assertNull (response .getMediaType ());
188+ assertEquals ("ISO-8859-1" , response .getContentCharset ().name ());
189+ }
190+
191+ public void testParseAsString_jsonContentType () throws IOException {
192+ HttpTransport transport =
193+ new MockHttpTransport () {
194+ @ Override
195+ public LowLevelHttpRequest buildRequest (String method , String url ) throws IOException {
196+ return new MockLowLevelHttpRequest () {
197+ @ Override
198+ public LowLevelHttpResponse execute () throws IOException {
199+ MockLowLevelHttpResponse result = new MockLowLevelHttpResponse ();
200+ result .setContent (JSON_SAMPLE );
201+ result .setContentType (JSON_CONTENT_TYPE );
202+ return result ;
203+ }
204+ };
205+ }
206+ };
207+ HttpRequest request =
208+ transport .createRequestFactory ().buildGetRequest (HttpTesting .SIMPLE_GENERIC_URL );
209+
210+ HttpResponse response = request .execute ();
211+ assertEquals (JSON_SAMPLE , response .parseAsString ());
212+ assertEquals (JSON_CONTENT_TYPE , response .getContentType ());
213+ assertEquals ("UTF-8" , response .getContentCharset ().name ());
182214 }
183215
184216 public void testStatusCode_negative_dontThrowException () throws Exception {
0 commit comments