Skip to content

Commit de045d4

Browse files
authored
Merge pull request #852 from alxndrsn/econnreset
fix: avoid server re-use race conditions
2 parents be0d5c7 + 96c06e3 commit de045d4

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* Module dependencies.
55
*/
66
const methods = require('methods');
7-
const http = require('http');
87
let http2;
98
try {
109
http2 = require('http2'); // eslint-disable-line global-require
@@ -32,15 +31,12 @@ module.exports = function(app, options = {}) {
3231
'supertest: this version of Node.js does not support http2'
3332
);
3433
}
35-
app = http2.createServer(app); // eslint-disable-line no-param-reassign
36-
} else {
37-
app = http.createServer(app); // eslint-disable-line no-param-reassign
3834
}
3935
}
4036

4137
methods.forEach(function(method) {
4238
obj[method] = function(url) {
43-
var test = new Test(app, method, url);
39+
var test = new Test(app, method, url, options.http2);
4440
if (options.http2) {
4541
test.http2();
4642
}

lib/test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@
55
*/
66

77
const { inspect } = require('util');
8+
const http = require('http');
89
const { STATUS_CODES } = require('http');
910
const { Server } = require('tls');
1011
const { deepStrictEqual } = require('assert');
1112
const { Request } = require('superagent');
13+
let http2;
14+
try {
15+
http2 = require('http2'); // eslint-disable-line global-require
16+
} catch (_) {
17+
// eslint-disable-line no-empty
18+
}
1219

1320
/** @typedef {import('superagent').Response} Response */
1421

@@ -22,9 +29,17 @@ class Test extends Request {
2229
* @param {String} path
2330
* @api public
2431
*/
25-
constructor (app, method, path) {
32+
constructor (app, method, path, optHttp2) {
2633
super(method.toUpperCase(), path);
2734

35+
if (typeof app === 'function') {
36+
if (optHttp2) {
37+
app = http2.createServer(app); // eslint-disable-line no-param-reassign
38+
} else {
39+
app = http.createServer(app); // eslint-disable-line no-param-reassign
40+
}
41+
}
42+
2843
this.redirects(0);
2944
this.buffer();
3045
this.app = app;

test/supertest.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,23 @@ describe('request(app)', function () {
7979
});
8080
});
8181

82+
it('should not ECONNRESET on multiple simultaneous tests', function (done) {
83+
const app = express();
84+
85+
app.get('/', function (req, res) {
86+
res.send('hey');
87+
});
88+
89+
const test = request(app);
90+
91+
const requestCount = 10;
92+
93+
const requests = [];
94+
for (let i = 0; i < requestCount; i += 1) requests.push(test.get('/'));
95+
96+
global.Promise.all(requests).then(() => done(), done);
97+
});
98+
8299
it('should work with an active server', function (done) {
83100
const app = express();
84101
let server;

0 commit comments

Comments
 (0)