-
-
Notifications
You must be signed in to change notification settings - Fork 521
/
test_exceptions.py
224 lines (216 loc) · 7.76 KB
/
test_exceptions.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import unittest
from websockets.datastructures import Headers
from websockets.exceptions import *
from websockets.frames import Close, CloseCode
from websockets.http11 import Response
from .utils import DeprecationTestCase
class ExceptionsTests(unittest.TestCase):
def test_str(self):
for exception, exception_str in [
(
WebSocketException("something went wrong"),
"something went wrong",
),
(
ConnectionClosed(
Close(CloseCode.NORMAL_CLOSURE, ""),
Close(CloseCode.NORMAL_CLOSURE, ""),
True,
),
"received 1000 (OK); then sent 1000 (OK)",
),
(
ConnectionClosed(
Close(CloseCode.GOING_AWAY, "Bye!"),
Close(CloseCode.GOING_AWAY, "Bye!"),
False,
),
"sent 1001 (going away) Bye!; then received 1001 (going away) Bye!",
),
(
ConnectionClosed(
Close(CloseCode.NORMAL_CLOSURE, "race"),
Close(CloseCode.NORMAL_CLOSURE, "cond"),
True,
),
"received 1000 (OK) race; then sent 1000 (OK) cond",
),
(
ConnectionClosed(
Close(CloseCode.NORMAL_CLOSURE, "cond"),
Close(CloseCode.NORMAL_CLOSURE, "race"),
False,
),
"sent 1000 (OK) race; then received 1000 (OK) cond",
),
(
ConnectionClosed(
None,
Close(CloseCode.MESSAGE_TOO_BIG, ""),
None,
),
"sent 1009 (message too big); no close frame received",
),
(
ConnectionClosed(
Close(CloseCode.PROTOCOL_ERROR, ""),
None,
None,
),
"received 1002 (protocol error); no close frame sent",
),
(
ConnectionClosedOK(
Close(CloseCode.NORMAL_CLOSURE, ""),
Close(CloseCode.NORMAL_CLOSURE, ""),
True,
),
"received 1000 (OK); then sent 1000 (OK)",
),
(
ConnectionClosedError(
None,
None,
None,
),
"no close frame received or sent",
),
(
InvalidURI("|", "not at all!"),
"| isn't a valid URI: not at all!",
),
(
InvalidHandshake("invalid request"),
"invalid request",
),
(
SecurityError("redirect from WSS to WS"),
"redirect from WSS to WS",
),
(
InvalidMessage("malformed HTTP message"),
"malformed HTTP message",
),
(
InvalidStatus(Response(401, "Unauthorized", Headers())),
"server rejected WebSocket connection: HTTP 401",
),
(
InvalidHeader("Name"),
"missing Name header",
),
(
InvalidHeader("Name", None),
"missing Name header",
),
(
InvalidHeader("Name", ""),
"empty Name header",
),
(
InvalidHeader("Name", "Value"),
"invalid Name header: Value",
),
(
InvalidHeaderFormat("Sec-WebSocket-Protocol", "exp. token", "a=|", 3),
"invalid Sec-WebSocket-Protocol header: exp. token at 3 in a=|",
),
(
InvalidHeaderValue("Sec-WebSocket-Version", "42"),
"invalid Sec-WebSocket-Version header: 42",
),
(
InvalidOrigin("http://bad.origin"),
"invalid Origin header: http://bad.origin",
),
(
InvalidUpgrade("Upgrade"),
"missing Upgrade header",
),
(
InvalidUpgrade("Connection", "websocket"),
"invalid Connection header: websocket",
),
(
NegotiationError("unsupported subprotocol: spam"),
"unsupported subprotocol: spam",
),
(
DuplicateParameter("a"),
"duplicate parameter: a",
),
(
InvalidParameterName("|"),
"invalid parameter name: |",
),
(
InvalidParameterValue("a", None),
"missing value for parameter a",
),
(
InvalidParameterValue("a", ""),
"empty value for parameter a",
),
(
InvalidParameterValue("a", "|"),
"invalid value for parameter a: |",
),
(
ProtocolError("invalid opcode: 7"),
"invalid opcode: 7",
),
(
PayloadTooBig(None, 4),
"frame exceeds limit of 4 bytes",
),
(
PayloadTooBig(8, 4),
"frame with 8 bytes exceeds limit of 4 bytes",
),
(
PayloadTooBig(8, 4, 12),
"frame with 8 bytes after reading 12 bytes exceeds limit of 16 bytes",
),
(
InvalidState("WebSocket connection isn't established yet"),
"WebSocket connection isn't established yet",
),
(
ConcurrencyError("get() or get_iter() is already running"),
"get() or get_iter() is already running",
),
]:
with self.subTest(exception=exception):
self.assertEqual(str(exception), exception_str)
class DeprecationTests(DeprecationTestCase):
def test_connection_closed_attributes_deprecation(self):
exception = ConnectionClosed(Close(CloseCode.NORMAL_CLOSURE, "OK"), None, None)
with self.assertDeprecationWarning(
"ConnectionClosed.code is deprecated; "
"use Protocol.close_code or ConnectionClosed.rcvd.code"
):
self.assertEqual(exception.code, CloseCode.NORMAL_CLOSURE)
with self.assertDeprecationWarning(
"ConnectionClosed.reason is deprecated; "
"use Protocol.close_reason or ConnectionClosed.rcvd.reason"
):
self.assertEqual(exception.reason, "OK")
def test_connection_closed_attributes_deprecation_defaults(self):
exception = ConnectionClosed(None, None, None)
with self.assertDeprecationWarning(
"ConnectionClosed.code is deprecated; "
"use Protocol.close_code or ConnectionClosed.rcvd.code"
):
self.assertEqual(exception.code, CloseCode.ABNORMAL_CLOSURE)
with self.assertDeprecationWarning(
"ConnectionClosed.reason is deprecated; "
"use Protocol.close_reason or ConnectionClosed.rcvd.reason"
):
self.assertEqual(exception.reason, "")
def test_payload_too_big_with_message(self):
with self.assertDeprecationWarning(
"PayloadTooBig(message) is deprecated; "
"change to PayloadTooBig(size, max_size)",
):
exc = PayloadTooBig("payload length exceeds limit: 2 > 1 bytes")
self.assertEqual(str(exc), "payload length exceeds limit: 2 > 1 bytes")