-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathtest_graphql_over_http_spec.py
More file actions
648 lines (553 loc) · 19.8 KB
/
test_graphql_over_http_spec.py
File metadata and controls
648 lines (553 loc) · 19.8 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
"""This file essentially mirrors the GraphQL over HTTP audits:
https://github.com/graphql/graphql-http/blob/main/src/audits/server.ts
"""
import pytest
try:
from tests.http.clients.chalice import ChaliceHttpClient
except ImportError:
ChaliceHttpClient = type(None)
try:
from tests.http.clients.django import DjangoHttpClient
except ImportError:
DjangoHttpClient = type(None)
try:
from tests.http.clients.sanic import SanicHttpClient
except ImportError:
SanicHttpClient = type(None)
async def test_22eb(http_client):
"""SHOULD accept application/graphql-response+json and match the content-type"""
response = await http_client.query(
method="post",
headers={
"Content-Type": "application/json",
"Accept": "application/graphql-response+json",
},
query="{ __typename }",
)
assert response.status_code == 200
assert "application/graphql-response+json" in response.headers["content-type"]
async def test_4655(http_client):
"""MUST accept application/json and match the content-type"""
response = await http_client.query(
method="post",
headers={
"Content-Type": "application/json",
"Accept": "application/json",
},
query="{ __typename }",
)
assert response.status_code == 200
assert "application/json" in response.headers["content-type"]
async def test_47de(http_client):
"""SHOULD accept */* and use application/json for the content-type"""
response = await http_client.query(
method="post",
headers={
"Content-Type": "application/json",
"Accept": "*/*",
},
query="{ __typename }",
)
assert response.status_code == 200
assert "application/json" in response.headers["content-type"]
async def test_80d8(http_client):
"""SHOULD assume application/json content-type when accept is missing"""
response = await http_client.query(
method="post",
headers={"Content-Type": "application/json"},
query="{ __typename }",
)
assert response.status_code == 200
assert "application/json" in response.headers["content-type"]
async def test_82a3(http_client):
"""MUST use utf-8 encoding when responding"""
response = await http_client.query(
method="post",
headers={"Content-Type": "application/json"},
query="{ __typename }",
)
assert response.status_code == 200
assert isinstance(response.data, bytes)
try:
response.data.decode(encoding="utf-8", errors="strict")
except UnicodeDecodeError:
pytest.fail("Response body is not UTF-8 encoded")
async def test_bf61(http_client):
"""MUST accept utf-8 encoded request"""
response = await http_client.query(
method="post",
headers={"Content-Type": "application/json; charset=utf-8"},
query='{ __type(name: "Run🏃Swim🏊") { name } }',
)
assert response.status_code == 200
async def test_78d5(http_client):
"""MUST assume utf-8 in request if encoding is unspecified"""
response = await http_client.query(
method="post",
headers={"Content-Type": "application/json"},
query="{ __typename }",
)
assert response.status_code == 200
async def test_2c94(http_client):
"""MUST accept POST requests"""
response = await http_client.query(
method="post",
headers={"Content-Type": "application/json"},
query="{ __typename }",
)
assert response.status_code == 200
async def test_5a70(http_client):
"""MAY accept application/x-www-form-urlencoded formatted GET requests"""
response = await http_client.query(method="get", query="{ __typename }")
assert response.status_code == 200
async def test_9c48(http_client):
"""MAY NOT allow executing mutations on GET requests"""
response = await http_client.query(
method="get",
headers={"Accept": "application/graphql-response+json"},
query="mutation { __typename }",
)
assert 400 <= response.status_code <= 499
async def test_9abe(http_client):
"""MAY respond with 4xx status code if content-type is not supplied on POST requests"""
response = await http_client.post(
url="/graphql",
headers={},
data=b'{"query": "{ __typename }"}',
)
assert 400 <= response.status_code <= 499
async def test_03d4(http_client):
"""MUST accept application/json POST requests"""
response = await http_client.query(
method="post",
headers={"Content-Type": "application/json"},
query="{ __typename }",
)
assert response.status_code == 200
async def test_a5bf(http_client):
"""MAY use 400 status code when request body is missing on POST"""
response = await http_client.post(
url="/graphql",
headers={"Content-Type": "application/json"},
)
assert response.status_code == 400
async def test_423l(http_client):
"""MAY use 400 status code on missing {query} parameter"""
response = await http_client.post(
url="/graphql",
headers={
"Content-Type": "application/json",
"Accept": "application/graphql-response+json",
},
json={"notquery": "{ __typename }"},
)
assert response.status_code == 400
@pytest.mark.parametrize(
"invalid",
[{"obj": "etc"}, 0, False, ["array"]],
ids=["LKJ0", "LKJ1", "LKJ2", "LKJ3"],
)
async def test_lkj_(http_client, invalid):
"""MAY use 400 status code on invalid {query} parameter"""
response = await http_client.post(
url="/graphql",
headers={"Content-Type": "application/json"},
json={"query": invalid},
)
assert response.status_code == 400
async def test_34a2(http_client):
"""SHOULD allow string {query} parameter when accepting application/graphql-response+json"""
response = await http_client.query(
method="post",
headers={
"Content-Type": "application/json",
"Accept": "application/graphql-response+json",
},
query="{ __typename }",
)
assert response.status_code == 200
async def test_13ee(http_client):
"""MUST allow string {query} parameter when accepting application/json"""
response = await http_client.query(
method="post",
headers={
"Content-Type": "application/json",
"Accept": "application/json",
},
query="{ __typename }",
)
assert response.status_code == 200
assert isinstance(response.json, dict)
assert "errors" not in response.json
@pytest.mark.parametrize(
"invalid",
[{"obj": "etc"}, 0, False, ["array"]],
ids=["6C00", "6C01", "6C02", "6C03"],
)
async def test_6c0_(http_client, invalid):
"""MAY use 400 status code on invalid {operationName} parameter"""
response = await http_client.post(
url="/graphql",
headers={"Content-Type": "application/json"},
json={
"operationName": invalid,
"query": "{ __typename }",
},
)
assert response.status_code == 400
async def test_8161(http_client):
"""SHOULD allow string {operationName} parameter when accepting application/graphql-response+json"""
response = await http_client.post(
url="/graphql",
headers={
"Content-Type": "application/json",
"Accept": "application/graphql-response+json",
},
json={
"operationName": "Query",
"query": "query Query { __typename }",
},
)
assert response.status_code == 200
async def test_b8b3(http_client):
"""MUST allow string {operationName} parameter when accepting application/json"""
response = await http_client.post(
url="/graphql",
headers={
"Content-Type": "application/json",
"Accept": "application/json",
},
json={
"operationName": "Query",
"query": "query Query { __typename }",
},
)
assert response.status_code == 200
assert isinstance(response.json, dict)
assert "errors" not in response.json
@pytest.mark.parametrize(
"parameter",
["variables", "operationName", "extensions"],
ids=["94B0", "94B1", "94B2"],
)
async def test_94b_(http_client, parameter):
"""SHOULD allow null variables/operationName/extensions parameter when accepting application/graphql-response+json"""
response = await http_client.post(
url="/graphql",
headers={
"Content-Type": "application/json",
"Accept": "application/graphql-response+json",
},
json={
"query": "{ __typename }",
parameter: None,
},
)
assert response.status_code == 200
assert "errors" not in response.json
@pytest.mark.parametrize(
"parameter",
["variables", "operationName", "extensions"],
ids=["0220", "0221", "0222"],
)
async def test_022_(http_client, parameter):
"""MUST allow null variables/operationName/extensions parameter when accepting application/json"""
response = await http_client.post(
url="/graphql",
headers={
"Content-Type": "application/json",
"Accept": "application/json",
},
json={
"query": "{ __typename }",
parameter: None,
},
)
assert response.status_code == 200
assert "errors" not in response.json
@pytest.mark.parametrize(
"invalid",
["string", 0, False, ["array"]],
ids=["4760", "4761", "4762", "4763"],
)
async def test_476_(http_client, invalid):
"""MAY use 400 status code on invalid {variables} parameter"""
response = await http_client.post(
url="/graphql",
headers={"Content-Type": "application/json"},
json={
"query": "{ __typename }",
"variables": invalid,
},
)
assert response.status_code == 400
async def test_2ea1(http_client):
"""SHOULD allow map {variables} parameter when accepting application/graphql-response+json"""
response = await http_client.post(
url="/graphql",
headers={
"Content-Type": "application/json",
"Accept": "application/graphql-response+json",
},
json={
"query": "query Type($name: String!) { __type(name: $name) { name } }",
"variables": {"name": "sometype"},
},
)
assert response.status_code == 200
async def test_28b9(http_client):
"""MUST allow map {variables} parameter when accepting application/json"""
response = await http_client.post(
url="/graphql",
headers={
"Content-Type": "application/json",
"Accept": "application/json",
},
json={
"query": "query Type($name: String!) { __type(name: $name) { name } }",
"variables": {"name": "sometype"},
},
)
assert response.status_code == 200
assert isinstance(response.json, dict)
assert "errors" not in response.json
async def test_d6d5(http_client):
"""MAY allow URL-encoded JSON string {variables} parameter in GETs when accepting application/graphql-response+json"""
response = await http_client.query(
query="query Type($name: String!) { __type(name: $name) { name } }",
variables={"name": "sometype"},
method="get",
headers={"Accept": "application/graphql-response+json"},
)
assert response.status_code == 200
async def test_6a70(http_client):
"""MAY allow URL-encoded JSON string {variables} parameter in GETs when accepting application/json"""
response = await http_client.query(
query="query Type($name: String!) { __type(name: $name) { name } }",
variables={"name": "sometype"},
method="get",
headers={"Accept": "application/json"},
)
assert response.status_code == 200
assert isinstance(response.json, dict)
assert "errors" not in response.json
@pytest.mark.parametrize(
"invalid",
["string", 0, False, ["array"]],
ids=["58B0", "58B1", "58B2", "58B3"],
)
async def test_58b_(http_client, invalid):
"""MAY use 400 status code on invalid {extensions} parameter"""
response = await http_client.post(
url="/graphql",
headers={"Content-Type": "application/json"},
json={
"query": "{ __typename }",
"extensions": invalid,
},
)
assert response.status_code == 400
async def test_428f(http_client):
"""SHOULD allow map {extensions} parameter when accepting application/graphql-response+json"""
response = await http_client.post(
url="/graphql",
headers={
"Content-Type": "application/json",
"Accept": "application/graphql-response+json",
},
json={
"query": "{ __typename }",
"extensions": {"some": "value"},
},
)
assert response.status_code == 200
async def test_1b7a(http_client):
"""MUST allow map {extensions} parameter when accepting application/json"""
response = await http_client.post(
url="/graphql",
headers={
"Content-Type": "application/json",
"Accept": "application/json",
},
json={
"query": "{ __typename }",
"extensions": {"some": "value"},
},
)
assert response.status_code == 200
assert isinstance(response.json, dict)
assert "errors" not in response.json
async def test_b6dc(http_client):
"""MAY use 4xx or 5xx status codes on JSON parsing failure"""
response = await http_client.post(
url="/graphql",
headers={"Content-Type": "application/json"},
data=b'{ "not a JSON',
)
assert 400 <= response.status_code <= 599
async def test_bcf8(http_client):
"""MAY use 400 status code on JSON parsing failure"""
response = await http_client.post(
url="/graphql",
headers={"Content-Type": "application/json"},
data=b'{ "not a JSON',
)
assert response.status_code == 400
async def test_8764(http_client):
"""MAY use 4xx or 5xx status codes if parameters are invalid"""
response = await http_client.post(
url="/graphql",
headers={"Content-Type": "application/json"},
json={"qeury": "{ __typename }"}, # typo in 'query'
)
assert 400 <= response.status_code <= 599
async def test_3e3a(http_client):
"""MAY use 400 status code if parameters are invalid"""
response = await http_client.post(
url="/graphql",
headers={"Content-Type": "application/json"},
json={"qeury": "{ __typename }"}, # typo in 'query'
)
assert response.status_code == 400
async def test_39aa(http_client):
"""MUST accept a map for the {extensions} parameter"""
response = await http_client.post(
url="/graphql",
headers={
"Content-Type": "application/json",
"Accept": "application/json",
},
json={
"query": "{ __typename }",
"extensions": {"some": "value"},
},
)
assert response.status_code == 200
assert isinstance(response.json, dict)
assert "errors" not in response.json
async def test_572b(http_client):
"""SHOULD use 200 status code on document parsing failure when accepting application/json"""
response = await http_client.post(
url="/graphql",
headers={
"Content-Type": "application/json",
"Accept": "application/json",
},
json={"query": "{"},
)
assert response.status_code == 200
async def test_dfe2(http_client):
"""SHOULD use 200 status code on document validation failure when accepting application/json"""
response = await http_client.post(
url="/graphql",
headers={
"Content-Type": "application/json",
"Accept": "application/json",
},
json={
"query": "{ 8f31403dfe404bccbb0e835f2629c6a7 }"
}, # making sure the field doesn't exist
)
assert response.status_code == 200
async def test_7b9b(http_client):
"""SHOULD use a status code of 200 on variable coercion failure when accepting application/json"""
response = await http_client.post(
url="/graphql",
headers={
"Content-Type": "application/json",
"Accept": "application/json",
},
json={
"query": "query CoerceFailure($id: ID!){ __typename }",
"variables": {"id": None},
},
)
assert response.status_code == 200
async def test_865d(http_client):
"""SHOULD use 4xx or 5xx status codes on document parsing failure when accepting application/graphql-response+json"""
response = await http_client.post(
url="/graphql",
headers={
"Content-Type": "application/json",
"Accept": "application/graphql-response+json",
},
json={"query": "{"},
)
assert 400 <= response.status_code <= 599
async def test_556a(http_client):
"""SHOULD use 400 status code on document parsing failure when accepting application/graphql-response+json"""
response = await http_client.post(
url="/graphql",
headers={
"Content-Type": "application/json",
"Accept": "application/graphql-response+json",
},
json={"query": "{"},
)
assert response.status_code == 400
async def test_d586(http_client):
"""SHOULD NOT contain the data entry on document parsing failure when accepting application/graphql-response+json"""
response = await http_client.post(
url="/graphql",
headers={
"Content-Type": "application/json",
"Accept": "application/graphql-response+json",
},
json={"query": "{"},
)
assert response.status_code == 400
assert "data" not in response.json
async def test_51fe(http_client):
"""SHOULD use 4xx or 5xx status codes on document validation failure when accepting application/graphql-response+json"""
response = await http_client.post(
url="/graphql",
headers={
"Content-Type": "application/json",
"Accept": "application/graphql-response+json",
},
json={
"query": "{ 8f31403dfe404bccbb0e835f2629c6a7 }", # making sure the field doesn't exist
},
)
assert 400 <= response.status_code <= 599
async def test_74ff(http_client):
"""SHOULD use 400 status code on document validation failure when accepting application/graphql-response+json"""
response = await http_client.post(
url="/graphql",
headers={
"Content-Type": "application/json",
"Accept": "application/graphql-response+json",
},
json={
"query": "{ 8f31403dfe404bccbb0e835f2629c6a7 }", # making sure the field doesn't exist
},
)
assert response.status_code == 400
async def test_5e5b(http_client):
"""SHOULD NOT contain the data entry on document validation failure when accepting application/graphql-response+json"""
response = await http_client.post(
url="/graphql",
headers={
"Content-Type": "application/json",
"Accept": "application/graphql-response+json",
},
json={
"query": "{ 8f31403dfe404bccbb0e835f2629c6a7 }", # making sure the field doesn't exist
},
)
assert response.status_code == 400
assert "data" not in response.json
async def test_86ee(http_client):
"""SHOULD use a status code of 400 on variable coercion failure when accepting application/graphql-response+json"""
response = await http_client.post(
url="/graphql",
headers={
"Content-Type": "application/json",
"Accept": "application/graphql-response+json",
},
json={
"query": "query CoerceFailure($id: ID!){ __typename }",
"variables": {"id": None},
},
)
assert response.status_code == 400