10000 Support both encoded utf-8 strings and raw unicode strings · MC6/twilio-python@5e20dbc · GitHub
[go: up one dir, main page]

Skip to content

Commit 5e20dbc

Browse files
committed
Support both encoded utf-8 strings and raw unicode strings
1 parent 98d507b commit 5e20dbc

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

tests/test_unicode.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,56 @@
22
from mock import patch, Mock
33
from twilio.rest import resources
44

5+
@patch("httplib2.Http")
6+
@patch("twilio.rest.resources.Response")
7+
def test_ascii_encode(resp_mock, mock):
8+
http = mock.return_value
9+
http.request.return_value = (Mock(), Mock())
10+
11+
data = {
12+
"body": "HeyHey".encode('utf-8')
13+
}
14+
15+
resources.make_request("GET", "http://www.example.com", data=data)
16+
17+
http.request.assert_called_with("http://www.example.com", "GET",
18+
headers=None, body="body=HeyHey")
19+
20+
21+
@patch("httplib2.Http")
22+
@patch("twilio.rest.resources.Response")
23+
def test_ascii(resp_mock, mock):
24+
http = mock.return_value
25+
http.request.return_value = (Mock(), Mock())
26+
27+
data = {
28+
"body": "HeyHey"
29+
}
30+
31+
resources.make_request("GET", "http://www.example.com", data=data)
32+
33+
http.request.assert_called_with("http://www.example.com", "GET",
34+
headers=None, body="body=HeyHey")
35+
36+
37+
@patch("httplib2.Http")
38+
@patch("twilio.rest.resources.Response")
39+
def test_double_encoding(resp_mock, mock):
40+
http = mock.return_value
41+
http.request.return_value = (Mock(), Mock())
42+
43+
body = u"Chloéñ"
44+
45+
data = {
46+
"body": body.encode('utf-8'),
47+
}
48+
49+
resources.make_request("GET", "http://www.example.com", data=data)
50+
51+
http.request.assert_called_with("http://www.example.com", "GET",
52+
headers=None, body="body=Chlo%C3%A9%C3%B1")
53+
54+
555
@patch("httplib2.Http")
656
@patch("twilio.rest.resources.Response")
757
def test_paging(resp_mock, mock):

twilio/rest/resources.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ def make_request(method, url,
128128
if data is not None:
129129
udata = {}
130130
for k, v in data.iteritems():
131-
udata[k.encode('utf-8')] = unicode(v).encode('utf-8')
131+
try:
132+
udata[k.encode('utf-8')] = unicode(v).encode('utf-8')
133+
except UnicodeDecodeError:
134+
udata[k.encode('utf-8')] = v
132135
data = urlencode(udata)
133136

134137
if params is not None:
@@ -806,7 +809,7 @@ def transfer(self, account_sid):
806809

807810
def update(self, **kwargs):
808811
"""
809-
Update this phone number instance
812+
Update this phone number instance.
810813
"""
811814
a = self.parent.update(self.name, **kwargs)
812815
self.load(a.__dict__)

0 commit comments

Comments
 (0)
0