Debian Codegraph @

tests/test_proxy.py

raw
1 import os
2 import unittest
3 from unittest.mock import patch
4
5 from websockets.exceptions import InvalidProxy
6 from websockets.http11 import USER_AGENT
7 from websockets.proxy import *
8 from websockets.proxy import prepare_connect_request
9 from websockets.uri import parse_uri
10
11
12 VALID_PROXIES = [
13 (
14 "http://proxy:8080",
15 Proxy("http", "proxy", 8080, None, None),
16 ),
17 (
18 "https://proxy:8080",
19 Proxy("https", "proxy", 8080, None, None),
20 ),
21 (
22 "http://proxy",
23 Proxy("http", "proxy", 80, None, None),
24 ),
25 (
26 "http://proxy:8080/",
27 Proxy("http", "proxy", 8080, None, None),
28 ),
29 (
30 "http://PROXY:8080",
31 Proxy("http", "proxy", 8080, None, None),
32 ),
33 (
34 "http://user:pass@proxy:8080",
35 Proxy("http", "proxy", 8080, "user", "pass"),
36 ),
37 (
38 "http://høst:8080/",
39 Proxy("http", "xn--hst-0na", 8080, None, None),
40 ),
41 (
42 "http://üser:påss@høst:8080",
43 Proxy("http", "xn--hst-0na", 8080, "%C3%BCser", "p%C3%A5ss"),
44 ),
45 ]
46
47 INVALID_PROXIES = [
48 "ws://proxy:8080",
49 "wss://proxy:8080",
50 "http://proxy:8080/path",
51 "http://proxy:8080/?query",
52 "http://proxy:8080/#fragment",
53 "http://user@proxy",
54 "http:///",
55 ]
56
57 PROXIES_WITH_USER_INFO = [
58 ("http://proxy", None),
59 ("http://user:pass@proxy", ("user", "pass")),
60 ("http://üser:påss@høst", ("%C3%BCser", "p%C3%A5ss")),
61 ]
62
63 PROXY_ENVS = [
64 (
65 {"ws_proxy": "http://proxy:8080"},
66 "ws://example.com/",
67 "http://proxy:8080",
68 ),
69 (
70 {"ws_proxy": "http://proxy:8080"},
71 "wss://example.com/",
72 None,
73 ),
74 (
75 {"wss_proxy": "http://proxy:8080"},
76 "ws://example.com/",
77 None,
78 ),
79 (
80 {"wss_proxy": "http://proxy:8080"},
81 "wss://example.com/",
82 "http://proxy:8080",
83 ),
84 (
85 {"http_proxy": "http://proxy:8080"},
86 "ws://example.com/",
87 "http://proxy:8080",
88 ),
89 (
90 {"http_proxy": "http://proxy:8080"},
91 "wss://example.com/",
92 None,
93 ),
94 (
95 {"https_proxy": "http://proxy:8080"},
96 "ws://example.com/",
97 "http://proxy:8080",
98 ),
99 (
100 {"https_proxy": "http://proxy:8080"},
101 "wss://example.com/",
102 "http://proxy:8080",
103 ),
104 (
105 {"socks_proxy": "http://proxy:1080"},
106 "ws://example.com/",
107 "socks5h://proxy:1080",
108 ),
109 (
110 {"socks_proxy": "http://proxy:1080"},
111 "wss://example.com/",
112 "socks5h://proxy:1080",
113 ),
114 (
115 {"ws_proxy": "http://proxy1:8080", "wss_proxy": "http://proxy2:8080"},
116 "ws://example.com/",
117 "http://proxy1:8080",
118 ),
119 (
120 {"ws_proxy": "http://proxy1:8080", "wss_proxy": "http://proxy2:8080"},
121 "wss://example.com/",
122 "http://proxy2:8080",
123 ),
124 (
125 {"http_proxy": "http://proxy1:8080", "https_proxy": "http://proxy2:8080"},
126 "ws://example.com/",
127 "http://proxy2:8080",
128 ),
129 (
130 {"http_proxy": "http://proxy1:8080", "https_proxy": "http://proxy2:8080"},
131 "wss://example.com/",
132 "http://proxy2:8080",
133 ),
134 (
135 {"https_proxy": "http://proxy:8080", "socks_proxy": "http://proxy:1080"},
136 "ws://example.com/",
137 "socks5h://proxy:1080",
138 ),
139 (
140 {"https_proxy": "http://proxy:8080", "socks_proxy": "http://proxy:1080"},
141 "wss://example.com/",
142 "socks5h://proxy:1080",
143 ),
144 (
145 {"socks_proxy": "http://proxy:1080", "no_proxy": ".local"},
146 "ws://example.local/",
147 None,
148 ),
149 ]
150
151 CONNECT_REQUESTS = [
152 (
153 {"https_proxy": "http://proxy:8080"},
154 "ws://example.com/",
155 (
156 b"CONNECT example.com:80 HTTP/1.1\r\nHost: example.com\r\n"
157 b"User-Agent: " + USER_AGENT.encode() + b"\r\n\r\n"
158 ),
159 ),
160 (
161 {"https_proxy": "http://proxy:8080"},
162 "wss://example.com/",
163 (
164 b"CONNECT example.com:443 HTTP/1.1\r\nHost: example.com\r\n"
165 b"User-Agent: " + USER_AGENT.encode() + b"\r\n\r\n"
166 ),
167 ),
168 (
169 {"https_proxy": "http://hello:iloveyou@proxy:8080"},
170 "ws://example.com/",
171 (
172 b"CONNECT example.com:80 HTTP/1.1\r\nHost: example.com\r\n"
173 b"User-Agent: " + USER_AGENT.encode() + b"\r\n"
174 b"Proxy-Authorization: Basic aGVsbG86aWxvdmV5b3U=\r\n\r\n"
175 ),
176 ),
177 ]
178
179 CONNECT_REQUESTS_WITH_USER_AGENT = [
180 (
181 "Smith",
182 (
183 b"CONNECT example.com:80 HTTP/1.1\r\nHost: example.com\r\n"
184 b"User-Agent: Smith\r\n\r\n"
185 ),
186 ),
187 (
188 None,
189 b"CONNECT example.com:80 HTTP/1.1\r\nHost: example.com\r\n\r\n",
190 ),
191 ]
192
193
194 class ProxyTests(unittest.TestCase):
195 def test_parse_valid_proxies(self):
196 for proxy, parsed in VALID_PROXIES:
197 with self.subTest(proxy=proxy):
198 self.assertEqual(parse_proxy(proxy), parsed)
199
200 def test_parse_invalid_proxies(self):
201 for proxy in INVALID_PROXIES:
202 with self.subTest(proxy=proxy):
203 with self.assertRaises(InvalidProxy):
204 parse_proxy(proxy)
205
206 def test_parse_proxy_user_info(self):
207 for proxy, user_info in PROXIES_WITH_USER_INFO:
208 with self.subTest(proxy=proxy):
209 self.assertEqual(parse_proxy(proxy).user_info, user_info)
210
211 def test_get_proxy(self):
212 for environ, uri, proxy in PROXY_ENVS:
213 with patch.dict(os.environ, environ):
214 with self.subTest(environ=environ, uri=uri):
215 self.assertEqual(get_proxy(parse_uri(uri)), proxy)
216
217 def test_prepare_connect_request(self):
218 for environ, uri, request in CONNECT_REQUESTS:
219 with patch.dict(os.environ, environ):
220 with self.subTest(environ=environ, uri=uri):
221 uri = parse_uri(uri)
222 proxy = parse_proxy(get_proxy(uri))
223 self.assertEqual(prepare_connect_request(proxy, uri), request)
224
225 def test_prepare_connect_request_with_user_agent(self):
226 for user_agent_header, request in CONNECT_REQUESTS_WITH_USER_AGENT:
227 with self.subTest(user_agent_header=user_agent_header):
228 uri = parse_uri("ws://example.com")
229 proxy = parse_proxy("http://proxy:8080")
230 self.assertEqual(
231 prepare_connect_request(proxy, uri, user_agent_header),
232 request,
233 )