Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
test('bandwidth', function (done) {
const opts = { bps: 1, threshold: 5 }
const req = new http.IncomingMessage()
const res = clone.clonePrototype(new http.OutgoingMessage())
const buf = []
var lastWrite = Date.now()
Object.getPrototypeOf(res).write = function (buffer, encoding, next) {
expect(buffer).to.have.length(opts.bps)
expect(Date.now() - lastWrite).to.be.least(opts.threshold - 1)
lastWrite = Date.now()
buf.push(buffer)
next()
}
Object.getPrototypeOf(res).end = function () {
expect(buf).to.have.length(11)
expect(buf.join('')).to.be.equal('Hello World')
console.log('')
test('read', function (done) {
const threshold = 5
const spy = sinon.spy()
const init = Date.now()
const res = new http.OutgoingMessage()
const req = clone.clonePrototype(new http.IncomingMessage())
req.method = 'POST'
Object.getPrototypeOf(req).push = function (data) {
spy(data)
if (data === null) assert()
}
slowRead({ chunk: 1, threshold: threshold })(req, res, spy)
req.push(new Buffer('Hello World'))
req.push(null)
function assert () {
expect(Date.now() - init).to.be.at.least(threshold * 10)
expect(spy.args).to.have.length(13)
expect(spy.args.shift()[0]).to.be.undefined
test('small chunks', function (done) {
const opts = { chunk: 1, threshold: 5 }
const buf = []
const req = new http.IncomingMessage()
const res = clone.clonePrototype(new http.OutgoingMessage())
var lastWrite = Date.now()
Object.getPrototypeOf(res).write = function (buffer, encoding, next) {
expect(buffer).to.have.length(opts.chunk)
expect(Date.now() - lastWrite).to.be.at.least(opts.threshold - 1)
lastWrite = Date.now()
buf.push(buffer)
next()
}
Object.getPrototypeOf(res).end = function () {
expect(buf).to.have.length(11)
expect(buf.join('')).to.be.equal('Hello World')
done()
}
test('close', function (done) {
const delay = 50
const expected = { body: 'Hello', code: 200, headers: { server: 'rocky' } }
const spy = sinon.spy()
const init = Date.now()
const res = clone.clonePrototype({})
Object.getPrototypeOf(res).writeHead = spy
Object.getPrototypeOf(res).end = function (body) {
spy(body)
end()
}
slowClose({ delay: delay })(null, res, spy)
res.writeHead(200, { 'content-length': 100, server: 'rocky' })
res.end(expected.body)
function end (err) {
expect(Date.now() - init).to.be.at.least(delay - 1)
expect(spy.calledThrice).to.be.true
expect(spy.args[1][0]).to.be.equal(expected.code)
expect(spy.args[1][1]).to.be.deep.equal(expected.headers)
test('premature close', function (done) {
const threshold = 10
const spy = sinon.spy()
const init = Date.now()
const res = new http.OutgoingMessage()
const req = clone.clonePrototype(new http.IncomingMessage())
req.method = 'POST'
Object.getPrototypeOf(req).push = function (data) {
spy(data)
if (data === null) assert()
}
slowRead({ chunk: 1, threshold: threshold })(req, res, spy)
req.emit('close')
req.push(new Buffer('Hello World'))
req.push(null)
function assert () {
expect(Date.now() - init).to.be.within(0, 5)