| 1 |
import unittest |
| 2 |
|
| 3 |
from websockets.exceptions import InvalidURI |
| 4 |
from websockets.uri import * |
| 5 |
|
| 6 |
|
| 7 |
VALID_URIS = [ |
| 8 |
( |
| 9 |
"ws://localhost/", |
| 10 |
WebSocketURI(False, "localhost", 80, "/", "", None, None), |
| 11 |
), |
| 12 |
( |
| 13 |
"wss://localhost/", |
| 14 |
WebSocketURI(True, "localhost", 443, "/", "", None, None), |
| 15 |
), |
| 16 |
( |
| 17 |
"ws://localhost", |
| 18 |
WebSocketURI(False, "localhost", 80, "", "", None, None), |
| 19 |
), |
| 20 |
( |
| 21 |
"ws://localhost/path?query", |
| 22 |
WebSocketURI(False, "localhost", 80, "/path", "query", None, None), |
| 23 |
), |
| 24 |
( |
| 25 |
"ws://localhost/path;params", |
| 26 |
WebSocketURI(False, "localhost", 80, "/path;params", "", None, None), |
| 27 |
), |
| 28 |
( |
| 29 |
"WS://LOCALHOST/PATH?QUERY", |
| 30 |
WebSocketURI(False, "localhost", 80, "/PATH", "QUERY", None, None), |
| 31 |
), |
| 32 |
( |
| 33 |
"ws://user:pass@localhost/", |
| 34 |
WebSocketURI(False, "localhost", 80, "/", "", "user", "pass"), |
| 35 |
), |
| 36 |
( |
| 37 |
"ws://høst/", |
| 38 |
WebSocketURI(False, "xn--hst-0na", 80, "/", "", None, None), |
| 39 |
), |
| 40 |
( |
| 41 |
"ws://üser:påss@høst/πass?qùéry", |
| 42 |
WebSocketURI( |
| 43 |
False, |
| 44 |
"xn--hst-0na", |
| 45 |
80, |
| 46 |
"/%CF%80ass", |
| 47 |
"q%C3%B9%C3%A9ry", |
| 48 |
"%C3%BCser", |
| 49 |
"p%C3%A5ss", |
| 50 |
), |
| 51 |
), |
| 52 |
] |
| 53 |
|
| 54 |
INVALID_URIS = [ |
| 55 |
"http://localhost/", |
| 56 |
"https://localhost/", |
| 57 |
"ws://localhost/path#fragment", |
| 58 |
"ws://user@localhost/", |
| 59 |
"ws:///path", |
| 60 |
] |
| 61 |
|
| 62 |
URIS_WITH_RESOURCE_NAMES = [ |
| 63 |
("ws://localhost/", "/"), |
| 64 |
("ws://localhost", "/"), |
| 65 |
("ws://localhost/path?query", "/path?query"), |
| 66 |
("ws://høst/πass?qùéry", "/%CF%80ass?q%C3%B9%C3%A9ry"), |
| 67 |
] |
| 68 |
|
| 69 |
URIS_WITH_USER_INFO = [ |
| 70 |
("ws://localhost/", None), |
| 71 |
("ws://user:pass@localhost/", ("user", "pass")), |
| 72 |
("ws://üser:påss@høst/", ("%C3%BCser", "p%C3%A5ss")), |
| 73 |
] |
| 74 |
|
| 75 |
|
| 76 |
class URITests(unittest.TestCase): |
| 77 |
def test_parse_valid_uris(self): |
| 78 |
for uri, parsed in VALID_URIS: |
| 79 |
with self.subTest(uri=uri): |
| 80 |
self.assertEqual(parse_uri(uri), parsed) |
| 81 |
|
| 82 |
def test_parse_invalid_uris(self): |
| 83 |
for uri in INVALID_URIS: |
| 84 |
with self.subTest(uri=uri): |
| 85 |
with self.assertRaises(InvalidURI): |
| 86 |
parse_uri(uri) |
| 87 |
|
| 88 |
def test_parse_resource_name(self): |
| 89 |
for uri, resource_name in URIS_WITH_RESOURCE_NAMES: |
| 90 |
with self.subTest(uri=uri): |
| 91 |
self.assertEqual(parse_uri(uri).resource_name, resource_name) |
| 92 |
|
| 93 |
def test_parse_user_info(self): |
| 94 |
for uri, user_info in URIS_WITH_USER_INFO: |
| 95 |
with self.subTest(uri=uri): |
| 96 |
self.assertEqual(parse_uri(uri).user_info, user_info) |