-
Notifications
You must be signed in to change notification settings - Fork 748
/
Copy pathtest_client.py
146 lines (122 loc) · 5.2 KB
/
test_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import unittest
import aiounittest
try:
from unittest.mock import AsyncMock, Mock
except ImportError:
# Python 3.7
from mock import AsyncMock, Mock
from twilio.http.response import Response
from twilio.rest import Client
class TestRegionEdgeClients(unittest.TestCase):
def setUp(self):
self.client = Client("username", "password")
def test_set_client_edge_default_region(self):
self.client.edge = "edge"
self.assertEqual(
self.client.get_hostname("https://api.twilio.com"),
"https://api.edge.us1.twilio.com",
)
def test_set_client_region(self):
self.client.region = "region"
self.assertEqual(
self.client.get_hostname("https://api.twilio.com"),
"https://api.region.twilio.com",
)
def test_set_uri_region(self):
self.assertEqual(
self.client.get_hostname("https://api.region.twilio.com"),
"https://api.region.twilio.com",
)
def test_set_client_edge_region(self):
self.client.edge = "edge"
self.client.region = "region"
self.assertEqual(
self.client.get_hostname("https://api.twilio.com"),
"https://api.edge.region.twilio.com",
)
def test_set_client_edge_uri_region(self):
self.client.edge = "edge"
self.assertEqual(
self.client.get_hostname("https://api.region.twilio.com"),
"https://api.edge.region.twilio.com",
)
def test_set_client_region_uri_edge_region(self):
self.client.region = "region"
self.assertEqual(
self.client.get_hostname("https://api.edge.uriRegion.twilio.com"),
"https://api.edge.region.twilio.com",
)
def test_set_client_edge_uri_edge_region(self):
self.client.edge = "edge"
self.assertEqual(
self.client.get_hostname("https://api.uriEdge.region.twilio.com"),
"https://api.edge.region.twilio.com",
)
def test_set_uri_edge_region(self):
self.assertEqual(
self.client.get_hostname("https://api.edge.region.twilio.com"),
"https://api.edge.region.twilio.com",
)
def test_periods_in_query(self):
self.client.region = "region"
self.client.edge = "edge"
self.assertEqual(
self.client.get_hostname(
"https://api.twilio.com/path/to/something.json?foo=12.34"
),
"https://api.edge.region.twilio.com/path/to/something.json?foo=12.34",
)
class TestUserAgentClients(unittest.TestCase):
def setUp(self):
self.client = Client("username", "password")
def tearDown(self):
self.client.http_client.session.close()
def test_set_default_user_agent(self):
self.client.request("GET", "https://api.twilio.com/")
request_header = self.client.http_client._test_only_last_request.headers[
"User-Agent"
]
self.assertRegex(
request_header,
r"^twilio-python\/[0-9.]+(-rc\.[0-9]+)?\s\(\w+\s\w+\)\sPython\/[^\s]+$",
)
def test_set_user_agent_extensions(self):
expected_user_agent_extensions = ["twilio-run/2.0.0-test", "flex-plugin/3.4.0"]
self.client.user_agent_extensions = expected_user_agent_extensions
self.client.request("GET", "https://api.twilio.com/")
user_agent_headers = self.client.http_client._test_only_last_request.headers[
"User-Agent"
]
user_agent_extensions = user_agent_headers.split(" ")[
-len(expected_user_agent_extensions) :
]
self.assertEqual(user_agent_extensions, expected_user_agent_extensions)
class TestClientAsyncRequest(aiounittest.AsyncTestCase):
def setUp(self):
self.mock_async_http_client = AsyncMock()
self.mock_async_http_client.request.return_value = Response(200, "test")
self.mock_async_http_client.is_async = True
self.client = Client(
"username", "password", http_client=self.mock_async_http_client
)
async def test_raise_error_if_client_not_marked_async(self):
mock_http_client = Mock()
mock_http_client.request.return_value = Response(200, "doesnt matter")
mock_http_client.is_async = None
client = Client("username", "password", http_client=mock_http_client)
with self.assertRaises(RuntimeError):
await client.request_async("doesnt matter", "doesnt matter")
async def test_raise_error_if_client_is_not_async(self):
mock_http_client = Mock()
mock_http_client.request.return_value = Response(200, "doesnt matter")
mock_http_client.is_async = False
client = Client("username", "password", http_client=mock_http_client)
with self.assertRaises(RuntimeError):
await client.request_async("doesnt matter", "doesnt matter")
async def test_request_async_called_with_method_and_url(self):
await self.client.request_async("GET", "http://mock.twilio.com")
self.assertEqual(self.mock_async_http_client.request.call_args.args[0], "GET")
self.assertEqual(
self.mock_async_http_client.request.call_args.args[1],
"http://mock.twilio.com",
)