This repository was archived by the owner on Mar 20, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +52
-2
lines changed Expand file tree Collapse file tree 3 files changed +52
-2
lines changed Original file line number Diff line number Diff line change 92
92
"multer" : " 1.2.0" ,
93
93
"raw-body" : " 2.1.6" ,
94
94
"sane" : " 1.4.1" ,
95
+ "sinon" : " 1.17.6" ,
95
96
"supertest" : " 1.0.1" ,
96
97
"supertest-as-promised" : " 2.0.2"
97
98
},
Original file line number Diff line number Diff line change 13
13
14
14
import { expect } from 'chai' ;
15
15
import { describe , it } from 'mocha' ;
16
+ import sinon from 'sinon' ;
16
17
import { stringify } from 'querystring' ;
17
18
import url from 'url' ;
18
19
import zlib from 'zlib' ;
@@ -832,6 +833,42 @@ describe('test harness', () => {
832
833
} ) ;
833
834
} ) ;
834
835
836
+ describe ( 'Response functionality' , ( ) => {
837
+ it ( 'uses send only for express' , async ( ) => {
838
+ const app = server ( ) ;
839
+ let spyEnd = { } ;
840
+ let spySend = { } ;
841
+
842
+ // mount a middleware to spy on response methods
843
+ app . use ( urlString ( ) , ( req , res , next ) => {
844
+ spyEnd = sinon . spy ( res , 'end' ) ;
845
+ try {
846
+ // res.send is undefined with connect
847
+ spySend = sinon . spy ( res , 'send' ) ;
848
+ } catch ( err ) {
849
+ spySend = undefined ;
850
+ }
851
+ next ( ) ;
852
+ } ) ;
853
+
854
+ app . use ( urlString ( ) , graphqlHTTP ( {
855
+ schema : TestSchema
856
+ } ) ) ;
857
+
858
+ await request ( app )
859
+ . get ( urlString ( {
860
+ query : '{test}'
861
+ } ) ) ;
862
+
863
+ if ( name === 'connect' ) {
864
+ expect ( spyEnd . calledOnce ) ;
865
+ expect ( spySend ) . to . equal ( undefined ) ;
866
+ } else {
867
+ expect ( spySend . calledOnce ) ;
868
+ }
869
+ } ) ;
870
+ } ) ;
871
+
835
872
describe ( 'Pretty printing' , ( ) => {
836
873
it ( 'supports pretty printing' , async ( ) => {
837
874
const app = server ( ) ;
Original file line number Diff line number Diff line change @@ -241,12 +241,12 @@ export default function graphqlHTTP(options: Options): Middleware {
241
241
operationName, result
242
242
} ) ;
243
243
response . setHeader ( 'Content-Type' , 'text/html; charset=utf-8' ) ;
244
- response . end ( data ) ;
244
+ sendResponse ( response , data ) ;
245
245
} else {
246
246
// Otherwise, present JSON directly.
247
247
const data = JSON . stringify ( result , null , pretty ? 2 : 0 ) ;
248
248
response . setHeader ( 'Content-Type' , 'application/json; charset=utf-8' ) ;
249
- response . end ( data ) ;
249
+ sendResponse ( response , data ) ;
250
250
}
251
251
} ) ;
252
252
} ;
@@ -306,3 +306,15 @@ function canDisplayGraphiQL(
306
306
// prefers HTML over JSON.
307
307
return ! raw && accepts ( request ) . types ( [ 'json' , 'html' ] ) === 'html' ;
308
308
}
309
+
310
+ /**
311
+ * Helper function for sending the response data. Use response.send it method
312
+ * exists (express), otherwise use response.end (connect).
313
+ */
314
+ function sendResponse ( response : Response , data : string ) : void {
315
+ if ( typeof response . send === 'function' ) {
316
+ response . send ( data ) ;
317
+ } else {
318
+ response . end ( data ) ;
319
+ }
320
+ }
You can’t perform that action at this time.
0 commit comments