@@ -84,6 +84,7 @@ describe('GraphQL-HTTP tests for connect', () => {
84
84
runTests ( ( ) => {
85
85
const app = connect ( ) ;
86
86
87
+ /* istanbul ignore next Error handler added only for debugging failed tests */
87
88
app . on ( 'error' , ( error ) => {
88
89
// eslint-disable-next-line no-console
89
90
console . warn ( 'App encountered an error:' , error ) ;
@@ -107,6 +108,7 @@ describe('GraphQL-HTTP tests for express', () => {
107
108
// This ensures consistent tests, as express defaults json spacing to 0 only in "production" mode.
108
109
app . set ( 'json spaces' , 0 ) ;
109
110
111
+ /* istanbul ignore next Error handler added only for debugging failed tests */
110
112
app . on ( 'error' , ( error ) => {
111
113
// eslint-disable-next-line no-console
112
114
console . warn ( 'App encountered an error:' , error ) ;
@@ -126,6 +128,7 @@ describe('GraphQL-HTTP tests for restify', () => {
126
128
runTests ( ( ) => {
127
129
const app = restify . createServer ( ) ;
128
130
131
+ /* istanbul ignore next Error handler added only for debugging failed tests */
129
132
app . on ( 'error' , ( error ) => {
130
133
// eslint-disable-next-line no-console
131
134
console . warn ( 'App encountered an error:' , error ) ;
@@ -1438,6 +1441,28 @@ function runTests(server: Server) {
1438
1441
} ) ;
1439
1442
} ) ;
1440
1443
1444
+ it ( 'handles invalid body' , async ( ) => {
1445
+ const app = server ( ) ;
1446
+
1447
+ app . post (
1448
+ urlString ( ) ,
1449
+ graphqlHTTP ( ( ) => ( {
1450
+ schema : TestSchema ,
1451
+ } ) ) ,
1452
+ ) ;
1453
+
1454
+ const response = await app
1455
+ . request ( )
1456
+ . post ( urlString ( ) )
1457
+ . set ( 'Content-Type' , 'application/json' )
1458
+ . send ( `{ "query": "{ ${ new Array ( 102400 ) . fill ( 'test' ) . join ( '' ) } }" }` ) ;
1459
+
1460
+ expect ( response . status ) . to . equal ( 400 ) ;
1461
+ expect ( JSON . parse ( response . text ) ) . to . deep . equal ( {
1462
+ errors : [ { message : 'Invalid body: request entity too large.' } ] ,
1463
+ } ) ;
1464
+ } ) ;
1465
+
1441
1466
it ( 'handles poorly formed variables' , async ( ) => {
1442
1467
const app = server ( ) ;
1443
1468
@@ -1525,6 +1550,30 @@ function runTests(server: Server) {
1525
1550
} ) ;
1526
1551
} ) ;
1527
1552
1553
+ it ( 'allows disabling prettifying poorly formed requests' , async ( ) => {
1554
+ const app = server ( ) ;
1555
+
1556
+ app . get (
1557
+ urlString ( ) ,
1558
+ graphqlHTTP ( {
1559
+ schema : TestSchema ,
1560
+ pretty : false ,
1561
+ } ) ,
1562
+ ) ;
1563
+
1564
+ const response = await app . request ( ) . get (
1565
+ urlString ( {
1566
+ variables : 'who:You' ,
1567
+ query : 'query helloWho($who: String){ test(who: $who) }' ,
1568
+ } ) ,
1569
+ ) ;
1570
+
1571
+ expect ( response . status ) . to . equal ( 400 ) ;
1572
+ expect ( response . text ) . to . equal (
1573
+ '{"errors":[{"message":"Variables are invalid JSON."}]}' ,
1574
+ ) ;
1575
+ } ) ;
1576
+
1528
1577
it ( 'handles invalid variables' , async ( ) => {
1529
1578
const app = server ( ) ;
1530
1579
@@ -2191,5 +2240,30 @@ function runTests(server: Server) {
2191
2240
'{"data":{"test":"Hello World"},"extensions":{"eventually":42}}' ,
2192
2241
) ;
2193
2242
} ) ;
2243
+
2244
+ it ( 'does nothing if extensions function does not return an object' , async ( ) => {
2245
+ const app = server ( ) ;
2246
+
2247
+ app . get (
2248
+ urlString ( ) ,
2249
+ // @ts -expect-error
2250
+ graphqlHTTP ( ( ) => ( {
2251
+ schema : TestSchema ,
2252
+ context : { foo : 'bar' } ,
2253
+ extensions ( { context } ) {
2254
+ return ( ) => ( { contextValue : JSON . stringify ( context ) } ) ;
2255
+ } ,
2256
+ } ) ) ,
2257
+ ) ;
2258
+
2259
+ const response = await app
2260
+ . request ( )
2261
+ . get ( urlString ( { query : '{test}' , raw : '' } ) )
2262
+ . set ( 'Accept' , 'text/html' ) ;
2263
+
2264
+ expect ( response . status ) . to . equal ( 200 ) ;
2265
+ expect ( response . type ) . to . equal ( 'application/json' ) ;
2266
+ expect ( response . text ) . to . equal ( '{"data":{"test":"Hello World"}}' ) ;
2267
+ } ) ;
2194
2268
} ) ;
2195
2269
}
0 commit comments