File: test_graphql_ide.py

package info (click to toggle)
strawberry-graphql 0.306.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 18,176 kB
  • sloc: javascript: 178,052; python: 65,643; sh: 33; makefile: 25
file content (57 lines) | stat: -rw-r--r-- 1,804 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from typing import Literal

import pytest

from tests.views.schema import schema

from .clients.base import HttpClient


@pytest.mark.parametrize("header_value", ["text/html", "*/*"])
@pytest.mark.parametrize("graphql_ide", ["graphiql", "apollo-sandbox", "pathfinder"])
async def test_renders_graphql_ide(
    header_value: str,
    http_client_class: type[HttpClient],
    graphql_ide: Literal["graphiql", "apollo-sandbox", "pathfinder"],
):
    http_client = http_client_class(schema, graphql_ide=graphql_ide)

    response = await http_client.get("/graphql", headers={"Accept": header_value})
    content_type = response.headers.get(
        "content-type", response.headers.get("Content-Type", "")
    )

    assert response.status_code == 200
    assert "text/html" in content_type
    assert "<title>Strawberry" in response.text

    if graphql_ide == "apollo-sandbox":
        assert "embeddable-sandbox.cdn.apollographql" in response.text

    if graphql_ide == "pathfinder":
        assert "@pathfinder-ide/react" in response.text

    if graphql_ide == "graphiql":
        assert "unpkg.com/graphiql" in response.text


async def test_does_not_render_graphiql_if_wrong_accept(
    http_client_class: type[HttpClient],
):
    http_client = http_client_class(schema)
    response = await http_client.get("/graphql", headers={"Accept": "text/xml"})

    # THIS might need to be changed to 404

    assert response.status_code == 400


@pytest.mark.parametrize("graphql_ide", [False, None])
async def test_renders_graphiql_disabled(
    http_client_class: type[HttpClient],
    graphql_ide: bool | None,
):
    http_client = http_client_class(schema, graphql_ide=graphql_ide)
    response = await http_client.get("/graphql", headers={"Accept": "text/html"})

    assert response.status_code == 404