6
6
7
7
import type { ExecutionResult } from 'graphql' ;
8
8
import { Audit , AuditResult } from './common' ;
9
- import { assert , audit , extendedTypeof } from './utils' ;
9
+ import {
10
+ assert ,
11
+ assertBodyAsExecutionResult ,
12
+ audit ,
13
+ extendedTypeof ,
14
+ } from './utils' ;
10
15
11
16
/**
12
17
* Options for server audits required to check GraphQL over HTTP spec conformance.
@@ -158,8 +163,10 @@ export function serverAudits(opts: ServerAuditOptions): Audit[] {
158
163
if ( contentType . includes ( 'application/json' ) ) {
159
164
assert ( `Content-Type ${ contentType } status code` , res . status ) . toBe ( 200 ) ;
160
165
161
- const body = await res . json ( ) ;
162
- assert ( 'Body data errors' , body . errors ) . toBeDefined ( ) ;
166
+ assert (
167
+ 'Body data errors' ,
168
+ ( await assertBodyAsExecutionResult ( res ) ) . errors ,
169
+ ) . toBeDefined ( ) ;
163
170
return ;
164
171
}
165
172
@@ -345,7 +352,9 @@ export function serverAudits(opts: ServerAuditOptions): Audit[] {
345
352
} ) ,
346
353
} ) ;
347
354
assert ( 'Status code' , res . status ) . toBe ( 200 ) ;
348
- const result = ( await res . json ( ) ) as ExecutionResult ;
355
+ const result = ( await assertBodyAsExecutionResult (
356
+ res ,
357
+ ) ) as ExecutionResult ;
349
358
assert ( 'Execution result' , result ) . notToHaveProperty ( 'errors' ) ;
350
359
} ) ,
351
360
audit (
@@ -361,7 +370,7 @@ export function serverAudits(opts: ServerAuditOptions): Audit[] {
361
370
method : 'GET' ,
362
371
} ) ;
363
372
assert ( 'Status code' , res . status ) . toBe ( 200 ) ;
364
- const result = ( await res . json ( ) ) as ExecutionResult ;
373
+ const result = await assertBodyAsExecutionResult ( res ) ;
365
374
assert ( 'Execution result' , result ) . notToHaveProperty ( 'errors' ) ;
366
375
} ,
367
376
) ,
@@ -493,7 +502,10 @@ export function serverAudits(opts: ServerAuditOptions): Audit[] {
493
502
} ,
494
503
body : '{ "not a JSON' ,
495
504
} ) ;
496
- assert ( 'Data entry' , ( await res . json ( ) ) . data ) . toBe ( undefined ) ;
505
+ assert (
506
+ 'Data entry' ,
507
+ ( await assertBodyAsExecutionResult ( res ) ) . data ,
508
+ ) . toBe ( undefined ) ;
497
509
} ,
498
510
) ,
499
511
audit (
@@ -530,7 +542,10 @@ export function serverAudits(opts: ServerAuditOptions): Audit[] {
530
542
method : 'GET' ,
531
543
headers : { accept : 'application/graphql-response+json' } ,
532
544
} ) ;
533
- assert ( 'Data entry' , ( await res . json ( ) ) . data ) . toBe ( undefined ) ;
545
+ assert (
546
+ 'Data entry' ,
547
+ ( await assertBodyAsExecutionResult ( res ) ) . data ,
548
+ ) . toBe ( undefined ) ;
534
549
} ,
535
550
) ,
536
551
audit (
@@ -567,7 +582,10 @@ export function serverAudits(opts: ServerAuditOptions): Audit[] {
567
582
method : 'GET' ,
568
583
headers : { accept : 'application/graphql-response+json' } ,
569
584
} ) ;
570
- assert ( 'Data entry' , ( await res . json ( ) ) . data ) . toBe ( undefined ) ;
585
+ assert (
586
+ 'Data entry' ,
587
+ ( await assertBodyAsExecutionResult ( res ) ) . data ,
588
+ ) . toBe ( undefined ) ;
571
589
} ,
572
590
) ,
573
591
audit (
@@ -604,7 +622,10 @@ export function serverAudits(opts: ServerAuditOptions): Audit[] {
604
622
method : 'GET' ,
605
623
headers : { accept : 'application/graphql-response+json' } ,
606
624
} ) ;
607
- assert ( 'Data entry' , ( await res . json ( ) ) . data ) . toBe ( undefined ) ;
625
+ assert (
626
+ 'Data entry' ,
627
+ ( await assertBodyAsExecutionResult ( res ) ) . data ,
628
+ ) . toBe ( undefined ) ;
608
629
} ,
609
630
) ,
610
631
// TODO: how to fail and have the data entry?
0 commit comments