| 1 |
import io |
| 2 |
import os |
| 3 |
import re |
| 4 |
import unittest |
| 5 |
from unittest.mock import patch |
| 6 |
|
| 7 |
from websockets.cli import * |
| 8 |
from websockets.exceptions import ConnectionClosed |
| 9 |
from websockets.version import version |
| 10 |
|
| 11 |
# Run a test server in a thread. This is easier than running an asyncio server |
| 12 |
# because we would have to run main() in a thread, due to using asyncio.run(). |
| 13 |
from .sync.server import get_uri, run_server |
| 14 |
from .utils import SERVER_CONTEXT |
| 15 |
|
| 16 |
|
| 17 |
vt100_commands = re.compile(r"\x1b\[[A-Z]|\x1b[78]|\r") |
| 18 |
|
| 19 |
|
| 20 |
def remove_commands_and_prompts(output): |
| 21 |
return vt100_commands.sub("", output).replace("> ", "") |
| 22 |
|
| 23 |
|
| 24 |
def add_connection_messages(output, server_uri): |
| 25 |
return f"Connected to {server_uri}.\n{output}Connection closed: 1000 (OK).\n" |
| 26 |
|
| 27 |
|
| 28 |
def echo_handler(websocket): |
| 29 |
websocket.send(websocket.recv()) |
| 30 |
|
| 31 |
|
| 32 |
class CLITests(unittest.TestCase): |
| 33 |
def run_main(self, argv, inputs="", close_input=False, expected_exit_code=None): |
| 34 |
# Replace sys.stdin with a file-like object backed by a file descriptor |
| 35 |
# so it be left open and block readline(), or be closed to simulate EOF. |
| 36 |
stdin_read_fd, stdin_write_fd = os.pipe() |
| 37 |
stdin = open(stdin_read_fd) |
| 38 |
self.addCleanup(stdin.close) |
| 39 |
os.write(stdin_write_fd, inputs.encode()) |
| 40 |
if close_input: |
| 41 |
os.close(stdin_write_fd) |
| 42 |
else: |
| 43 |
self.addCleanup(os.close, stdin_write_fd) |
| 44 |
# Replace sys.stdout with a file-like object to record outputs. |
| 45 |
stdout = io.StringIO() |
| 46 |
with patch("sys.stdin", new=stdin), patch("sys.stdout", new=stdout): |
| 47 |
# Catch sys.exit() calls when expected. |
| 48 |
if expected_exit_code is not None: |
| 49 |
with self.assertRaises(SystemExit) as raised: |
| 50 |
main(argv) |
| 51 |
self.assertEqual(raised.exception.code, expected_exit_code) |
| 52 |
else: |
| 53 |
main(argv) |
| 54 |
return stdout.getvalue() |
| 55 |
|
| 56 |
def test_help(self): |
| 57 |
output = self.run_main(["--help"], expected_exit_code=0) |
| 58 |
output = vt100_commands.sub("", output) # remove colors |
| 59 |
self.assertTrue(output.startswith("usage: websockets ")) |
| 60 |
|
| 61 |
def test_version(self): |
| 62 |
output = self.run_main(["--version"], expected_exit_code=0) |
| 63 |
self.assertEqual(output, f"websockets {version}\n") |
| 64 |
|
| 65 |
def test_receive_text_message(self): |
| 66 |
def text_handler(websocket): |
| 67 |
websocket.send("café") |
| 68 |
|
| 69 |
with run_server(text_handler) as server: |
| 70 |
server_uri = get_uri(server) |
| 71 |
output = self.run_main([server_uri], "") |
| 72 |
self.assertEqual( |
| 73 |
remove_commands_and_prompts(output), |
| 74 |
add_connection_messages("\n< café\n", server_uri), |
| 75 |
) |
| 76 |
|
| 77 |
def test_receive_binary_message(self): |
| 78 |
def binary_handler(websocket): |
| 79 |
websocket.send(b"tea") |
| 80 |
|
| 81 |
with run_server(binary_handler) as server: |
| 82 |
server_uri = get_uri(server) |
| 83 |
output = self.run_main([server_uri], "") |
| 84 |
self.assertEqual( |
| 85 |
remove_commands_and_prompts(output), |
| 86 |
add_connection_messages("\n< (binary) 746561\n", server_uri), |
| 87 |
) |
| 88 |
|
| 89 |
def test_escape_control_characters(self): |
| 90 |
def handler(websocket): |
| 91 |
websocket.send("line1\nline2\rline3") |
| 92 |
|
| 93 |
with run_server(handler) as server: |
| 94 |
server_uri = get_uri(server) |
| 95 |
output = self.run_main([server_uri], "") |
| 96 |
self.assertEqual( |
| 97 |
remove_commands_and_prompts(output), |
| 98 |
add_connection_messages("\n< line1\\nline2\\rline3\n", server_uri), |
| 99 |
) |
| 100 |
|
| 101 |
def test_send_message(self): |
| 102 |
with run_server(echo_handler) as server: |
| 103 |
server_uri = get_uri(server) |
| 104 |
output = self.run_main([server_uri], "hello\n") |
| 105 |
self.assertEqual( |
| 106 |
remove_commands_and_prompts(output), |
| 107 |
add_connection_messages("\n< hello\n", server_uri), |
| 108 |
) |
| 109 |
|
| 110 |
def test_close_connection(self): |
| 111 |
def wait_handler(websocket): |
| 112 |
with self.assertRaises(ConnectionClosed): |
| 113 |
websocket.recv() |
| 114 |
|
| 115 |
with run_server(wait_handler) as server: |
| 116 |
server_uri = get_uri(server) |
| 117 |
output = self.run_main([server_uri], close_input=True) |
| 118 |
self.assertEqual( |
| 119 |
remove_commands_and_prompts(output), |
| 120 |
add_connection_messages("", server_uri), |
| 121 |
) |
| 122 |
|
| 123 |
def test_insecure_connection_rejected(self): |
| 124 |
with run_server(echo_handler, ssl=SERVER_CONTEXT) as server: |
| 125 |
server_uri = get_uri(server, secure=True) |
| 126 |
output = self.run_main([server_uri], "hello\n", expected_exit_code=1) |
| 127 |
self.assertTrue(output.startswith(f"Failed to connect to {server_uri}: ")) |
| 128 |
|
| 129 |
def test_insecure_connection_accepted(self): |
| 130 |
with run_server(echo_handler, ssl=SERVER_CONTEXT) as server: |
| 131 |
server_uri = get_uri(server, secure=True) |
| 132 |
output = self.run_main([server_uri, "--insecure"], "hello\n") |
| 133 |
self.assertEqual( |
| 134 |
remove_commands_and_prompts(output), |
| 135 |
add_connection_messages("\n< hello\n", server_uri), |
| 136 |
) |
| 137 |
|
| 138 |
def test_connection_failure(self): |
| 139 |
output = self.run_main(["ws://localhost:12345"], expected_exit_code=1) |
| 140 |
self.assertTrue( |
| 141 |
output.startswith("Failed to connect to ws://localhost:12345: ") |
| 142 |
) |
| 143 |
|
| 144 |
def test_no_args(self): |
| 145 |
output = self.run_main([], expected_exit_code=2) |
| 146 |
output = vt100_commands.sub("", output) # remove colors |
| 147 |
self.assertTrue(output.startswith("usage: websockets ")) |