@@ -84,6 +84,7 @@ describe('GraphQL-HTTP tests for connect', () => {
8484 runTests ( ( ) => {
8585 const app = connect ( ) ;
8686
87+ /* istanbul ignore next Error handler added only for debugging failed tests */
8788 app . on ( 'error' , ( error ) => {
8889 // eslint-disable-next-line no-console
8990 console . warn ( 'App encountered an error:' , error ) ;
@@ -107,6 +108,7 @@ describe('GraphQL-HTTP tests for express', () => {
107108 // This ensures consistent tests, as express defaults json spacing to 0 only in "production" mode.
108109 app . set ( 'json spaces' , 0 ) ;
109110
111+ /* istanbul ignore next Error handler added only for debugging failed tests */
110112 app . on ( 'error' , ( error ) => {
111113 // eslint-disable-next-line no-console
112114 console . warn ( 'App encountered an error:' , error ) ;
@@ -126,6 +128,7 @@ describe('GraphQL-HTTP tests for restify', () => {
126128 runTests ( ( ) => {
127129 const app = restify . createServer ( ) ;
128130
131+ /* istanbul ignore next Error handler added only for debugging failed tests */
129132 app . on ( 'error' , ( error ) => {
130133 // eslint-disable-next-line no-console
131134 console . warn ( 'App encountered an error:' , error ) ;
@@ -1438,6 +1441,28 @@ function runTests(server: Server) {
14381441 } ) ;
14391442 } ) ;
14401443
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+
14411466 it ( 'handles poorly formed variables' , async ( ) => {
14421467 const app = server ( ) ;
14431468
@@ -1525,6 +1550,30 @@ function runTests(server: Server) {
15251550 } ) ;
15261551 } ) ;
15271552
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+
15281577 it ( 'handles invalid variables' , async ( ) => {
15291578 const app = server ( ) ;
15301579
@@ -2191,5 +2240,30 @@ function runTests(server: Server) {
21912240 '{"data":{"test":"Hello World"},"extensions":{"eventually":42}}' ,
21922241 ) ;
21932242 } ) ;
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+ } ) ;
21942268 } ) ;
21952269}
0 commit comments