|
3 | 3 |
|
4 | 4 | Uses the awesome httpbin.org to validate responses
|
5 | 5 | """
|
| 6 | +import base64 |
6 | 7 | import platform
|
| 8 | +import unittest |
7 | 9 |
|
8 |
| -import twilio |
| 10 | +from httplib2 import Response |
9 | 11 | from nose.tools import assert_equal, raises
|
10 | 12 | from mock import patch, Mock, ANY
|
| 13 | + |
| 14 | +import twilio |
11 | 15 | from twilio.rest.exceptions import TwilioRestException
|
12 | 16 | from twilio.rest.resources.base import make_request, make_twilio_request
|
13 | 17 | from twilio.rest.resources.connection import Connection
|
14 | 18 | from twilio.rest.resources.connection import PROXY_TYPE_SOCKS5
|
15 | 19 |
|
| 20 | + |
16 | 21 | get_headers = {
|
17 | 22 | "User-Agent": "twilio-python/{version} (Python {python_version})".format(
|
18 | 23 | version=twilio.__version__,
|
|
26 | 31 | post_headers["Content-Type"] = "application/x-www-form-urlencoded"
|
27 | 32 |
|
28 | 33 |
|
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) |
| 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 | + mock_request.assert_called_with( |
| 94 | + ANY, |
| 95 | + '/get', |
| 96 | + 'GET', |
| 97 | + None, |
| 98 | + { |
| 99 | + 'accept-encoding': 'gzip, deflate', |
| 100 | + 'authorization': 'Basic {}'.format( |
| 101 | + base64.b64encode("{}:{}".format('AC123', 'AuthToken')) |
| 102 | + ), |
| 103 | + 'user-agent': ANY, |
| 104 | + } |
| 105 | + ) |
| 106 | + |
| 107 | + @patch('twilio.rest.resources.base.make_request') |
| 108 | + def test_make_twilio_request_headers(self, mock): |
| 109 | + url = "http://random/url" |
| 110 | + make_twilio_request("POST", url, use_json_extension=True) |
| 111 | + mock.assert_called_with("POST", "http://random/url.json", |
| 112 | + headers=post_headers) |
| 113 | + |
| 114 | + @raises(TwilioRestException) |
| 115 | + @patch('twilio.rest.resources.base.make_request') |
| 116 | + def test_make_twilio_request_bad_data(self, mock): |
| 117 | + resp = Mock() |
| 118 | + resp.ok = False |
| 119 | + resp.return_value = "error" |
| 120 | + mock.return_value = resp |
| 121 | + |
| 122 | + url = "http://random/url" |
| 123 | + make_twilio_request("POST", url) |
| 124 | + mock.assert_called_with("POST", "http://random/url.json", |
| 125 | + headers=post_headers) |
| 126 | + |
| 127 | + @patch('twilio.rest.resources.base.Response') |
| 128 | + @patch('httplib2.Http') |
| 129 | + def test_proxy_info(self, http_mock, resp_mock): |
| 130 | + http = Mock() |
| 131 | + http.request.return_value = (Mock(), Mock()) |
| 132 | + http_mock.return_value = http |
| 133 | + Connection.set_proxy_info( |
| 134 | + 'example.com', |
| 135 | + 8080, |
| 136 | + proxy_type=PROXY_TYPE_SOCKS5, |
| 137 | + ) |
| 138 | + make_request("GET", "http://httpbin.org/get") |
| 139 | + http_mock.assert_called_with(timeout=None, ca_certs=ANY, proxy_info=ANY) |
| 140 | + http.request.assert_called_with("http://httpbin.org/get", "GET", |
| 141 | + body=None, headers=None) |
| 142 | + proxy_info = http_mock.call_args[1]['proxy_info'] |
| 143 | + assert_equal(proxy_info.proxy_host, 'example.com') |
| 144 | + assert_equal(proxy_info.proxy_port, 8080) |
| 145 | + assert_equal(proxy_info.proxy_type, PROXY_TYPE_SOCKS5) |
0 commit comments