示例#1
0
 def __init__(
     self,
     path: str,
     event_loop: asyncio.AbstractEventLoop,
     *,
     framework: Type[ASGIFramework] = EchoFramework,
 ) -> None:
     self.transport = MockTransport()
     self.server = WebsocketServer(  # type: ignore
         framework, event_loop, Config(), self.transport)
     self.connection = WSConnection(ConnectionType.CLIENT)
     self.server.data_received(
         self.connection.send(Request(target=path, host="hypercorn")))
示例#2
0
class MockWebsocketConnection:
    def __init__(
        self,
        path: str,
        event_loop: asyncio.AbstractEventLoop,
        *,
        framework: Type[ASGIFramework] = EchoFramework,
    ) -> None:
        self.transport = MockTransport()
        self.server = WebsocketServer(  # type: ignore
            framework, event_loop, Config(), self.transport)
        self.connection = WSConnection(ConnectionType.CLIENT)
        self.server.data_received(
            self.connection.send(Request(target=path, host="hypercorn")))

    async def send(self, data: AnyStr) -> None:
        self.server.data_received(self.connection.send(Message(data=data)))
        await asyncio.sleep(0)  # Allow the server to respond

    async def receive(self) -> List[Message]:
        await self.transport.updated.wait()
        self.connection.receive_data(self.transport.data)
        self.transport.clear()
        return [event for event in self.connection.events()]

    def close(self) -> None:
        self.server.data_received(
            self.connection.send(CloseConnection(code=1000)))
示例#3
0
class MockHTTPConnection:
    def __init__(
        self,
        path: str,
        event_loop: asyncio.AbstractEventLoop,
        *,
        framework: Type[ASGIFramework] = EchoFramework,
    ) -> None:
        self.transport = MockTransport()
        self.client = h11.Connection(h11.CLIENT)
        self.server = WebsocketServer(  # type: ignore
            framework, event_loop, Config(), self.transport)
        self.server.data_received(
            self.client.send(
                h11.Request(
                    method="GET",
                    target=path,
                    headers=[
                        ("Host", "Hypercorn"),
                        ("Upgrade", "WebSocket"),
                        ("Connection", "Upgrade"),
                        ("Sec-WebSocket-Version", "13"),
                        ("Sec-WebSocket-Key", "121312"),
                    ],
                )))

    def get_events(self) -> list:
        events = []
        self.client.receive_data(self.transport.data)
        while True:
            event = self.client.next_event()
            if event in (h11.NEED_DATA, h11.PAUSED):
                break
            events.append(event)
            if isinstance(event, h11.ConnectionClosed):
                break
        return events
示例#4
0
 def __init__(
     self,
     path: str,
     event_loop: asyncio.AbstractEventLoop,
     *,
     framework: Type[ASGIFramework] = EchoFramework,
 ) -> None:
     self.transport = MockTransport()
     self.client = h11.Connection(h11.CLIENT)
     self.server = WebsocketServer(  # type: ignore
         framework, event_loop, Config(), self.transport)
     self.server.data_received(
         self.client.send(
             h11.Request(
                 method="GET",
                 target=path,
                 headers=[
                     ("Host", "Hypercorn"),
                     ("Upgrade", "WebSocket"),
                     ("Connection", "Upgrade"),
                     ("Sec-WebSocket-Version", "13"),
                     ("Sec-WebSocket-Key", "121312"),
                 ],
             )))