def test_http_auto(): config = Config(app=None) server_state = ServerState() protocol = AutoHTTPProtocol(config=config, server_state=server_state) if httptools is None: assert type(protocol).__name__ == "H11Protocol" else: assert type(protocol).__name__ == "HttpToolsProtocol"
def test_websocket_auto(): config = Config(app=None) server_state = ServerState() protocol = AutoWebSocketsProtocol(config=config, server_state=server_state) if websockets is None: assert type(protocol).__name__ == "WSProtocol" else: assert type(protocol).__name__ == "WebSocketProtocol"
def get_connected_protocol(app, protocol_cls, **kwargs): loop = MockLoop() transport = MockTransport() config = Config(app=app, **kwargs) server_state = ServerState() protocol = protocol_cls(config=config, server_state=server_state, _loop=loop) protocol.connection_made(transport) return protocol
def get_connected_protocol(app, protocol_cls, event_loop, **kwargs): loop = MockLoop(event_loop) transport = MockTransport() config = Config(app=app, **kwargs) server_state = ServerState() protocol = protocol_cls(config=config, server_state=server_state, _loop=loop) protocol.connection_made(transport) yield protocol protocol.loop.close()
def get_connected_protocol(app, protocol_cls, event_loop, **kwargs): loop = MockLoop(event_loop) asyncio._set_running_loop(loop) transport = MockTransport() config = Config(app=app, **kwargs) server_state = ServerState() protocol = protocol_cls(config=config, server_state=server_state, _loop=loop) protocol.connection_made(transport) try: yield protocol finally: protocol.loop.close() asyncio._set_running_loop(None)
def run_server(app, protocol_cls, path="/"): asyncio.set_event_loop(None) loop = asyncio.new_event_loop() config = Config(app=app, ws=protocol_cls) server_state = ServerState() protocol = functools.partial(H11Protocol, config=config, server_state=server_state) create_server_task = loop.create_server(protocol, host="127.0.0.1") server = loop.run_until_complete(create_server_task) port = server.sockets[0].getsockname()[1] url = "ws://127.0.0.1:{port}{path}".format(port=port, path=path) try: # Run the event loop in a new thread. thread = threading.Thread(target=run_loop, args=[loop]) thread.start() # Return the contextmanager state. yield url finally: # Close the loop from our main thread. while server_state.tasks: time.sleep(0.01) loop.call_soon_threadsafe(loop.stop) thread.join()
async def test_websocket_auto(): config = Config(app=app) server_state = ServerState() protocol = AutoWebSocketsProtocol(config=config, server_state=server_state) expected_websockets = "WSProtocol" if websockets is None else "WebSocketProtocol" assert type(protocol).__name__ == expected_websockets
async def test_http_auto(): config = Config(app=app) server_state = ServerState() protocol = AutoHTTPProtocol(config=config, server_state=server_state) expected_http = "H11Protocol" if httptools is None else "HttpToolsProtocol" assert type(protocol).__name__ == expected_http
def test_http_auto(): config = Config(app=None) server_state = ServerState() protocol = AutoHTTPProtocol(config=config, server_state=server_state) assert isinstance(protocol, HttpToolsProtocol)