@@ -58,10 +58,12 @@ public void testParseAsString_none() throws Exception {
58
58
59
59
private static final String SAMPLE = "123\u05D9 \u05e0 \u05D9 \u05D1 " ;
60
60
private static final String SAMPLE2 = "123abc" ;
61
+ private static final String JSON_SAMPLE = "{\" foo\" : \" ßar\" }" ;
61
62
private static final String VALID_CONTENT_TYPE = "text/plain" ;
62
63
private static final String VALID_CONTENT_TYPE_WITH_PARAMS =
63
64
"application/vnd.com.google.datastore.entity+json; charset=utf-8; version=v1; q=0.9" ;
64
65
private static final String INVALID_CONTENT_TYPE = "!!!invalid!!!" ;
66
+ private static final String JSON_CONTENT_TYPE = "application/json" ;
65
67
66
68
public void testParseAsString_utf8 () throws Exception {
67
69
HttpTransport transport =
@@ -83,6 +85,7 @@ public LowLevelHttpResponse execute() throws IOException {
83
85
transport .createRequestFactory ().buildGetRequest (HttpTesting .SIMPLE_GENERIC_URL );
84
86
HttpResponse response = request .execute ();
85
87
assertEquals (SAMPLE , response .parseAsString ());
88
+ assertEquals ("UTF-8" , response .getContentCharset ().name ());
86
89
}
87
90
88
91
public void testParseAsString_noContentType () throws Exception {
@@ -104,6 +107,7 @@ public LowLevelHttpResponse execute() throws IOException {
104
107
transport .createRequestFactory ().buildGetRequest (HttpTesting .SIMPLE_GENERIC_URL );
105
108
HttpResponse response = request .execute ();
106
109
assertEquals (SAMPLE2 , response .parseAsString ());
110
+ assertEquals ("ISO-8859-1" , response .getContentCharset ().name ());
107
111
}
108
112
109
113
public void testParseAsString_validContentType () throws Exception {
@@ -129,6 +133,7 @@ public LowLevelHttpResponse execute() throws IOException {
129
133
assertEquals (SAMPLE2 , response .parseAsString ());
130
134
assertEquals (VALID_CONTENT_TYPE , response .getContentType ());
131
135
assertNotNull (response .getMediaType ());
136
+ assertEquals ("ISO-8859-1" , response .getContentCharset ().name ());
132
137
}
133
138
134
139
public void testParseAsString_validContentTypeWithParams () throws Exception {
@@ -154,6 +159,7 @@ public LowLevelHttpResponse execute() throws IOException {
154
159
assertEquals (SAMPLE2 , response .parseAsString ());
155
160
assertEquals (VALID_CONTENT_TYPE_WITH_PARAMS , response .getContentType ());
156
161
assertNotNull (response .getMediaType ());
162
+ assertEquals ("UTF-8" , response .getContentCharset ().name ());
157
163
}
158
164
159
165
public void testParseAsString_invalidContentType () throws Exception {
@@ -179,6 +185,32 @@ public LowLevelHttpResponse execute() throws IOException {
179
185
assertEquals (SAMPLE2 , response .parseAsString ());
180
186
assertEquals (INVALID_CONTENT_TYPE , response .getContentType ());
181
187
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 ());
182
214
}
183
215
184
216
public void testStatusCode_negative_dontThrowException () throws Exception {
0 commit comments