async def test_connection_init_timeout_cancellation(test_client):
    app = create_app(connection_init_wait_timeout=timedelta(milliseconds=500))
    test_client = TestClient(app)

    with test_client.websocket_connect("/", [GRAPHQL_TRANSPORT_WS_PROTOCOL]) as ws:
        ws.send_json(ConnectionInitMessage().as_dict())

        response = ws.receive_json()
        assert response == ConnectionAckMessage().as_dict()

        await asyncio.sleep(1)

        ws.send_json(
            SubscribeMessage(
                id="sub1",
                payload=SubscribeMessagePayload(
                    query="subscription { debug { isConnectionInitTimeoutTaskDone } }"
                ),
            ).as_dict()
        )

        response = ws.receive_json()
        assert (
            response
            == NextMessage(
                id="sub1",
                payload={"data": {"debug": {"isConnectionInitTimeoutTaskDone": True}}},
            ).as_dict()
        )

        ws.close()
Beispiel #2
0
def test_unsupported_ws_protocol():
    app = create_app(subscription_protocols=[])
    test_client = TestClient(app)

    with pytest.raises(WebSocketDisconnect) as exc:
        with test_client.websocket_connect("/", ["imaginary-protocol"]):
            pass

    assert exc.value.code == 4406
Beispiel #3
0
def test_turning_off_graphql_ws():
    app = create_app(subscription_protocols=[GRAPHQL_TRANSPORT_WS_PROTOCOL])
    test_client = TestClient(app)

    with pytest.raises(WebSocketDisconnect) as exc:
        with test_client.websocket_connect("/", [GRAPHQL_WS_PROTOCOL]):
            pass

    assert exc.value.code == 4406
Beispiel #4
0
def test_clients_can_prefer_protocols():
    app = create_app(subscription_protocols=[
        GRAPHQL_WS_PROTOCOL, GRAPHQL_TRANSPORT_WS_PROTOCOL
    ])
    test_client = TestClient(app)

    with test_client.websocket_connect(
            "/", [GRAPHQL_TRANSPORT_WS_PROTOCOL, GRAPHQL_WS_PROTOCOL]) as ws:
        assert ws.accepted_subprotocol == GRAPHQL_TRANSPORT_WS_PROTOCOL

    with test_client.websocket_connect(
            "/", [GRAPHQL_WS_PROTOCOL, GRAPHQL_TRANSPORT_WS_PROTOCOL]) as ws:
        assert ws.accepted_subprotocol == GRAPHQL_WS_PROTOCOL
async def test_connection_init_timeout():
    app = create_app(connection_init_wait_timeout=timedelta(seconds=0))
    test_client = TestClient(app)

    # Hope that the connection init timeout expired
    await asyncio.sleep(0.1)

    try:
        with test_client.websocket_connect("/", [GRAPHQL_TRANSPORT_WS_PROTOCOL]) as ws:
            data = ws.receive()
            assert data["type"] == "websocket.close"
            assert data["code"] == 4408
    except WebSocketDisconnect as exc:
        assert exc.code == 4408
Beispiel #6
0
def test_client():
    app = create_app()
    return TestClient(app)
Beispiel #7
0
def test_client_no_graphiql():
    app = create_app(graphiql=False)
    return TestClient(app)
Beispiel #8
0
def test_client_keep_alive():
    app = create_app(keep_alive=True, keep_alive_interval=0.1)
    return TestClient(app)