|
3 | 3 |
|
4 | 4 | Uses the awesome httpbin.org to validate responses
|
5 | 5 | """
|
6 |
| -import base64 |
7 | 6 | import platform
|
8 |
| -import unittest |
9 | 7 |
|
10 |
| -from httplib2 import Response |
| 8 | +import twilio |
11 | 9 | from nose.tools import assert_equal, raises
|
12 | 10 | from mock import patch, Mock, ANY
|
13 |
| - |
14 |
| -import twilio |
15 | 11 | from twilio.rest.exceptions import TwilioRestException
|
16 | 12 | from twilio.rest.resources.base import make_request, make_twilio_request
|
17 | 13 | from twilio.rest.resources.connection import Connection
|
18 | 14 | from twilio.rest.resources.connection import PROXY_TYPE_SOCKS5
|
19 | 15 |
|
20 |
| - |
21 | 16 | get_headers = {
|
22 | 17 | "User-Agent": "twilio-python/{version} (Python {python_version})".format(
|
23 | 18 | version=twilio.__version__,
|
|
31 | 26 | post_headers["Content-Type"] = "application/x-www-form-urlencoded"
|
32 | 27 |
|
33 | 28 |
|
34 |
| -class MakeRequestTest(unittest.TestCase): |
35 |
| - |
36 |
| - @patch('twilio.rest.resources.base.Response') |
37 |
| - @patch('httplib2.Http') |
38 |
| - def test_get_params(self, http_mock, response_mock): |
39 |
| - http = Mock() |
40 |
| - http.request.return_value = (Mock(), Mock()) |
41 |
| - http_mock.return_value = http |
42 |
| - make_request("GET", "http://httpbin.org/get", params={"hey": "you"}) |
43 |
| - http.request.assert_called_with("http://httpbin.org/get?hey=you", "GET", |
44 |
| - body=None, headers=None) |
45 |
| - |
46 |
| - @patch('twilio.rest.resources.base.Response') |
47 |
| - @patch('httplib2.Http') |
48 |
| - def test_get_extra_params(self, http_mock, response_mock): |
49 |
| - http = Mock() |
50 |
| - http.request.return_value = (Mock(), Mock()) |
51 |
| - http_mock.return_value = http |
52 |
| - make_request("GET", "http://httpbin.org/get?foo=bar", params={"hey": "you"}) |
53 |
| - http.request.assert_called_with("http://httpbin.org/get?foo=bar&hey=you", "GET", |
54 |
| - body=None, headers=None) |
55 |
| - |
56 |
| - @patch('twilio.rest.resources.base.Response') |
57 |
| - @patch('httplib2.Http') |
58 |
| - def test_resp_uri(self, http_mock, response_mock): |
59 |
| - http = Mock() |
60 |
| - http.request.return_value = (Mock(), Mock()) |
61 |
| - http_mock.return_value = http |
62 |
| - make_request("GET", "http://httpbin.org/get") |
63 |
| - http.request.assert_called_with("http://httpbin.org/get", "GET", |
64 |
| - body=None, headers=None) |
65 |
| - |
66 |
| - @patch('twilio.rest.resources.base.Response') |
67 |
| - @patch('httplib2.Http') |
68 |
| - def test_sequence_data(self, http_mock, response_mock): |
69 |
| - http = Mock() |
70 |
| - http.request.return_value = (Mock(), Mock()) |
71 |
| - http_mock.return_value = http |
72 |
| - make_request( |
73 |
| - "POST", |
74 |
| - "http://httpbin.org/post", |
75 |
| - data={"a_list": ["here", "is", "some", "stuff"]}, |
76 |
| - ) |
77 |
| - http.request.assert_called_with( |
78 |
| - "http://httpbin.org/post", |
79 |
| - "POST", |
80 |
| - body="a_list=here&a_list=is&a_list=some&a_list=stuff", |
81 |
| - headers=None, |
82 |
| - ) |
83 |
| - |
84 |
| - @patch('twilio.rest.resources.base.Response') |
85 |
| - @patch('httplib2.Http._conn_request') |
86 |
| - def test_make_request_basic_auth(self, mock_request, mock_response): |
87 |
| - response = Response({ |
88 |
| - 'status': '401', |
89 |
| - 'WWW-Authenticate': 'Basic realm="Twilio API"' |
90 |
| - }) |
91 |
| - mock_request.side_effect = [(response, Mock()), (Mock(), Mock())] |
92 |
| - make_request('GET', 'http://httpbin.org/get', auth=('AC123', 'AuthToken')) |
93 |
| - |
94 |
| - auth = "{0}:{1}".format('AC123', 'AuthToken') |
95 |
| - encoded_auth = auth.encode('utf-8') |
96 |
| - b64_auth = base64.b64encode(encoded_auth) |
97 |
| - |
98 |
| - mock_request.assert_called_with( |
99 |
| - ANY, |
100 |
| - '/get', |
101 |
| - 'GET', |
102 |
| - None, |
103 |
| - { |
104 |
| - 'accept-encoding': 'gzip, deflate', |
105 |
| - 'authorization': 'Basic {0}'.format(b64_auth.decode('utf-8')), |
106 |
| - 'user-agent': ANY, |
107 |
| - } |
108 |
| - ) |
109 |
| - |
110 |
| - @patch('twilio.rest.resources.base.make_request') |
111 |
| - def test_make_twilio_request_headers(self, mock): |
112 |
| - url = "http://random/url" |
113 |
| - make_twilio_request("POST", url, use_json_extension=True) |
114 |
| - mock.assert_called_with("POST", "http://random/url.json", |
115 |
| - headers=post_headers) |
116 |
| - |
117 |
| - @raises(TwilioRestException) |
118 |
| - @patch('twilio.rest.resources.base.make_request') |
119 |
| - def test_make_twilio_request_bad_data(self, mock): |
120 |
| - resp = Mock() |
121 |
| - resp.ok = False |
122 |
| - resp.return_value = "error" |
123 |
| - mock.return_value = resp |
124 |
| - |
125 |
| - url = "http://random/url" |
126 |
| - make_twilio_request("POST", url) |
127 |
| - mock.assert_called_with("POST", "http://random/url.json", |
128 |
| - headers=post_headers) |
129 |
| - |
130 |
| - @patch('twilio.rest.resources.base.Response') |
131 |
| - @patch('httplib2.Http') |
132 |
| - def test_proxy_info(self, http_mock, resp_mock): |
133 |
| - http = Mock() |
134 |
| - http.request.return_value = (Mock(), Mock()) |
135 |
| - http_mock.return_value = http |
136 |
| - Connection.set_proxy_info( |
137 |
| - 'example.com', |
138 |
| - 8080, |
139 |
| - proxy_type=PROXY_TYPE_SOCKS5, |
140 |
| - ) |
141 |
| - make_request("GET", "http://httpbin.org/get") |
142 |
| - http_mock.assert_called_with(timeout=None, ca_certs=ANY, proxy_info=ANY) |
143 |
| - http.request.assert_called_with("http://httpbin.org/get", "GET", |
144 |
| - body=None, headers=None) |
145 |
| - proxy_info = http_mock.call_args[1]['proxy_info'] |
146 |
| - assert_equal(proxy_info.proxy_host, 'example.com') |
147 |
| - assert_equal(proxy_info.proxy_port, 8080) |
148 |
| - assert_equal(proxy_info.proxy_type, PROXY_TYPE_SOCKS5) |
| 29 | +@patch('twilio.rest.resources.base.Response') |
| 30 | +@patch('httplib2.Http') |
| 31 | +def test_get_params(http_mock, response_mock): |
| 32 | + http = Mock() |
| 33 | + http.request.return_value = (Mock(), Mock()) |
| 34 | + http_mock.return_value = http |
| 35 | + make_request("GET", "http://httpbin.org/get", params={"hey": "you"}) |
| 36 | + http.request.assert_called_with("http://httpbin.org/get?hey=you", "GET", |
| 37 | + body=None, headers=None) |
| 38 | + |
| 39 | + |
| 40 | +@patch('twilio.rest.resources.base.Response') |
| 41 | +@patch('httplib2.Http') |
| 42 | +def test_get_extra_params(http_mock, response_mock): |
| 43 | + http = Mock() |
| 44 | + http.request.return_value = (Mock(), Mock()) |
| 45 | + http_mock.return_value = http |
| 46 | + make_request("GET", "http://httpbin.org/get?foo=bar", params={"hey": "you"}) |
| 47 | + http.request.assert_called_with("http://httpbin.org/get?foo=bar&hey=you", "GET", |
| 48 | + body=None, headers=None) |
| 49 | + |
| 50 | + |
| 51 | +@patch('twilio.rest.resources.base.Response') |
| 52 | +@patch('httplib2.Http') |
| 53 | +def test_resp_uri(http_mock, response_mock): |
| 54 | + http = Mock() |
| 55 | + http.request.return_value = (Mock(), Mock()) |
| 56 | + http_mock.return_value = http |
| 57 | + make_request("GET", "http://httpbin.org/get") |
| 58 | + http.request.assert_called_with("http://httpbin.org/get", "GET", |
| 59 | + body=None, headers=None) |
| 60 | + |
| 61 | + |
| 62 | +@patch('twilio.rest.resources.base.Response') |
| 63 | +@patch('httplib2.Http') |
| 64 | +def test_sequence_data(http_mock, response_mock): |
| 65 | + http = Mock() |
| 66 | + http.request.return_value = (Mock(), Mock()) |
| 67 | + http_mock.return_value = http |
| 68 | + make_request( |
| 69 | + "POST", |
| 70 | + "http://httpbin.org/post", |
| 71 | + data={"a_list": ["here", "is", "some", "stuff"]}, |
| 72 | + ) |
| 73 | + http.request.assert_called_with( |
| 74 | + "http://httpbin.org/post", |
| 75 | + "POST", |
| 76 | + body="a_list=here&a_list=is&a_list=some&a_list=stuff", |
| 77 | + headers=None, |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +@patch('twilio.rest.resources.base.make_request') |
| 82 | +def test_make_twilio_request_headers(mock): |
| 83 | + url = "http://random/url" |
| 84 | + make_twilio_request("POST", url, use_json_extension=True) |
| 85 | + mock.assert_called_with("POST", "http://random/url.json", |
| 86 | + headers=post_headers) |
| 87 | + |
| 88 | + |
| 89 | +@raises(TwilioRestException) |
| 90 | +@patch('twilio.rest.resources.base.make_request') |
| 91 | +def test_make_twilio_request_bad_data(mock): |
| 92 | + resp = Mock() |
| 93 | + resp.ok = False |
| 94 | + resp.return_value = "error" |
| 95 | + mock.return_value = resp |
| 96 | + |
| 97 | + url = "http://random/url" |
| 98 | + make_twilio_request("POST", url) |
| 99 | + mock.assert_called_with("POST", "http://random/url.json", |
| 100 | + headers=post_headers) |
| 101 | + |
| 102 | + |
| 103 | +@patch('twilio.rest.resources.base.Response') |
| 104 | +@patch('httplib2.Http') |
| 105 | +def test_proxy_info(http_mock, resp_mock): |
| 106 | + http = Mock() |
| 107 | + http.request.return_value = (Mock(), Mock()) |
| 108 | + http_mock.return_value = http |
| 109 | + Connection.set_proxy_info( |
| 110 | + 'example.com', |
| 111 | + 8080, |
| 112 | + proxy_type=PROXY_TYPE_SOCKS5, |
| 113 | + ) |
| 114 | + make_request("GET", "http://httpbin.org/get") |
| 115 | + http_mock.assert_called_with(timeout=None, ca_certs=ANY, proxy_info=ANY) |
| 116 | + http.request.assert_called_with("http://httpbin.org/get", "GET", |
| 117 | + body=None, headers=None) |
| 118 | + proxy_info = http_mock.call_args[1]['proxy_info'] |
| 119 | + assert_equal(proxy_info.proxy_host, 'example.com') |
| 120 | + assert_equal(proxy_info.proxy_port, 8080) |
| 121 | + assert_equal(proxy_info.proxy_type, PROXY_TYPE_SOCKS5) |
| 122 | +>>>>>>> parent of 6bf0e94... Merge remote-tracking branch 'origin/signal-beta' into edge |
0 commit comments