MCPcopy Index your code
hub / github.com/python-websockets/websockets / test_junk_handshake

Method test_junk_handshake

tests/asyncio/test_server.py:456–479  ·  view source on GitHub ↗

Server closes the connection when receiving non-HTTP request from client.

(self)

Source from the content-addressed store, hash-verified

454 writer.close()
455
456 async def test_junk_handshake(self):
457 """Server closes the connection when receiving non-HTTP request from client."""
458 with self.assertLogs("websockets", logging.ERROR) as logs:
459 async with serve(*args) as server:
460 reader, writer = await asyncio.open_connection(*get_host_port(server))
461 writer.write(b"HELO relay.invalid\r\n")
462 try:
463 # Wait for the server to close the connection.
464 self.assertEqual(await reader.read(4096), b"")
465 finally:
466 writer.close()
467
468 self.assertEqual(
469 [record.getMessage() for record in logs.records],
470 ["opening handshake failed"],
471 )
472 self.assertEqual(
473 [str(record.exc_info[1]) for record in logs.records],
474 ["did not receive a valid HTTP request"],
475 )
476 self.assertEqual(
477 [str(record.exc_info[1].__cause__) for record in logs.records],
478 ["invalid HTTP request line: HELO relay.invalid"],
479 )
480
481 async def test_close_server_rejects_connecting_connections(self):
482 """Server rejects connecting connections with HTTP 503 when closing."""

Callers

nothing calls this directly

Calls 5

get_host_portFunction · 0.85
readMethod · 0.80
serveFunction · 0.50
writeMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected