Beispiel #1
0
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",
                                       [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("/graphql", ["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", [GRAPHQL_WS_PROTOCOL]):
            pass

    assert exc.value.code == 4406
Beispiel #4
0
def test_client():
    @strawberry.type
    class Query:
        @strawberry.field
        async def hello(self, name: typing.Optional[str] = None) -> str:
            return f"Hello {name or 'world'}"

    async_schema = strawberry.Schema(Query)
    app = create_app(schema=async_schema)
    return TestClient(app)
Beispiel #5
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",
        [GRAPHQL_TRANSPORT_WS_PROTOCOL, GRAPHQL_WS_PROTOCOL]) as ws:
        assert ws.accepted_subprotocol == GRAPHQL_TRANSPORT_WS_PROTOCOL

    with test_client.websocket_connect(
            "/graphql",
        [GRAPHQL_WS_PROTOCOL, GRAPHQL_TRANSPORT_WS_PROTOCOL]) as ws:
        assert ws.accepted_subprotocol == GRAPHQL_WS_PROTOCOL
Beispiel #6
0
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", [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 #7
0
def test_custom_context():
    @strawberry.type
    class Query:
        @strawberry.field
        def custom_context_value(self, info: Info) -> str:
            return info.context["custom_value"]

    schema = strawberry.Schema(query=Query)
    app = create_app(schema=schema)

    test_client = TestClient(app)
    response = test_client.post("/graphql",
                                json={"query": "{ customContextValue }"})

    assert response.status_code == 200
    assert response.json() == {"data": {"customContextValue": "Hi!"}}
Beispiel #8
0
def test_can_set_background_task():
    task_complete = False

    def task():
        nonlocal task_complete
        task_complete = True

    @strawberry.type
    class Query:
        @strawberry.field
        def something(self, info: Info) -> str:
            tasks = info.context["background_tasks"]
            tasks.add_task(task)
            return "foo"

    schema = strawberry.Schema(query=Query)
    app = create_app(schema=schema)

    test_client = TestClient(app)
    response = test_client.post("/graphql", json={"query": "{ something }"})

    assert response.json() == {"data": {"something": "foo"}}
    assert task_complete
Beispiel #9
0
def test_client():
    app = create_app()
    return TestClient(app)
Beispiel #10
0
def test_client_no_graphiql():
    app = create_app(graphiql=False)
    return TestClient(app)
Beispiel #11
0
def test_client_keep_alive():
    app = create_app(keep_alive=True, keep_alive_interval=0.1)
    return TestClient(app)