Beispiel #1
0
def test_secure_echo(ws):
    custom_port_range = range(1024, 65535)
    config = EvaluatorServerConfig(custom_port_range=custom_port_range)

    async def handler(websocket, path):
        msg = await websocket.recv()
        await websocket.send(msg)

    ws(
        config.host,
        config.port,
        handler,
        ssl=config.get_server_ssl_context(),
        sock=config.get_socket(),
    )
    with ExitStack() as stack:
        duplexer = SyncWebsocketDuplexer(
            f"wss://{config.host}:{config.port}",
            f"wss://{config.host}:{config.port}",
            cert=config.cert,
            token=None,
        )
        stack.callback(duplexer.stop)
        duplexer.send("Hello Secure World")
        assert next(duplexer.receive()) == "Hello Secure World"
Beispiel #2
0
def test_receive(unused_tcp_port, ws):
    async def handler(websocket, path):
        await websocket.send("Hello World")

    ws("localhost", unused_tcp_port, handler)
    with ExitStack() as stack:
        duplexer = SyncWebsocketDuplexer(
            f"ws://localhost:{unused_tcp_port}",
            f"ws://localhost:{unused_tcp_port}",
            None,
            None,
        )
        stack.callback(duplexer.stop)
        assert next(duplexer.receive()) == "Hello World"
Beispiel #3
0
def test_unexpected_close(unused_tcp_port, ws):
    async def handler(websocket, path):
        await websocket.close()

    ws("localhost", unused_tcp_port, handler)
    with ExitStack() as stack:
        duplexer = SyncWebsocketDuplexer(
            f"ws://localhost:{unused_tcp_port}",
            f"ws://localhost:{unused_tcp_port}",
            None,
            None,
        )
        stack.callback(duplexer.stop)
        with pytest.raises(ConnectionClosedOK):
            next(duplexer.receive())
Beispiel #4
0
 def track(self):
     with ExitStack() as stack:
         duplexer = self._ws_duplexer
         if not duplexer:
             duplexer = SyncWebsocketDuplexer(self._client_uri,
                                              self._base_uri, self._cert,
                                              self._token)
             stack.callback(duplexer.stop)
         for message in duplexer.receive():
             try:
                 event = from_json(
                     message,
                     data_unmarshaller=serialization.evaluator_unmarshaller)
             except DataUnmarshallerError:
                 event = from_json(message, data_unmarshaller=pickle.loads)
             yield event
             if event["type"] == identifiers.EVTYPE_EE_TERMINATED:
                 logger.debug(
                     f"monitor-{self._id} client received terminated")
                 break
Beispiel #5
0
def test_generator(unused_tcp_port, ws):
    async def handler(websocket, path):
        await websocket.send("one")
        await websocket.send("two")
        await websocket.send("three")
        await websocket.send("four")

    ws("localhost", unused_tcp_port, handler)
    with ExitStack() as stack:
        duplexer = SyncWebsocketDuplexer(
            f"ws://localhost:{unused_tcp_port}",
            f"ws://localhost:{unused_tcp_port}",
            None,
            None,
        )
        stack.callback(duplexer.stop)

        expected = ["one", "two", "three"]
        for msg in duplexer.receive():
            assert msg == expected.pop(0)

            # Cause a GeneratorExit
            if len(expected) == 1:
                break