source: trip-planner-front/node_modules/needle/test/headers_spec.js@ fa375fe

Last change on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 5.8 KB
Line 
1var http = require('http'),
2 helpers = require('./helpers'),
3 should = require('should');
4
5var port = 54321;
6
7describe('request headers', function() {
8
9 var needle,
10 server,
11 existing_sockets,
12 original_defaultMaxSockets;
13
14 before(function(done) {
15 setTimeout(function() {
16 existing_sockets = get_active_sockets().length;
17 server = helpers.server({ port: port }, done);
18 }, 100);
19 })
20
21 after(function(done) {
22 server.close(done);
23 })
24
25 function send_request(opts, cb) {
26 needle.get('http://localhost:' + port, opts, cb);
27 }
28
29 function get_active_sockets() {
30 var handles = process._getActiveHandles();
31
32 return handles.filter(function(el) {
33 if (el.constructor.name.toString() == 'Socket') {
34 return el.destroyed !== true;
35 }
36 })
37 }
38
39 describe('old node versions (<0.11.4) with persistent keep-alive connections', function() {
40
41 before(function() {
42 delete require.cache[require.resolve('..')] // in case it was already loaded
43 original_defaultMaxSockets = http.Agent.defaultMaxSockets;
44 http.Agent.defaultMaxSockets = 5;
45 needle = require('..');
46 })
47
48 after(function() {
49 http.Agent.defaultMaxSockets = original_defaultMaxSockets;
50 delete require.cache[require.resolve('..')]
51 })
52
53 describe('default options', function() {
54
55 it('sends a Connection: close header', function(done) {
56 send_request({}, function(err, resp) {
57 resp.body.headers['connection'].should.eql('close');
58 done();
59 })
60 })
61
62 it('no open sockets remain after request', function(done) {
63 send_request({}, function(err, resp) {
64 setTimeout(function() {
65 get_active_sockets().length.should.eql(existing_sockets);
66 done();
67 }, 10)
68 });
69 })
70
71 })
72
73 describe('passing connection: close', function() {
74
75 it('sends a Connection: close header', function(done) {
76 send_request({ connection: 'close' }, function(err, resp) {
77 resp.body.headers['connection'].should.eql('close');
78 done();
79 })
80 })
81
82 it('no open sockets remain after request', function(done) {
83 send_request({ connection: 'close' }, function(err, resp) {
84 setTimeout(function() {
85 get_active_sockets().length.should.eql(existing_sockets);
86 done();
87 }, 10)
88 });
89 })
90
91 })
92
93 describe('passing connection: keep-alive', function() {
94
95 it('sends a Connection: keep-alive header (using options.headers.connection)', function(done) {
96 send_request({ headers: { connection: 'keep-alive' }}, function(err, resp) {
97 resp.body.headers['connection'].should.eql('keep-alive');
98 done();
99 })
100 })
101
102 it('sends a Connection: keep-alive header (using options.connection)', function(done) {
103 send_request({ connection: 'keep-alive' }, function(err, resp) {
104 resp.body.headers['connection'].should.eql('keep-alive');
105 done();
106 })
107 })
108
109 it('one open socket remain after request', function(done) {
110 send_request({ connection: 'keep-alive' }, function(err, resp) {
111 get_active_sockets().length.should.eql(existing_sockets + 1);
112 done();
113 });
114 })
115
116 })
117
118 })
119
120 describe('new node versions with smarter connection disposing', function() {
121
122 before(function() {
123 delete require.cache[require.resolve('..')]
124 original_defaultMaxSockets = http.Agent.defaultMaxSockets;
125 http.Agent.defaultMaxSockets = Infinity;
126 needle = require('..');
127 })
128
129 after(function() {
130 http.Agent.defaultMaxSockets = original_defaultMaxSockets;
131 delete require.cache[require.resolve('..')]
132 })
133
134 describe('default options', function() {
135
136 // TODO:
137 // this is weird. by default, new node versions set a 'close' header
138 // while older versions set a keep-alive header
139
140 it.skip('sets a Connection header', function(done) {
141 send_request({}, function(err, resp) {
142 // should.not.exist(resp.body.headers['connection']);
143 // done();
144 })
145 })
146
147 it.skip('one open sockets remain after request', function(done) {
148 send_request({}, function(err, resp) {
149 // get_active_sockets().length.should.eql(1);
150 // done();
151 });
152 })
153
154 })
155
156 describe('passing connection: close', function() {
157
158 it('sends a Connection: close header', function(done) {
159 send_request({ connection: 'close' }, function(err, resp) {
160 resp.body.headers['connection'].should.eql('close');
161 done();
162 })
163 })
164
165 it('no open sockets remain after request', function(done) {
166 send_request({ connection: 'close' }, function(err, resp) {
167 setTimeout(function() {
168 get_active_sockets().length.should.eql(existing_sockets);
169 done();
170 }, 10);
171 });
172 })
173
174 })
175
176 describe('passing connection: keep-alive', function() {
177
178 it('sends a Connection: keep-alive header (using options.headers.connection)', function(done) {
179 send_request({ headers: { connection: 'keep-alive' }}, function(err, resp) {
180 resp.body.headers['connection'].should.eql('keep-alive');
181 done();
182 })
183 })
184
185 it('sends a Connection: keep-alive header (using options.connection)', function(done) {
186 send_request({ connection: 'keep-alive' }, function(err, resp) {
187 resp.body.headers['connection'].should.eql('keep-alive');
188 done();
189 })
190 })
191
192 it('one open socket remain after request', function(done) {
193 send_request({ connection: 'keep-alive' }, function(err, resp) {
194 get_active_sockets().length.should.eql(existing_sockets + 1);
195 done();
196 });
197 })
198
199 })
200
201 })
202
203})
Note: See TracBrowser for help on using the repository browser.