| 1 |
import unittest |
| 2 |
|
| 3 |
from websockets.datastructures import Headers |
| 4 |
from websockets.exceptions import * |
| 5 |
from websockets.frames import Close, CloseCode |
| 6 |
from websockets.http11 import Response |
| 7 |
|
| 8 |
from .utils import DeprecationTestCase |
| 9 |
|
| 10 |
|
| 11 |
class ExceptionsTests(unittest.TestCase): |
| 12 |
def test_str(self): |
| 13 |
for exception, exception_str in [ |
| 14 |
( |
| 15 |
WebSocketException("something went wrong"), |
| 16 |
"something went wrong", |
| 17 |
), |
| 18 |
( |
| 19 |
ConnectionClosed( |
| 20 |
Close(CloseCode.NORMAL_CLOSURE, ""), |
| 21 |
Close(CloseCode.NORMAL_CLOSURE, ""), |
| 22 |
True, |
| 23 |
), |
| 24 |
"received 1000 (OK); then sent 1000 (OK)", |
| 25 |
), |
| 26 |
( |
| 27 |
ConnectionClosed( |
| 28 |
Close(CloseCode.GOING_AWAY, "Bye!"), |
| 29 |
Close(CloseCode.GOING_AWAY, "Bye!"), |
| 30 |
False, |
| 31 |
), |
| 32 |
"sent 1001 (going away) Bye!; then received 1001 (going away) Bye!", |
| 33 |
), |
| 34 |
( |
| 35 |
ConnectionClosed( |
| 36 |
Close(CloseCode.NORMAL_CLOSURE, "race"), |
| 37 |
Close(CloseCode.NORMAL_CLOSURE, "cond"), |
| 38 |
True, |
| 39 |
), |
| 40 |
"received 1000 (OK) race; then sent 1000 (OK) cond", |
| 41 |
), |
| 42 |
( |
| 43 |
ConnectionClosed( |
| 44 |
Close(CloseCode.NORMAL_CLOSURE, "cond"), |
| 45 |
Close(CloseCode.NORMAL_CLOSURE, "race"), |
| 46 |
False, |
| 47 |
), |
| 48 |
"sent 1000 (OK) race; then received 1000 (OK) cond", |
| 49 |
), |
| 50 |
( |
| 51 |
ConnectionClosed( |
| 52 |
None, |
| 53 |
Close(CloseCode.MESSAGE_TOO_BIG, ""), |
| 54 |
None, |
| 55 |
), |
| 56 |
"sent 1009 (message too big); no close frame received", |
| 57 |
), |
| 58 |
( |
| 59 |
ConnectionClosed( |
| 60 |
Close(CloseCode.PROTOCOL_ERROR, ""), |
| 61 |
None, |
| 62 |
None, |
| 63 |
), |
| 64 |
"received 1002 (protocol error); no close frame sent", |
| 65 |
), |
| 66 |
( |
| 67 |
ConnectionClosedOK( |
| 68 |
Close(CloseCode.NORMAL_CLOSURE, ""), |
| 69 |
Close(CloseCode.NORMAL_CLOSURE, ""), |
| 70 |
True, |
| 71 |
), |
| 72 |
"received 1000 (OK); then sent 1000 (OK)", |
| 73 |
), |
| 74 |
( |
| 75 |
ConnectionClosedError( |
| 76 |
None, |
| 77 |
None, |
| 78 |
None, |
| 79 |
), |
| 80 |
"no close frame received or sent", |
| 81 |
), |
| 82 |
( |
| 83 |
InvalidURI("|", "not at all!"), |
| 84 |
"| isn't a valid URI: not at all!", |
| 85 |
), |
| 86 |
( |
| 87 |
InvalidProxy("|", "not at all!"), |
| 88 |
"| isn't a valid proxy: not at all!", |
| 89 |
), |
| 90 |
( |
| 91 |
InvalidHandshake("invalid request"), |
| 92 |
"invalid request", |
| 93 |
), |
| 94 |
( |
| 95 |
SecurityError("redirect from WSS to WS"), |
| 96 |
"redirect from WSS to WS", |
| 97 |
), |
| 98 |
( |
| 99 |
RequestLineTooLong("read 8202 bytes, expected no more than 8192 bytes"), |
| 100 |
"read 8202 bytes, expected no more than 8192 bytes", |
| 101 |
), |
| 102 |
( |
| 103 |
StatusLineTooLong("read 8202 bytes, expected no more than 8192 bytes"), |
| 104 |
"read 8202 bytes, expected no more than 8192 bytes", |
| 105 |
), |
| 106 |
( |
| 107 |
HeaderLineTooLong("read 8202 bytes, expected no more than 8192 bytes"), |
| 108 |
"read 8202 bytes, expected no more than 8192 bytes", |
| 109 |
), |
| 110 |
( |
| 111 |
TooManyHeaders("expected no more than 128 headers"), |
| 112 |
"expected no more than 128 headers", |
| 113 |
), |
| 114 |
( |
| 115 |
ProxyError("failed to connect to SOCKS proxy"), |
| 116 |
"failed to connect to SOCKS proxy", |
| 117 |
), |
| 118 |
( |
| 119 |
InvalidMessage("malformed HTTP message"), |
| 120 |
"malformed HTTP message", |
| 121 |
), |
| 122 |
( |
| 123 |
InvalidMethod("POST"), |
| 124 |
"unsupported HTTP method: POST", |
| 125 |
), |
| 126 |
( |
| 127 |
InvalidProtocol("HTTP/2.0"), |
| 128 |
"unsupported HTTP version: HTTP/2.0", |
| 129 |
), |
| 130 |
( |
| 131 |
InvalidStatus(Response(401, "Unauthorized", Headers())), |
| 132 |
"server rejected WebSocket connection: HTTP 401", |
| 133 |
), |
| 134 |
( |
| 135 |
InvalidProxyMessage("malformed HTTP message"), |
| 136 |
"malformed HTTP message", |
| 137 |
), |
| 138 |
( |
| 139 |
InvalidProxyStatus(Response(401, "Unauthorized", Headers())), |
| 140 |
"proxy rejected connection: HTTP 401", |
| 141 |
), |
| 142 |
( |
| 143 |
InvalidHeader("Name"), |
| 144 |
"missing Name header", |
| 145 |
), |
| 146 |
( |
| 147 |
InvalidHeader("Name", None), |
| 148 |
"missing Name header", |
| 149 |
), |
| 150 |
( |
| 151 |
InvalidHeader("Name", ""), |
| 152 |
"empty Name header", |
| 153 |
), |
| 154 |
( |
| 155 |
InvalidHeader("Name", "Value"), |
| 156 |
"invalid Name header: Value", |
| 157 |
), |
| 158 |
( |
| 159 |
InvalidHeaderFormat("Sec-WebSocket-Protocol", "exp. token", "a=|", 3), |
| 160 |
"invalid Sec-WebSocket-Protocol header: exp. token at 3 in a=|", |
| 161 |
), |
| 162 |
( |
| 163 |
InvalidHeaderValue("Sec-WebSocket-Version", "42"), |
| 164 |
"invalid Sec-WebSocket-Version header: 42", |
| 165 |
), |
| 166 |
( |
| 167 |
InvalidOrigin("http://bad.origin"), |
| 168 |
"invalid Origin header: http://bad.origin", |
| 169 |
), |
| 170 |
( |
| 171 |
InvalidUpgrade("Upgrade"), |
| 172 |
"missing Upgrade header", |
| 173 |
), |
| 174 |
( |
| 175 |
InvalidUpgrade("Connection", "websocket"), |
| 176 |
"invalid Connection header: websocket", |
| 177 |
), |
| 178 |
( |
| 179 |
NegotiationError("unsupported subprotocol: spam"), |
| 180 |
"unsupported subprotocol: spam", |
| 181 |
), |
| 182 |
( |
| 183 |
DuplicateParameter("a"), |
| 184 |
"duplicate parameter: a", |
| 185 |
), |
| 186 |
( |
| 187 |
InvalidParameterName("|"), |
| 188 |
"invalid parameter name: |", |
| 189 |
), |
| 190 |
( |
| 191 |
InvalidParameterValue("a", None), |
| 192 |
"missing value for parameter a", |
| 193 |
), |
| 194 |
( |
| 195 |
InvalidParameterValue("a", ""), |
| 196 |
"empty value for parameter a", |
| 197 |
), |
| 198 |
( |
| 199 |
InvalidParameterValue("a", "|"), |
| 200 |
"invalid value for parameter a: |", |
| 201 |
), |
| 202 |
( |
| 203 |
ProtocolError("invalid opcode: 7"), |
| 204 |
"invalid opcode: 7", |
| 205 |
), |
| 206 |
( |
| 207 |
PayloadTooBig(None, 4), |
| 208 |
"frame exceeds limit of 4 bytes", |
| 209 |
), |
| 210 |
( |
| 211 |
PayloadTooBig(8, 4), |
| 212 |
"frame with 8 bytes exceeds limit of 4 bytes", |
| 213 |
), |
| 214 |
( |
| 215 |
PayloadTooBig(8, 4, 12), |
| 216 |
"frame with 8 bytes after reading 12 bytes exceeds limit of 16 bytes", |
| 217 |
), |
| 218 |
( |
| 219 |
InvalidState("WebSocket connection isn't established yet"), |
| 220 |
"WebSocket connection isn't established yet", |
| 221 |
), |
| 222 |
( |
| 223 |
ConcurrencyError("get() or get_iter() is already running"), |
| 224 |
"get() or get_iter() is already running", |
| 225 |
), |
| 226 |
]: |
| 227 |
with self.subTest(exception=exception): |
| 228 |
self.assertEqual(str(exception), exception_str) |
| 229 |
|
| 230 |
|
| 231 |
class DeprecationTests(DeprecationTestCase): |
| 232 |
def test_connection_closed_attributes_deprecation(self): |
| 233 |
exception = ConnectionClosed(Close(CloseCode.NORMAL_CLOSURE, "OK"), None, None) |
| 234 |
with self.assertDeprecationWarning( |
| 235 |
"ConnectionClosed.code is deprecated; " |
| 236 |
"use Protocol.close_code or ConnectionClosed.rcvd.code" |
| 237 |
): |
| 238 |
self.assertEqual(exception.code, CloseCode.NORMAL_CLOSURE) |
| 239 |
with self.assertDeprecationWarning( |
| 240 |
"ConnectionClosed.reason is deprecated; " |
| 241 |
"use Protocol.close_reason or ConnectionClosed.rcvd.reason" |
| 242 |
): |
| 243 |
self.assertEqual(exception.reason, "OK") |
| 244 |
|
| 245 |
def test_connection_closed_attributes_deprecation_defaults(self): |
| 246 |
exception = ConnectionClosed(None, None, None) |
| 247 |
with self.assertDeprecationWarning( |
| 248 |
"ConnectionClosed.code is deprecated; " |
| 249 |
"use Protocol.close_code or ConnectionClosed.rcvd.code" |
| 250 |
): |
| 251 |
self.assertEqual(exception.code, CloseCode.ABNORMAL_CLOSURE) |
| 252 |
with self.assertDeprecationWarning( |
| 253 |
"ConnectionClosed.reason is deprecated; " |
| 254 |
"use Protocol.close_reason or ConnectionClosed.rcvd.reason" |
| 255 |
): |
| 256 |
self.assertEqual(exception.reason, "") |
| 257 |
|
| 258 |
def test_payload_too_big_with_message(self): |
| 259 |
with self.assertDeprecationWarning( |
| 260 |
"PayloadTooBig(message) is deprecated; " |
| 261 |
"change to PayloadTooBig(size, max_size)", |
| 262 |
): |
| 263 |
exc = PayloadTooBig("payload length exceeds limit: 2 > 1 bytes") |
| 264 |
self.assertEqual(str(exc), "payload length exceeds limit: 2 > 1 bytes") |