1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
'use strict'
var request = require('../index')
var http = require('http')
var tape = require('tape')
var s = http.createServer(function (req, res) {
res.statusCode = 200
res.end('asdf')
})
tape('setup', function (t) {
s.listen(0, function () {
s.url = 'http://localhost:' + this.address().port
t.end()
})
})
tape('pool', function (t) {
request({
url: s.url,
pool: false
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.equal(body, 'asdf')
var agent = res.request.agent
t.equal(agent, false)
t.end()
})
})
tape('forever', function (t) {
var r = request({
url: s.url,
forever: true,
pool: {maxSockets: 1024}
}, function (err, res, body) {
// explicitly shut down the agent
if (typeof r.agent.destroy === 'function') {
r.agent.destroy()
} else {
// node < 0.12
Object.keys(r.agent.sockets).forEach(function (name) {
r.agent.sockets[name].forEach(function (socket) {
socket.end()
})
})
}
t.equal(err, null)
t.equal(res.statusCode, 200)
t.equal(body, 'asdf')
var agent = res.request.agent
t.equal(agent.maxSockets, 1024)
t.end()
})
})
tape('forever, should use same agent in sequential requests', function (t) {
var r = request.defaults({
forever: true
})
var req1 = r(s.url)
var req2 = r(s.url + '/somepath')
req1.abort()
req2.abort()
if (typeof req1.agent.destroy === 'function') {
req1.agent.destroy()
}
if (typeof req2.agent.destroy === 'function') {
req2.agent.destroy()
}
t.equal(req1.agent, req2.agent)
t.end()
})
tape('forever, should use same agent in sequential requests(with pool.maxSockets)', function (t) {
var r = request.defaults({
forever: true,
pool: {maxSockets: 1024}
})
var req1 = r(s.url)
var req2 = r(s.url + '/somepath')
req1.abort()
req2.abort()
if (typeof req1.agent.destroy === 'function') {
req1.agent.destroy()
}
if (typeof req2.agent.destroy === 'function') {
req2.agent.destroy()
}
t.equal(req1.agent.maxSockets, 1024)
t.equal(req1.agent, req2.agent)
t.end()
})
tape('forever, should use same agent in request() and request.verb', function (t) {
var r = request.defaults({
forever: true,
pool: {maxSockets: 1024}
})
var req1 = r(s.url)
var req2 = r.get(s.url)
req1.abort()
req2.abort()
if (typeof req1.agent.destroy === 'function') {
req1.agent.destroy()
}
if (typeof req2.agent.destroy === 'function') {
req2.agent.destroy()
}
t.equal(req1.agent.maxSockets, 1024)
t.equal(req1.agent, req2.agent)
t.end()
})
tape('should use different agent if pool option specified', function (t) {
var r = request.defaults({
forever: true,
pool: {maxSockets: 1024}
})
var req1 = r(s.url)
var req2 = r.get({
url: s.url,
pool: {maxSockets: 20}
})
req1.abort()
req2.abort()
if (typeof req1.agent.destroy === 'function') {
req1.agent.destroy()
}
if (typeof req2.agent.destroy === 'function') {
req2.agent.destroy()
}
t.equal(req1.agent.maxSockets, 1024)
t.equal(req2.agent.maxSockets, 20)
t.notEqual(req1.agent, req2.agent)
t.end()
})
tape('cleanup', function (t) {
s.close(function () {
t.end()
})
})
|