|
| 1 | +import http |
1 | 2 | import logging
|
2 | 3 | import socket
|
3 | 4 | import socketserver
|
|
6 | 7 | import time
|
7 | 8 | import unittest
|
8 | 9 |
|
9 |
| -from websockets.exceptions import InvalidHandshake, InvalidURI |
| 10 | +from websockets.exceptions import InvalidHandshake, InvalidStatus, InvalidURI |
10 | 11 | from websockets.extensions.permessage_deflate import PerMessageDeflate
|
11 | 12 | from websockets.sync.client import *
|
12 | 13 |
|
@@ -156,6 +157,36 @@ def close_connection(self, request):
|
156 | 157 | "connection closed while reading HTTP status line",
|
157 | 158 | )
|
158 | 159 |
|
| 160 | + def test_http_response(self): |
| 161 | + """Client reads HTTP response.""" |
| 162 | + |
| 163 | + def http_response(connection, request): |
| 164 | + return connection.respond(http.HTTPStatus.OK, "👌") |
| 165 | + |
| 166 | + with run_server(process_request=http_response) as server: |
| 167 | + with self.assertRaises(InvalidStatus) as raised: |
| 168 | + with connect(get_uri(server)): |
| 169 | + self.fail("did not raise") |
| 170 | + |
| 171 | + self.assertEqual(raised.exception.response.status_code, 200) |
| 172 | + self.assertEqual(raised.exception.response.body.decode(), "👌") |
| 173 | + |
| 174 | + def test_http_response_without_content_length(self): |
| 175 | + """Client reads HTTP response without a Content-Length header.""" |
| 176 | + |
| 177 | + def http_response(connection, request): |
| 178 | + response = connection.respond(http.HTTPStatus.OK, "👌") |
| 179 | + del response.headers["Content-Length"] |
| 180 | + return response |
| 181 | + |
| 182 | + with run_server(process_request=http_response) as server: |
| 183 | + with self.assertRaises(InvalidStatus) as raised: |
| 184 | + with connect(get_uri(server)): |
| 185 | + self.fail("did not raise") |
| 186 | + |
| 187 | + self.assertEqual(raised.exception.response.status_code, 200) |
| 188 | + self.assertEqual(raised.exception.response.body.decode(), "👌") |
| 189 | + |
159 | 190 | def test_junk_handshake(self):
|
160 | 191 | """Client closes the connection when receiving non-HTTP response from server."""
|
161 | 192 |
|
|
0 commit comments