|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | 3 | var request = require('../index') |
4 | | - , http = require('http') |
5 | | - , tape = require('tape') |
| 4 | + , version = require('../lib/helpers').version |
| 5 | + , http = require('http') |
| 6 | + , ForeverAgent = require('forever-agent') |
| 7 | + , tape = require('tape') |
6 | 8 |
|
7 | | -var s = http.createServer(function(req, res) { |
| 9 | +var s = http.createServer(function (req, res) { |
8 | 10 | res.statusCode = 200 |
9 | | - res.end('ok') |
| 11 | + res.end() |
10 | 12 | }) |
11 | 13 |
|
12 | | -tape('setup', function(t) { |
| 14 | +tape('setup', function (t) { |
13 | 15 | s.listen(6767, function() { |
14 | 16 | t.end() |
15 | 17 | }) |
16 | 18 | }) |
17 | 19 |
|
18 | | -tape('should work with forever agent', function(t) { |
19 | | - var r = request.forever({maxSockets: 1}) |
| 20 | +function httpAgent (t, options, req) { |
| 21 | + var r = (req || request)(options, function (_err, res, body) { |
20 | 22 |
|
21 | | - r({ |
22 | | - url: 'http://localhost:6767', |
23 | | - headers: { 'Connection':'Close' } |
24 | | - }, function(err, resp, body) { |
25 | | - t.equal(err, null) |
26 | | - t.equal(body, 'ok') |
27 | | - t.end() |
| 23 | + t.ok(r.agent instanceof http.Agent, 'is http.Agent') |
| 24 | + t.equal(r.agent.options.keepAlive, true, 'is keepAlive') |
| 25 | + t.equal(Object.keys(r.agent.sockets).length, 1, '1 socket name') |
| 26 | + |
| 27 | + var name = (typeof r.agent.getName === 'function') |
| 28 | + ? r.agent.getName({port:6767}) |
| 29 | + : 'localhost:6767' // node 0.10- |
| 30 | + t.equal(r.agent.sockets[name].length, 1, '1 open socket') |
| 31 | + |
| 32 | + var socket = r.agent.sockets[name][0] |
| 33 | + socket.on('close', function () { |
| 34 | + t.equal(Object.keys(r.agent.sockets).length, 0, '0 open sockets') |
| 35 | + t.end() |
| 36 | + }) |
| 37 | + socket.end() |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +function foreverAgent (t, options, req) { |
| 42 | + var r = (req || request)(options, function (_err, res, body) { |
| 43 | + |
| 44 | + t.ok(r.agent instanceof ForeverAgent, 'is ForeverAgent') |
| 45 | + t.equal(Object.keys(r.agent.sockets).length, 1, '1 socket name') |
| 46 | + |
| 47 | + var name = 'localhost:6767' // node 0.10- |
| 48 | + t.equal(r.agent.sockets[name].length, 1, '1 open socket') |
| 49 | + |
| 50 | + var socket = r.agent.sockets[name][0] |
| 51 | + socket.on('close', function () { |
| 52 | + t.equal(Object.keys(r.agent.sockets[name]).length, 0, '0 open sockets') |
| 53 | + t.end() |
| 54 | + }) |
| 55 | + socket.end() |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +// http.Agent |
| 60 | + |
| 61 | +tape('options.agent', function (t) { |
| 62 | + httpAgent(t, { |
| 63 | + uri: 'http://localhost:6767', |
| 64 | + agent: new http.Agent({keepAlive: true}) |
28 | 65 | }) |
29 | 66 | }) |
30 | 67 |
|
31 | | -tape('cleanup', function(t) { |
| 68 | +tape('options.agentClass + options.agentOptions', function (t) { |
| 69 | + httpAgent(t, { |
| 70 | + uri: 'http://localhost:6767', |
| 71 | + agentClass: http.Agent, |
| 72 | + agentOptions: {keepAlive: true} |
| 73 | + }) |
| 74 | +}) |
| 75 | + |
| 76 | +// forever-agent |
| 77 | + |
| 78 | +tape('options.forever = true', function (t) { |
| 79 | + var v = version() |
| 80 | + var options = { |
| 81 | + uri: 'http://localhost:6767', |
| 82 | + forever: true |
| 83 | + } |
| 84 | + |
| 85 | + if (v.major === 0 && v.minor <= 10) {foreverAgent(t, options)} |
| 86 | + else {httpAgent(t, options)} |
| 87 | +}) |
| 88 | + |
| 89 | +tape('forever() method', function (t) { |
| 90 | + var v = version() |
| 91 | + var options = { |
| 92 | + uri: 'http://localhost:6767' |
| 93 | + } |
| 94 | + var r = request.forever({maxSockets: 1}) |
| 95 | + |
| 96 | + if (v.major === 0 && v.minor <= 10) {foreverAgent(t, options, r)} |
| 97 | + else {httpAgent(t, options, r)} |
| 98 | +}) |
| 99 | + |
| 100 | +tape('cleanup', function (t) { |
32 | 101 | s.close(function() { |
33 | 102 | t.end() |
34 | 103 | }) |
|
0 commit comments