| 1 |
import unittest |
| 2 |
|
| 3 |
from websockets.exceptions import InvalidHeaderFormat, InvalidHeaderValue |
| 4 |
from websockets.headers import * |
| 5 |
|
| 6 |
|
| 7 |
class HeadersTests(unittest.TestCase): |
| 8 |
def test_build_host(self): |
| 9 |
for (host, port, secure), (result, result_with_port) in [ |
| 10 |
(("localhost", 80, False), ("localhost", "localhost:80")), |
| 11 |
(("localhost", 8000, False), ("localhost:8000", "localhost:8000")), |
| 12 |
(("localhost", 443, True), ("localhost", "localhost:443")), |
| 13 |
(("localhost", 8443, True), ("localhost:8443", "localhost:8443")), |
| 14 |
(("example.com", 80, False), ("example.com", "example.com:80")), |
| 15 |
(("example.com", 8000, False), ("example.com:8000", "example.com:8000")), |
| 16 |
(("example.com", 443, True), ("example.com", "example.com:443")), |
| 17 |
(("example.com", 8443, True), ("example.com:8443", "example.com:8443")), |
| 18 |
(("127.0.0.1", 80, False), ("127.0.0.1", "127.0.0.1:80")), |
| 19 |
(("127.0.0.1", 8000, False), ("127.0.0.1:8000", "127.0.0.1:8000")), |
| 20 |
(("127.0.0.1", 443, True), ("127.0.0.1", "127.0.0.1:443")), |
| 21 |
(("127.0.0.1", 8443, True), ("127.0.0.1:8443", "127.0.0.1:8443")), |
| 22 |
(("::1", 80, False), ("[::1]", "[::1]:80")), |
| 23 |
(("::1", 8000, False), ("[::1]:8000", "[::1]:8000")), |
| 24 |
(("::1", 443, True), ("[::1]", "[::1]:443")), |
| 25 |
(("::1", 8443, True), ("[::1]:8443", "[::1]:8443")), |
| 26 |
]: |
| 27 |
with self.subTest(host=host, port=port, secure=secure): |
| 28 |
self.assertEqual( |
| 29 |
build_host(host, port, secure), |
| 30 |
result, |
| 31 |
) |
| 32 |
self.assertEqual( |
| 33 |
build_host(host, port, secure, always_include_port=True), |
| 34 |
result_with_port, |
| 35 |
) |
| 36 |
|
| 37 |
def test_parse_connection(self): |
| 38 |
for header, parsed in [ |
| 39 |
# Realistic use cases |
| 40 |
("Upgrade", ["Upgrade"]), # Safari, Chrome |
| 41 |
("keep-alive, Upgrade", ["keep-alive", "Upgrade"]), # Firefox |
| 42 |
# Pathological example |
| 43 |
(",,\t, , ,Upgrade ,,", ["Upgrade"]), |
| 44 |
]: |
| 45 |
with self.subTest(header=header): |
| 46 |
self.assertEqual(parse_connection(header), parsed) |
| 47 |
|
| 48 |
def test_parse_connection_invalid_header_format(self): |
| 49 |
for header in ["???", "keep-alive; Upgrade"]: |
| 50 |
with self.subTest(header=header): |
| 51 |
with self.assertRaises(InvalidHeaderFormat): |
| 52 |
parse_connection(header) |
| 53 |
|
| 54 |
def test_parse_upgrade(self): |
| 55 |
for header, parsed in [ |
| 56 |
# Realistic use case |
| 57 |
("websocket", ["websocket"]), |
| 58 |
# Synthetic example |
| 59 |
("http/3.0, websocket", ["http/3.0", "websocket"]), |
| 60 |
# Pathological example |
| 61 |
(",, WebSocket, \t,,", ["WebSocket"]), |
| 62 |
]: |
| 63 |
with self.subTest(header=header): |
| 64 |
self.assertEqual(parse_upgrade(header), parsed) |
| 65 |
|
| 66 |
def test_parse_upgrade_invalid_header_format(self): |
| 67 |
for header in ["???", "websocket 2", "http/3.0; websocket"]: |
| 68 |
with self.subTest(header=header): |
| 69 |
with self.assertRaises(InvalidHeaderFormat): |
| 70 |
parse_upgrade(header) |
| 71 |
|
| 72 |
def test_parse_extension(self): |
| 73 |
for header, parsed in [ |
| 74 |
# Synthetic examples |
| 75 |
("foo", [("foo", [])]), |
| 76 |
("foo, bar", [("foo", []), ("bar", [])]), |
| 77 |
( |
| 78 |
'foo; name; token=token; quoted-string="quoted-string", ' |
| 79 |
"bar; quux; quuux", |
| 80 |
[ |
| 81 |
( |
| 82 |
"foo", |
| 83 |
[ |
| 84 |
("name", None), |
| 85 |
("token", "token"), |
| 86 |
("quoted-string", "quoted-string"), |
| 87 |
], |
| 88 |
), |
| 89 |
("bar", [("quux", None), ("quuux", None)]), |
| 90 |
], |
| 91 |
), |
| 92 |
# Pathological example |
| 93 |
( |
| 94 |
",\t, , ,foo ;bar = 42,, baz,,", |
| 95 |
[("foo", [("bar", "42")]), ("baz", [])], |
| 96 |
), |
| 97 |
# Realistic use cases for permessage-deflate |
| 98 |
("permessage-deflate", [("permessage-deflate", [])]), |
| 99 |
( |
| 100 |
"permessage-deflate; client_max_window_bits", |
| 101 |
[("permessage-deflate", [("client_max_window_bits", None)])], |
| 102 |
), |
| 103 |
( |
| 104 |
"permessage-deflate; server_max_window_bits=10", |
| 105 |
[("permessage-deflate", [("server_max_window_bits", "10")])], |
| 106 |
), |
| 107 |
]: |
| 108 |
with self.subTest(header=header): |
| 109 |
self.assertEqual(parse_extension(header), parsed) |
| 110 |
# Also ensure that build_extension round-trips cleanly. |
| 111 |
unparsed = build_extension(parsed) |
| 112 |
self.assertEqual(parse_extension(unparsed), parsed) |
| 113 |
|
| 114 |
def test_parse_extension_invalid_header_format(self): |
| 115 |
for header in [ |
| 116 |
# Truncated examples |
| 117 |
"", |
| 118 |
",\t,", |
| 119 |
"foo;", |
| 120 |
"foo; bar;", |
| 121 |
"foo; bar=", |
| 122 |
'foo; bar="baz', |
| 123 |
# Wrong delimiter |
| 124 |
"foo, bar, baz=quux; quuux", |
| 125 |
# Value in quoted string parameter that isn't a token |
| 126 |
'foo; bar=" "', |
| 127 |
]: |
| 128 |
with self.subTest(header=header): |
| 129 |
with self.assertRaises(InvalidHeaderFormat): |
| 130 |
parse_extension(header) |
| 131 |
|
| 132 |
def test_parse_subprotocol(self): |
| 133 |
for header, parsed in [ |
| 134 |
# Synthetic examples |
| 135 |
("foo", ["foo"]), |
| 136 |
("foo, bar", ["foo", "bar"]), |
| 137 |
# Pathological example |
| 138 |
(",\t, , ,foo ,, bar,baz,,", ["foo", "bar", "baz"]), |
| 139 |
]: |
| 140 |
with self.subTest(header=header): |
| 141 |
self.assertEqual(parse_subprotocol(header), parsed) |
| 142 |
# Also ensure that build_subprotocol round-trips cleanly. |
| 143 |
unparsed = build_subprotocol(parsed) |
| 144 |
self.assertEqual(parse_subprotocol(unparsed), parsed) |
| 145 |
|
| 146 |
def test_parse_subprotocol_invalid_header(self): |
| 147 |
for header in [ |
| 148 |
# Truncated examples |
| 149 |
"", |
| 150 |
",\t,", |
| 151 |
# Wrong delimiter |
| 152 |
"foo; bar", |
| 153 |
]: |
| 154 |
with self.subTest(header=header): |
| 155 |
with self.assertRaises(InvalidHeaderFormat): |
| 156 |
parse_subprotocol(header) |
| 157 |
|
| 158 |
def test_validate_subprotocols(self): |
| 159 |
for subprotocols in [[], ["sip"], ["v1.usp"], ["sip", "v1.usp"]]: |
| 160 |
with self.subTest(subprotocols=subprotocols): |
| 161 |
validate_subprotocols(subprotocols) |
| 162 |
|
| 163 |
def test_validate_subprotocols_invalid(self): |
| 164 |
for subprotocols, exception in [ |
| 165 |
({"sip": None}, TypeError), |
| 166 |
("sip", TypeError), |
| 167 |
([""], ValueError), |
| 168 |
]: |
| 169 |
with self.subTest(subprotocols=subprotocols): |
| 170 |
with self.assertRaises(exception): |
| 171 |
validate_subprotocols(subprotocols) |
| 172 |
|
| 173 |
def test_build_www_authenticate_basic(self): |
| 174 |
# Test vector from RFC 7617 |
| 175 |
self.assertEqual( |
| 176 |
build_www_authenticate_basic("foo"), 'Basic realm="foo", charset="UTF-8"' |
| 177 |
) |
| 178 |
|
| 179 |
def test_build_www_authenticate_basic_invalid_realm(self): |
| 180 |
# Realm contains a control character forbidden in quoted-string encoding |
| 181 |
with self.assertRaises(ValueError): |
| 182 |
build_www_authenticate_basic("\u0007") |
| 183 |
|
| 184 |
def test_build_authorization_basic(self): |
| 185 |
# Test vector from RFC 7617 |
| 186 |
self.assertEqual( |
| 187 |
build_authorization_basic("Aladdin", "open sesame"), |
| 188 |
"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==", |
| 189 |
) |
| 190 |
|
| 191 |
def test_build_authorization_basic_utf8(self): |
| 192 |
# Test vector from RFC 7617 |
| 193 |
self.assertEqual( |
| 194 |
build_authorization_basic("test", "123£"), "Basic dGVzdDoxMjPCow==" |
| 195 |
) |
| 196 |
|
| 197 |
def test_parse_authorization_basic(self): |
| 198 |
for header, parsed in [ |
| 199 |
("Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==", ("Aladdin", "open sesame")), |
| 200 |
# Password contains non-ASCII character |
| 201 |
("Basic dGVzdDoxMjPCow==", ("test", "123£")), |
| 202 |
# Password contains a colon |
| 203 |
("Basic YWxhZGRpbjpvcGVuOnNlc2FtZQ==", ("aladdin", "open:sesame")), |
| 204 |
# Scheme name must be case insensitive |
| 205 |
("basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==", ("Aladdin", "open sesame")), |
| 206 |
]: |
| 207 |
with self.subTest(header=header): |
| 208 |
self.assertEqual(parse_authorization_basic(header), parsed) |
| 209 |
|
| 210 |
def test_parse_authorization_basic_invalid_header_format(self): |
| 211 |
for header in [ |
| 212 |
"// Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==", |
| 213 |
"Basic\tQWxhZGRpbjpvcGVuIHNlc2FtZQ==", |
| 214 |
"Basic ****************************", |
| 215 |
"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== //", |
| 216 |
]: |
| 217 |
with self.subTest(header=header): |
| 218 |
with self.assertRaises(InvalidHeaderFormat): |
| 219 |
parse_authorization_basic(header) |
| 220 |
|
| 221 |
def test_parse_authorization_basic_invalid_header_value(self): |
| 222 |
for header in [ |
| 223 |
"Digest ...", |
| 224 |
"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ", |
| 225 |
"Basic QWxhZGNlc2FtZQ==", |
| 226 |
]: |
| 227 |
with self.subTest(header=header): |
| 228 |
with self.assertRaises(InvalidHeaderValue): |
| 229 |
parse_authorization_basic(header) |