Skip to content

Commit 582d052

Browse files
committed
Fix issues identified by typing
These are bugs or potential bugs identified by adding typing to the codebase.
1 parent f4b3339 commit 582d052

File tree

4 files changed

+71
-60
lines changed

4 files changed

+71
-60
lines changed

test/test_extensions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_accept(self):
1717

1818
def test_finalize(self):
1919
ext = wpext.Extension()
20-
assert ext.finalize(None) is None
20+
ext.finalize(None)
2121

2222
def test_frame_inbound_header(self):
2323
ext = wpext.Extension()
@@ -26,7 +26,7 @@ def test_frame_inbound_header(self):
2626

2727
def test_frame_inbound_payload_data(self):
2828
ext = wpext.Extension()
29-
data = object()
29+
data = b""
3030
assert ext.frame_inbound_payload_data(None, data) == data
3131

3232
def test_frame_inbound_complete(self):
@@ -36,5 +36,5 @@ def test_frame_inbound_complete(self):
3636
def test_frame_outbound(self):
3737
ext = wpext.Extension()
3838
rsv = fp.RsvBits(True, True, True)
39-
data = object()
39+
data = b""
4040
assert ext.frame_outbound(None, None, rsv, data, None) == (rsv, data)

test/test_handshake.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_rejected_handshake():
3737
def test_initiate_upgrade_as_client():
3838
client = H11Handshake(CLIENT)
3939
with pytest.raises(LocalProtocolError):
40-
client.initiate_upgrade_connection([], b"/")
40+
client.initiate_upgrade_connection([], "/")
4141

4242

4343
def test_send_invalid_event():

test/test_server.py

Lines changed: 66 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ def _make_connection_request(request_headers, method="GET"):
3030
def test_connection_request():
3131
event = _make_connection_request(
3232
[
33-
("Host", "localhost"),
34-
("Connection", "Keep-Alive, Upgrade"),
35-
("Upgrade", "WebSocket"),
36-
("Sec-WebSocket-Version", "13"),
37-
("Sec-WebSocket-Key", generate_nonce()),
38-
("X-Foo", "bar"),
33+
(b"Host", b"localhost"),
34+
(b"Connection", b"Keep-Alive, Upgrade"),
35+
(b"Upgrade", b"WebSocket"),
36+
(b"Sec-WebSocket-Version", b"13"),
37+
(b"Sec-WebSocket-Key", generate_nonce()),
38+
(b"X-Foo", b"bar"),
3939
]
4040
)
4141

@@ -57,11 +57,11 @@ def test_connection_request_bad_method():
5757
with pytest.raises(RemoteProtocolError) as excinfo:
5858
event = _make_connection_request(
5959
[
60-
("Host", "localhost"),
61-
("Connection", "Keep-Alive, Upgrade"),
62-
("Upgrade", "WebSocket"),
63-
("Sec-WebSocket-Version", "13"),
64-
("Sec-WebSocket-Key", generate_nonce()),
60+
(b"Host", b"localhost"),
61+
(b"Connection", b"Keep-Alive, Upgrade"),
62+
(b"Upgrade", b"WebSocket"),
63+
(b"Sec-WebSocket-Version", b"13"),
64+
(b"Sec-WebSocket-Key", generate_nonce()),
6565
],
6666
method="POST",
6767
)
@@ -72,11 +72,11 @@ def test_connection_request_bad_connection_header():
7272
with pytest.raises(RemoteProtocolError) as excinfo:
7373
event = _make_connection_request(
7474
[
75-
("Host", "localhost"),
76-
("Connection", "Keep-Alive, No-Upgrade"),
77-
("Upgrade", "WebSocket"),
78-
("Sec-WebSocket-Version", "13"),
79-
("Sec-WebSocket-Key", generate_nonce()),
75+
(b"Host", b"localhost"),
76+
(b"Connection", b"Keep-Alive, No-Upgrade"),
77+
(b"Upgrade", b"WebSocket"),
78+
(b"Sec-WebSocket-Version", b"13"),
79+
(b"Sec-WebSocket-Key", generate_nonce()),
8080
]
8181
)
8282
assert str(excinfo.value) == "Missing header, 'Connection: Upgrade'"
@@ -86,26 +86,26 @@ def test_connection_request_bad_upgrade_header():
8686
with pytest.raises(RemoteProtocolError) as excinfo:
8787
event = _make_connection_request(
8888
[
89-
("Host", "localhost"),
90-
("Connection", "Keep-Alive, Upgrade"),
91-
("Upgrade", "h2c"),
92-
("Sec-WebSocket-Version", "13"),
93-
("Sec-WebSocket-Key", generate_nonce()),
89+
(b"Host", b"localhost"),
90+
(b"Connection", b"Keep-Alive, Upgrade"),
91+
(b"Upgrade", b"h2c"),
92+
(b"Sec-WebSocket-Version", b"13"),
93+
(b"Sec-WebSocket-Key", generate_nonce()),
9494
]
9595
)
9696
assert str(excinfo.value) == "Missing header, 'Upgrade: WebSocket'"
9797

9898

99-
@pytest.mark.parametrize("version", ["12", "not-a-digit"])
99+
@pytest.mark.parametrize("version", [b"12", b"not-a-digit"])
100100
def test_connection_request_bad_version_header(version):
101101
with pytest.raises(RemoteProtocolError) as excinfo:
102102
event = _make_connection_request(
103103
[
104-
("Host", "localhost"),
105-
("Connection", "Keep-Alive, Upgrade"),
106-
("Upgrade", "WebSocket"),
107-
("Sec-WebSocket-Version", version),
108-
("Sec-WebSocket-Key", generate_nonce()),
104+
(b"Host", b"localhost"),
105+
(b"Connection", b"Keep-Alive, Upgrade"),
106+
(b"Upgrade", b"WebSocket"),
107+
(b"Sec-WebSocket-Version", version),
108+
(b"Sec-WebSocket-Key", generate_nonce()),
109109
]
110110
)
111111
assert str(excinfo.value) == "Missing header, 'Sec-WebSocket-Version'"
@@ -118,10 +118,10 @@ def test_connection_request_key_header():
118118
with pytest.raises(RemoteProtocolError) as excinfo:
119119
event = _make_connection_request(
120120
[
121-
("Host", "localhost"),
122-
("Connection", "Keep-Alive, Upgrade"),
123-
("Upgrade", "WebSocket"),
124-
("Sec-WebSocket-Version", "13"),
121+
(b"Host", b"localhost"),
122+
(b"Connection", b"Keep-Alive, Upgrade"),
123+
(b"Upgrade", b"WebSocket"),
124+
(b"Sec-WebSocket-Version", b"13"),
125125
]
126126
)
127127
assert str(excinfo.value) == "Missing header, 'Sec-WebSocket-Key'"
@@ -138,7 +138,7 @@ def test_upgrade_request():
138138
(b"Sec-WebSocket-Key", generate_nonce()),
139139
(b"X-Foo", b"bar"),
140140
],
141-
b"/",
141+
"/",
142142
)
143143
event = next(server.events())
144144

@@ -168,11 +168,11 @@ def _make_handshake(
168168
method="GET",
169169
target="/",
170170
headers=[
171-
("Host", "localhost"),
172-
("Connection", "Keep-Alive, Upgrade"),
173-
("Upgrade", "WebSocket"),
174-
("Sec-WebSocket-Version", "13"),
175-
("Sec-WebSocket-Key", nonce),
171+
(b"Host", b"localhost"),
172+
(b"Connection", b"Keep-Alive, Upgrade"),
173+
(b"Upgrade", b"WebSocket"),
174+
(b"Sec-WebSocket-Version", b"13"),
175+
(b"Sec-WebSocket-Key", nonce),
176176
]
177177
+ request_headers,
178178
)
@@ -198,9 +198,9 @@ def test_handshake():
198198
assert response == h11.InformationalResponse(
199199
status_code=101,
200200
headers=[
201-
("connection", "Upgrade"),
202-
("sec-websocket-accept", generate_accept_token(nonce)),
203-
("upgrade", "WebSocket"),
201+
(b"connection", b"Upgrade"),
202+
(b"sec-websocket-accept", generate_accept_token(nonce)),
203+
(b"upgrade", b"WebSocket"),
204204
],
205205
)
206206

@@ -212,18 +212,18 @@ def test_handshake_extra_headers():
212212
assert response == h11.InformationalResponse(
213213
status_code=101,
214214
headers=[
215-
("connection", "Upgrade"),
216-
("sec-websocket-accept", generate_accept_token(nonce)),
217-
("upgrade", "WebSocket"),
218-
("x-foo", "bar"),
215+
(b"connection", b"Upgrade"),
216+
(b"sec-websocket-accept", generate_accept_token(nonce)),
217+
(b"upgrade", b"WebSocket"),
218+
(b"x-foo", b"bar"),
219219
],
220220
)
221221

222222

223223
@pytest.mark.parametrize("accept_subprotocol", ["one", "two"])
224224
def test_handshake_with_subprotocol(accept_subprotocol):
225225
response, _ = _make_handshake(
226-
[("Sec-Websocket-Protocol", "one, two")], subprotocol=accept_subprotocol
226+
[(b"Sec-Websocket-Protocol", b"one, two")], subprotocol=accept_subprotocol
227227
)
228228

229229
headers = normed_header_dict(response.headers)
@@ -233,7 +233,8 @@ def test_handshake_with_subprotocol(accept_subprotocol):
233233
def test_handshake_with_extension():
234234
extension = FakeExtension(accept_response=True)
235235
response, _ = _make_handshake(
236-
[("Sec-Websocket-Extensions", extension.name)], extensions=[extension]
236+
[(b"Sec-Websocket-Extensions", extension.name.encode("ascii"))],
237+
extensions=[extension],
237238
)
238239

239240
headers = normed_header_dict(response.headers)
@@ -245,7 +246,12 @@ def test_handshake_with_extension_params():
245246
accepted_params = "parameter1=value1; parameter2=value2"
246247
extension = FakeExtension(accept_response=accepted_params)
247248
response, _ = _make_handshake(
248-
[("Sec-Websocket-Extensions", "%s; %s" % (extension.name, offered_params))],
249+
[
250+
(
251+
b"Sec-Websocket-Extensions",
252+
("%s; %s" % (extension.name, offered_params)).encode("ascii"),
253+
)
254+
],
249255
extensions=[extension],
250256
)
251257

@@ -259,7 +265,12 @@ def test_handshake_with_extension_params():
259265
def test_handshake_with_extra_unaccepted_extension():
260266
extension = FakeExtension(accept_response=True)
261267
response, _ = _make_handshake(
262-
[("Sec-Websocket-Extensions", "pretend, %s" % extension.name)],
268+
[
269+
(
270+
b"Sec-Websocket-Extensions",
271+
b"pretend, %s" % extension.name.encode("ascii"),
272+
)
273+
],
263274
extensions=[extension],
264275
)
265276

@@ -284,11 +295,11 @@ def _make_handshake_rejection(status_code, body=None):
284295
method="GET",
285296
target="/",
286297
headers=[
287-
("Host", "localhost"),
288-
("Connection", "Keep-Alive, Upgrade"),
289-
("Upgrade", "WebSocket"),
290-
("Sec-WebSocket-Version", "13"),
291-
("Sec-WebSocket-Key", nonce),
298+
(b"Host", b"localhost"),
299+
(b"Connection", b"Keep-Alive, Upgrade"),
300+
(b"Upgrade", b"WebSocket"),
301+
(b"Sec-WebSocket-Version", b"13"),
302+
(b"Sec-WebSocket-Key", nonce),
292303
],
293304
)
294305
)
@@ -317,15 +328,15 @@ def _make_handshake_rejection(status_code, body=None):
317328
def test_handshake_rejection():
318329
events = _make_handshake_rejection(400)
319330
assert events == [
320-
h11.Response(headers=[("content-length", "0")], status_code=400),
331+
h11.Response(headers=[(b"content-length", b"0")], status_code=400),
321332
h11.EndOfMessage(),
322333
]
323334

324335

325336
def test_handshake_rejection_with_body():
326337
events = _make_handshake_rejection(400, body=b"Hello")
327338
assert events == [
328-
h11.Response(headers=[("content-length", "5")], status_code=400),
339+
h11.Response(headers=[(b"content-length", b"5")], status_code=400),
329340
h11.Data(data=b"Hello"),
330341
h11.EndOfMessage(),
331342
]

wsproto/handshake.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ def _reject(self, event):
278278

279279
headers = event.headers
280280
if not event.has_body:
281-
headers.append(("content-length", "0"))
281+
headers.append((b"content-length", b"0"))
282282
response = h11.Response(status_code=event.status_code, headers=headers)
283283
data = self._h11_connection.send(response)
284284
self._state = ConnectionState.REJECTING

0 commit comments

Comments
 (0)