forked from encode/starlette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_graphql.py
172 lines (124 loc) Β· 5.24 KB
/
test_graphql.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import graphene
from graphql.execution.executors.asyncio import AsyncioExecutor
from graphql.error import GraphQLError
from starlette.applications import Starlette
from starlette.datastructures import Headers
from starlette.graphql import GraphQLApp
from starlette.testclient import TestClient
class FakeAuthMiddleware:
def __init__(self, app) -> None:
self.app = app
async def __call__(self, scope, receive, send):
headers = Headers(scope=scope)
scope["user"] = "Jane" if headers.get("Authorization") == "Bearer 123" else None
await self.app(scope, receive, send)
class Query(graphene.ObjectType):
hello = graphene.String(name=graphene.String(default_value="stranger"))
whoami = graphene.String()
graphql_error = graphene.String()
def resolve_hello(self, info, name):
return "Hello " + name
def resolve_whoami(self, info):
return (
"a mystery"
if info.context["request"]["user"] is None
else info.context["request"]["user"]
)
def resolve_graphql_error(self, info):
raise GraphQLError("GraphQL Error")
schema = graphene.Schema(query=Query)
app = GraphQLApp(schema=schema, graphiql=True)
client = TestClient(app)
def test_graphql_get():
response = client.get("/?query={ hello }")
assert response.status_code == 200
assert response.json() == {"data": {"hello": "Hello stranger"}}
def test_graphql_post():
response = client.post("/?query={ hello }")
assert response.status_code == 200
assert response.json() == {"data": {"hello": "Hello stranger"}}
def test_graphql_post_json():
response = client.post("/", json={"query": "{ hello }"})
assert response.status_code == 200
assert response.json() == {"data": {"hello": "Hello stranger"}}
def test_graphql_post_graphql():
response = client.post(
"/", data="{ hello }", headers={"content-type": "application/graphql"}
)
assert response.status_code == 200
assert response.json() == {"data": {"hello": "Hello stranger"}}
def test_graphql_post_invalid_media_type():
response = client.post("/", data="{ hello }", headers={"content-type": "dummy"})
assert response.status_code == 415
assert response.text == "Unsupported Media Type"
def test_graphql_put():
response = client.put("/", json={"query": "{ hello }"})
assert response.status_code == 405
assert response.text == "Method Not Allowed"
def test_graphql_no_query():
response = client.get("/")
assert response.status_code == 400
assert response.text == "No GraphQL query found in the request"
def test_graphql_invalid_field():
response = client.post("/", json={"query": "{ dummy }"})
assert response.status_code == 400
assert response.json() == {
"data": None,
"errors": [
{
"locations": [{"column": 3, "line": 1}],
"message": 'Cannot query field "dummy" on type "Query".',
}
],
}
def test_graphiql_get():
response = client.get("/", headers={"accept": "text/html"})
assert response.status_code == 200
assert "<!DOCTYPE html>" in response.text
def test_graphql_error():
response = client.get("/?query={ graphqlError }")
assert response.status_code == 400
assert response.json()["data"] == {"graphqlError": None}
assert response.json()["errors"][0]["message"] == "GraphQL Error"
def test_graphiql_not_found():
app = GraphQLApp(schema=schema, graphiql=False)
client = TestClient(app)
response = client.get("/", headers={"accept": "text/html"})
assert response.status_code == 404
assert response.text == "Not Found"
def test_add_graphql_route():
app = Starlette()
app.add_route("/", GraphQLApp(schema=schema))
client = TestClient(app)
response = client.get("/?query={ hello }")
assert response.status_code == 200
assert response.json() == {"data": {"hello": "Hello stranger"}}
def test_graphql_context():
app = Starlette()
app.add_middleware(FakeAuthMiddleware)
app.add_route("/", GraphQLApp(schema=schema))
client = TestClient(app)
response = client.post(
"/", json={"query": "{ whoami }"}, headers={"Authorization": "Bearer 123"}
)
assert response.status_code == 200
assert response.json() == {"data": {"whoami": "Jane"}}
class ASyncQuery(graphene.ObjectType):
hello = graphene.String(name=graphene.String(default_value="stranger"))
async def resolve_hello(self, info, name):
return "Hello " + name
async_schema = graphene.Schema(query=ASyncQuery)
async_app = GraphQLApp(schema=async_schema, executor_class=AsyncioExecutor)
def test_graphql_async():
client = TestClient(async_app)
response = client.get("/?query={ hello }")
assert response.status_code == 200
assert response.json() == {"data": {"hello": "Hello stranger"}}
async_schema = graphene.Schema(query=ASyncQuery)
old_style_async_app = GraphQLApp(schema=async_schema, executor=AsyncioExecutor())
def test_graphql_async_old_style_executor():
# See https://github.com/encode/starlette/issues/242
client = TestClient(old_style_async_app)
response = client.get("/?query={ hello }")
assert response.status_code == 200
assert response.json() == {"data": {"hello": "Hello stranger"}}