Skip to content

Commit

Permalink
test: add failing test for pipelining client
Browse files Browse the repository at this point in the history
  • Loading branch information
calebboyd committed Apr 4, 2021
1 parent 0d88438 commit dae078f
Showing 1 changed file with 47 additions and 21 deletions.
68 changes: 47 additions & 21 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,12 @@ describe('onFinished(res, listener)', function () {
describe('when calling many times on same response', function () {
it('should not print warnings', function (done) {
var server = http.createServer(function (req, res) {
var stderr = captureStderr(function () {
for (var i = 0; i < 400; i++) {
onFinished(res, noop)
}
})

var stderr = captureStderr()
for (var i = 0; i < 400; i++) {
onFinished(res, noop)
}
onFinished(res, done)
assert.strictEqual(stderr, '')
assert.strictEqual(stderr(), '')
res.end()
})

Expand Down Expand Up @@ -537,14 +535,12 @@ describe('onFinished(req, listener)', function () {
describe('when calling many times on same request', function () {
it('should not print warnings', function (done) {
var server = http.createServer(function (req, res) {
var stderr = captureStderr(function () {
for (var i = 0; i < 400; i++) {
onFinished(req, noop)
}
})

var stderr = captureStderr()
for (var i = 0; i < 400; i++) {
onFinished(req, noop)
}
onFinished(req, done)
assert.strictEqual(stderr, '')
assert.strictEqual(stderr(), '')
res.end()
})

Expand All @@ -558,6 +554,39 @@ describe('onFinished(req, listener)', function () {
})
})

describe('when a client is pipelining requests', function () {
it('should not print warnings', function (done) {
var stderr = captureStderr()
var server = http.createServer(function (req, res) {
onFinished(req, noop)
onFinished(res, noop)
res.end()
})
server.on('close', function () {
assert.strictEqual(stderr(), '')
done()
})
server.listen(function () {
var pipelineCount = 8
net.connect(this.address().port, function () {
var socket = this
for (var i = 0; i < pipelineCount; i++) {
writeRequest(this)
}
var responses = ''
var resRe = /HTTP\/1.1/g
socket.on('data', function (chunk) {
responses += chunk
if (responses.match(resRe).length === pipelineCount) {
socket.end()
server.close()
}
})
})
})
})
})

describe('when CONNECT method', function () {
it('should fire when request finishes', function (done) {
var client
Expand Down Expand Up @@ -1031,21 +1060,18 @@ describe('isFinished(req)', function () {
})
})

function captureStderr (fn) {
function captureStderr () {
var chunks = []
var write = process.stderr.write

process.stderr.write = function write (chunk, encoding) {
chunks.push(new Buffer(chunk, encoding)) // eslint-disable-line node/no-deprecated-api
chunks.push(Buffer.from ? Buffer.from(chunk, encoding) : new Buffer(chunk, encoding)) // eslint-disable-line node/no-deprecated-api
}

try {
fn()
} finally {
return function restore () {
process.stderr.write = write
return Buffer.concat(chunks).toString('utf8')
}

return Buffer.concat(chunks).toString('utf8')
}

function close (server, callback) {
Expand Down

0 comments on commit dae078f

Please sign in to comment.