Skip to content

Commit bc8c228

Browse files
authored
Merge b1e960d into 1db338b
2 parents 1db338b + b1e960d commit bc8c228

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import com.google.api.client.util.LoggingInputStream;
1919
import com.google.api.client.util.Preconditions;
2020
import com.google.api.client.util.StringUtils;
21+
import com.google.common.annotations.VisibleForTesting;
22+
2123
import java.io.ByteArrayOutputStream;
2224
import java.io.EOFException;
2325
import java.io.IOException;
@@ -512,8 +514,18 @@ public String parseAsString() throws IOException {
512514
* @since 1.10
513515
*/
514516
public Charset getContentCharset() {
515-
return mediaType == null || mediaType.getCharsetParameter() == null
516-
? StandardCharsets.ISO_8859_1
517-
: mediaType.getCharsetParameter();
517+
if (mediaType != null) {
518+
// use specified charset parameter from content/type header if available
519+
if (mediaType.getCharsetParameter() != null) {
520+
return mediaType.getCharsetParameter();
521+
}
522+
// fallback to well-known charsets
523+
if (mediaType.getType().equals("application") && mediaType.getSubType().equals("json")) {
524+
// https://tools.ietf.org/html/rfc4627 - JSON must be encoded with UTF-8
525+
return StandardCharsets.UTF_8;
526+
}
527+
}
528+
return StandardCharsets.ISO_8859_1;
518529
}
530+
519531
}

google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)