Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit 4c7ffcf

Browse files
committed
Merge branch 'brandondoran-express-send'
2 parents ffab0c0 + 26c82ea commit 4c7ffcf

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
"multer": "1.2.0",
9393
"raw-body": "2.1.6",
9494
"sane": "1.4.1",
95+
"sinon": "1.17.6",
9596
"supertest": "1.0.1",
9697
"supertest-as-promised": "2.0.2"
9798
},

src/__tests__/http-test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import { expect } from 'chai';
1515
import { describe, it } from 'mocha';
16+
import sinon from 'sinon';
1617
import { stringify } from 'querystring';
1718
import url from 'url';
1819
import zlib from 'zlib';
@@ -832,6 +833,42 @@ describe('test harness', () => {
832833
});
833834
});
834835

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+
835872
describe('Pretty printing', () => {
836873
it('supports pretty printing', async () => {
837874
const app = server();

src/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,12 @@ export default function graphqlHTTP(options: Options): Middleware {
241241
operationName, result
242242
});
243243
response.setHeader('Content-Type', 'text/html; charset=utf-8');
244-
response.end(data);
244+
sendResponse(response, data);
245245
} else {
246246
// Otherwise, present JSON directly.
247247
const data = JSON.stringify(result, null, pretty ? 2 : 0);
248248
response.setHeader('Content-Type', 'application/json; charset=utf-8');
249-
response.end(data);
249+
sendResponse(response, data);
250250
}
251251
});
252252
};
@@ -306,3 +306,15 @@ function canDisplayGraphiQL(
306306
// prefers HTML over JSON.
307307
return !raw && accepts(request).types([ 'json', 'html' ]) === 'html';
308308
}
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+
}

0 commit comments

Comments
 (0)