Ejemplo n.º 1
0
    async def test_websocket_startup_and_bad_uri(self):
        muxer = websocket_muxer.WebsocketMuxer()
        async with WebsocketContextManager() as ws_ctx:
            await ws_ctx.serve(muxer.handle_session, "localhost", 8765)

            client = await ws_ctx.connect("ws://localhost:8765/florgus")

        self.assertEqual(client.close_code, server_codes.UNSUPPORTED_URI)
        self.assertRegex(client.close_reason, "path '.*florgus.*' not found")
Ejemplo n.º 2
0
    async def run_server(self):
        v1 = v1_server.MediaServer(self.controller, self.ml)
        muxer = websocket_muxer.WebsocketMuxer()
        muxer.register(v1_c_types.SERVING_ADDRESS, v1)

        await websockets.serve(muxer.handle_session, "localhost",
                               v1_c_types.DEFAULT_PORT)
        print_msg("Server running @ ws://localhost:%s..." %
                  v1_c_types.DEFAULT_PORT)
Ejemplo n.º 3
0
    async def test_websocket_returns_response(self):
        muxer = websocket_muxer.WebsocketMuxer()
        client_commands = commands_from(True, False, True)
        server_responses = events_from("blorgus1", "blorgus2", "blorgus3")
        test_server = TestServer(expect_list=wrap_all(*client_commands),
                                 response_list=wrap_all(*server_responses))
        rc = ResponseCollector()

        async with WebsocketContextManager() as ws_ctx:
            muxer.register("/florgus", test_server)
            await ws_ctx.serve(muxer.handle_session, "localhost", 8765)
            client = await ws_ctx.connect("ws://localhost:8765/florgus")

            collection_cr = rc.route_responses(client)
            commands_cr = send_commands(client, client_commands)

            await asyncio.gather(collection_cr, commands_cr)

        self.assertConnectionClosedSuccessfully(client)
        self.assertListEqual(rc.responses, server_responses)
Ejemplo n.º 4
0
    async def test_server_error(self):
        muxer = websocket_muxer.WebsocketMuxer()
        exception = server_exceptions.ClientError("test error")

        class ExceptionHandler(websocket_muxer.Server):
            async def accept(self, message: str,
                             _client: websocket_muxer.ClientSession) -> None:
                raise exception

        muxer.register("/exception_test", ExceptionHandler())

        async with WebsocketContextManager() as ws_ctx:
            await ws_ctx.serve(muxer.handle_session, "localhost", 8765)
            client = await ws_ctx.connect("ws://localhost:8765/exception_test")
            await client.send("literally anything")
            with self.assertRaises(websockets.ConnectionClosedError):
                await client.send("anything else")
                await client.recv()  # Force it to close out.

        self.assertEqual(client.close_code, server_codes.BAD_CLIENT)
        self.assertRegex(client.close_reason, ".*%s.*" % (str(exception), ))
Ejemplo n.º 5
0
    async def test_multiple_paths(self):
        muxer = websocket_muxer.WebsocketMuxer()

        client_commands_1 = commands_from(True, False)
        server_responses_1 = events_from("blorgus1", "blorgus2")
        test_server_1 = TestServer(expect_list=wrap_all(*client_commands_1),
                                   response_list=wrap_all(*server_responses_1))
        muxer.register("/test1", test_server_1)
        rc1 = ResponseCollector()

        client_commands_2 = commands_from(True)
        server_responses_2 = events_from("blorgus2")
        test_server_2 = TestServer(expect_list=wrap_all(*client_commands_2),
                                   response_list=wrap_all(*server_responses_2))
        muxer.register("/test2", test_server_2)
        rc2 = ResponseCollector()

        async with WebsocketContextManager() as ws_ctx:
            await ws_ctx.serve(muxer.handle_session, "localhost", 8765)

            client_1 = await ws_ctx.connect("ws://localhost:8765/test1")
            client_2 = await ws_ctx.connect("ws://localhost:8765/test2")
            commands_1_cr = send_commands(client_1, client_commands_1)
            responses_1_cr = rc1.route_responses(client_1)
            commands_2_cr = send_commands(client_2, client_commands_2)
            responses_2_cr = rc2.route_responses(client_2)

            await asyncio.gather(commands_1_cr, responses_1_cr, commands_2_cr,
                                 responses_2_cr)

        self.assertConnectionClosedSuccessfully(client_1)
        self.assertConnectionClosedSuccessfully(client_2)

        self.assertEqual(rc1.responses,
                         expected_messages(wrap_all(*server_responses_1)))
        self.assertEqual(rc2.responses,
                         expected_messages(wrap_all(*server_responses_2)))