Skip to content

Commit 0c73dc9

Browse files
Doug BlackDoug Black
authored andcommitted
Merge pull request #96 from skimbrel/master
Support and request UTF-8 charset responses
2 parents d3e29b1 + bbb3c5f commit 0c73dc9

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

src/main/java/com/twilio/sdk/TwilioRestClient.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void setHttpclient(HttpClient httpclient) {
9797
}
9898

9999
/**
100-
* Explcitly construct a TwilioRestClient with the given API credentials.
100+
* Explicitly construct a TwilioRestClient with the given API credentials.
101101
*
102102
* @param accountSid
103103
* the 34 character Account identifier (starting with 'AC'). This
@@ -112,7 +112,7 @@ public TwilioRestClient(String accountSid, String authToken) {
112112
}
113113

114114
/**
115-
* Explcitly construct a TwilioRestClient with the given API credentials and
115+
* Explicitly construct a TwilioRestClient with the given API credentials and
116116
* endpoint.
117117
*
118118
* @param accountSid
@@ -501,6 +501,7 @@ private HttpUriRequest setupRequest(String path, String method,
501501
request.addHeader(new BasicHeader("User-Agent", "twilio-java/"
502502
+ VERSION));
503503
request.addHeader(new BasicHeader("Accept", "application/json"));
504+
request.addHeader(new BasicHeader("Accept-Charset", "utf-8"));
504505

505506
if (httpclient instanceof DefaultHttpClient) { // as DefaultHttpClient class has final method, I need httpClient to be a plain interface to be able to mock it
506507
((DefaultHttpClient) httpclient).getCredentialsProvider()

src/main/java/com/twilio/sdk/TwilioRestResponse.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public void setContentType(String contentType) {
202202
* @return true if this looks like a JSON response
203203
*/
204204
public boolean isJson() {
205-
return (this.contentType.equalsIgnoreCase("application/json"));
205+
return (this.contentType.toLowerCase().contains("application/json"));
206206
}
207207

208208
/**
@@ -211,8 +211,9 @@ public boolean isJson() {
211211
* @return true if this looks like an XML response
212212
*/
213213
public boolean isXml() {
214-
return (this.contentType.equalsIgnoreCase("text/xml") || this.contentType
215-
.equalsIgnoreCase("application/xml"));
214+
String lowercaseContentType = this.contentType.toLowerCase();
215+
return (lowercaseContentType.contains("text/xml") ||
216+
lowercaseContentType.contains("application/xml"));
216217
}
217218

218219
/**
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.twilio.sdk;
2+
3+
import static org.junit.Assert.assertTrue;
4+
import static org.junit.Assert.assertFalse;
5+
6+
import com.twilio.sdk.parser.JsonResponseParser;
7+
import com.twilio.sdk.parser.XmlResponseParser;
8+
9+
import org.junit.Test;
10+
11+
12+
/**
13+
* Tests for TwilioRestResponse.
14+
*/
15+
public class TwilioRestResponseTest {
16+
17+
/**
18+
* Test content-type logic.
19+
*/
20+
@Test
21+
public void testTwilioRestResponseContentType() {
22+
TwilioRestResponse response = new TwilioRestResponse("http://example.com/test", "don't care", 200);
23+
response.setContentType("application/json; charset=utf-8");
24+
assertTrue(response.isJson());
25+
assertFalse(response.isXml());
26+
27+
response.setContentType("application/xml; charset=utf-8");
28+
assertTrue(response.isXml());
29+
assertFalse(response.isJson());
30+
31+
response.setContentType("text/xml; charset=utf-8");
32+
assertTrue(response.isXml());
33+
assertFalse(response.isJson());
34+
35+
// Should work without charsets too
36+
response.setContentType("application/json");
37+
assertTrue(response.isJson());
38+
assertFalse(response.isXml());
39+
40+
response.setContentType("application/xml");
41+
assertTrue(response.isXml());
42+
assertFalse(response.isJson());
43+
44+
response.setContentType("text/xml");
45+
assertTrue(response.isXml());
46+
assertFalse(response.isJson());
47+
}
48+
49+
/**
50+
* Test response parsers.
51+
*/
52+
@Test
53+
public void testTwilioRestResponseParser() throws UnsupportedOperationException {
54+
TwilioRestResponse response = new TwilioRestResponse("http://example.com/test", "don't care", 200);
55+
56+
response.setContentType("application/json; charset=utf-8");
57+
assertTrue(response.getParser() instanceof JsonResponseParser);
58+
59+
response.setContentType("application/xml; charset=utf-8");
60+
assertTrue(response.getParser() instanceof XmlResponseParser);
61+
62+
response.setContentType("text/html");
63+
}
64+
65+
}

0 commit comments

Comments
 (0)