forked from request/request
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Auto Requests crashing after client network disconnect (#98)
- Loading branch information
1 parent
6a0f945
commit ca52d9b
Showing
10 changed files
with
524 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use strict' | ||
|
||
var server = require('./server') | ||
var request = require('../index') | ||
var tape = require('tape') | ||
|
||
var s | ||
|
||
function createServer () { | ||
const s = server.createServer() | ||
|
||
s.on('/', function (req, res) { | ||
res.writeHead(200, { 'content-type': 'text/plain' }) | ||
res.end() | ||
}) | ||
|
||
return s | ||
} | ||
|
||
tape('setup', function (t) { | ||
s = createServer() | ||
s.listen(0, function () { | ||
t.end() | ||
}) | ||
}) | ||
|
||
tape('should reuse the same agent', function (t) { | ||
const data = { | ||
url: s.url + '/', | ||
agentIdleTimeout: 1000 | ||
} | ||
|
||
const r1 = request(data, function (err, res) { | ||
t.equal(err, null) | ||
t.equal(res.statusCode, 200) | ||
const r2 = request(data, function (err) { | ||
t.equal(err, null) | ||
t.end() | ||
t.equal(r1.agent.identifier, r2.agent.identifier) | ||
}) | ||
}) | ||
r1.agent.identifier = '1234' | ||
}) | ||
|
||
tape('should use new agent after timeout', function (t) { | ||
const data = { | ||
url: s.url + '/', | ||
agentIdleTimeout: 100 | ||
} | ||
|
||
const r1 = request(data, function (err, res) { | ||
t.equal(err, null) | ||
t.equal(res.statusCode, 200) | ||
setTimeout(() => { | ||
const r2 = request(data, function (err) { | ||
t.equal(err, null) | ||
t.end() | ||
t.notEqual(r1.agent.identifier, r2.agent.identifier) | ||
}) | ||
}, 200) | ||
}) | ||
r1.agent.identifier = '12345' | ||
}) | ||
|
||
tape('cleanup', function (t) { | ||
s.close(function () { | ||
t.end() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict' | ||
|
||
var request = require('../index') | ||
var tape = require('tape') | ||
var server = require('./server') | ||
var destroyable = require('server-destroy') | ||
|
||
const s = server.createHttp2Server() | ||
|
||
destroyable(s) | ||
|
||
tape('setup', function (t) { | ||
s.listen(0, function () { | ||
t.end() | ||
}) | ||
}) | ||
|
||
function addTest (errorCode, data = {}) { | ||
tape('test ' + errorCode, function (t) { | ||
s.on('/' + errorCode, function (req, res) { | ||
if (errorCode === 0) { | ||
res.end() | ||
return | ||
} | ||
res.stream.close(errorCode) | ||
}) | ||
data.uri = s.url + '/' + errorCode | ||
request( | ||
{ ...data, strictSSL: false, protocolVersion: 'auto' }, | ||
function (err) { | ||
if (errorCode === 0) { | ||
t.equal(err, null) | ||
t.end() | ||
return | ||
} | ||
if (errorCode === 8) { | ||
t.equal(err.message, `HTTP/2 Stream closed with error code NGHTTP2_CANCEL`) | ||
t.end() | ||
return | ||
} | ||
t.equal(err.message, `Stream closed with error code ${errorCodes[errorCode]}`) | ||
t.end() | ||
} | ||
) | ||
}) | ||
} | ||
|
||
const errorCodes = [ | ||
'NGHTTP2_NO_ERROR', | ||
'NGHTTP2_PROTOCOL_ERROR', | ||
'NGHTTP2_INTERNAL_ERROR', | ||
'NGHTTP2_FLOW_CONTROL_ERROR', | ||
'NGHTTP2_SETTINGS_TIMEOUT', | ||
'NGHTTP2_STREAM_CLOSED', | ||
'NGHTTP2_FRAME_SIZE_ERROR', | ||
'NGHTTP2_REFUSED_STREAM', | ||
'NGHTTP2_CANCEL', | ||
'NGHTTP2_COMPRESSION_ERROR', | ||
'NGHTTP2_CONNECT_ERROR', | ||
'NGHTTP2_ENHANCE_YOUR_CALM', | ||
'NGHTTP2_INADEQUATE_SECURITY', | ||
'NGHTTP2_HTTP_1_1_REQUIRED' | ||
] | ||
|
||
for (let i = 0; i < errorCodes.length; i++) { | ||
addTest(i) | ||
} | ||
|
||
tape('cleanup', function (t) { | ||
s.destroy(function () { | ||
t.end() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict' | ||
|
||
var request = require('../index') | ||
var tape = require('tape') | ||
var server = require('./server') | ||
var destroyable = require('server-destroy') | ||
|
||
const s = server.createHttp2Server() | ||
|
||
destroyable(s) | ||
|
||
tape('setup', function (t) { | ||
s.listen(0, function () { | ||
t.end() | ||
}) | ||
}) | ||
|
||
function addTest (errorCode, data = {}) { | ||
tape('test ' + errorCode, function (t) { | ||
s.on('/' + errorCode, function (req, res) { | ||
if (errorCode === 0) { | ||
res.end() | ||
return | ||
} | ||
res.stream.close(errorCode) | ||
}) | ||
data.uri = s.url + '/' + errorCode | ||
request( | ||
{ ...data, strictSSL: false, protocolVersion: 'http2' }, | ||
function (err, resp, body) { | ||
if (errorCode === 0) { | ||
t.equal(err, null) | ||
t.end() | ||
return | ||
} | ||
if (errorCode === 8) { | ||
t.equal(err.message, `HTTP/2 Stream closed with error code NGHTTP2_CANCEL`) | ||
t.end() | ||
return | ||
} | ||
t.equal(err.message, `Stream closed with error code ${errorCodes[errorCode]}`) | ||
t.end() | ||
} | ||
) | ||
}) | ||
} | ||
|
||
const errorCodes = [ | ||
'NGHTTP2_NO_ERROR', | ||
'NGHTTP2_PROTOCOL_ERROR', | ||
'NGHTTP2_INTERNAL_ERROR', | ||
'NGHTTP2_FLOW_CONTROL_ERROR', | ||
'NGHTTP2_SETTINGS_TIMEOUT', | ||
'NGHTTP2_STREAM_CLOSED', | ||
'NGHTTP2_FRAME_SIZE_ERROR', | ||
'NGHTTP2_REFUSED_STREAM', | ||
'NGHTTP2_CANCEL', | ||
'NGHTTP2_COMPRESSION_ERROR', | ||
'NGHTTP2_CONNECT_ERROR', | ||
'NGHTTP2_ENHANCE_YOUR_CALM', | ||
'NGHTTP2_INADEQUATE_SECURITY', | ||
'NGHTTP2_HTTP_1_1_REQUIRED' | ||
] | ||
|
||
for (let i = 0; i < errorCodes.length; i++) { | ||
addTest(i) | ||
} | ||
|
||
tape('cleanup', function (t) { | ||
s.destroy(function () { | ||
t.end() | ||
}) | ||
}) |
Oops, something went wrong.