Client closes the connection when receiving non-HTTP response from server.
(self)
| 221 | self.assertEqual(raised.exception.response.body.decode(), "👌") |
| 222 | |
| 223 | def test_junk_handshake(self): |
| 224 | """Client closes the connection when receiving non-HTTP response from server.""" |
| 225 | |
| 226 | class JunkHandler(socketserver.BaseRequestHandler): |
| 227 | def handle(self): |
| 228 | time.sleep(MS) # wait for the client to send the handshake request |
| 229 | self.request.send(b"220 smtp.invalid ESMTP Postfix\r\n") |
| 230 | self.request.recv(4096) # wait for the client to close the connection |
| 231 | self.request.close() |
| 232 | |
| 233 | server = socketserver.TCPServer(("localhost", 0), JunkHandler) |
| 234 | host, port = server.server_address |
| 235 | with server: |
| 236 | thread = threading.Thread(target=server.serve_forever, args=(MS,)) |
| 237 | thread.start() |
| 238 | try: |
| 239 | with self.assertRaises(InvalidMessage) as raised: |
| 240 | with connect(f"ws://{host}:{port}"): |
| 241 | self.fail("did not raise") |
| 242 | self.assertEqual( |
| 243 | str(raised.exception), |
| 244 | "did not receive a valid HTTP response", |
| 245 | ) |
| 246 | self.assertIsInstance(raised.exception.__cause__, ValueError) |
| 247 | self.assertEqual( |
| 248 | str(raised.exception.__cause__), |
| 249 | "unsupported protocol; expected HTTP/1.1: " |
| 250 | "220 smtp.invalid ESMTP Postfix", |
| 251 | ) |
| 252 | finally: |
| 253 | server.shutdown() |
| 254 | thread.join() |
| 255 | |
| 256 | |
| 257 | class SecureClientTests(unittest.TestCase): |