-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathnode_http_client.spec.js
332 lines (307 loc) · 13.3 KB
/
node_http_client.spec.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Generated by CoffeeScript 1.12.3
(function() {
var AWS, EventEmitter, helpers, httpModule;
helpers = require('./helpers');
var ShakyStream = require('./mocks/shaky-stream');
AWS = helpers.AWS;
EventEmitter = require('events').EventEmitter;
httpModule = require('http');
if (AWS.util.isNode()) {
describe('AWS.NodeHttpClient', function() {
var http;
http = new AWS.NodeHttpClient();
describe('maxSockets delegation', function() {
it('delegates maxSockets from agent to globalAgent', function() {
var agent, https;
https = require('https');
agent = http.getAgent(true);
https.globalAgent.maxSockets = 5;
expect(https.globalAgent.maxSockets).to.equal(agent.maxSockets);
https.globalAgent.maxSockets += 1;
return expect(https.globalAgent.maxSockets).to.equal(agent.maxSockets);
});
it('overrides globalAgent value if global is set to Infinity', function() {
var agent, https;
https = require('https');
agent = http.getAgent(true);
https.globalAgent.maxSockets = 2e308;
return expect(agent.maxSockets).to.equal(50);
});
return it('overrides globalAgent value if global is set to false', function() {
var agent, https, oldGlobal;
https = require('https');
oldGlobal = https.globalAgent;
https.globalAgent = false;
agent = http.getAgent(true);
expect(agent.maxSockets).to.equal(50);
return https.globalAgent = oldGlobal;
});
});
describe('default agent', function() {
beforeEach(function() {
AWS.NodeHttpClient.agent = undefined;
AWS.NodeHttpClient.sslAgent = undefined;
});
it('should use default agent when the agent is passed as undefined', function(done) {
process.env.AWS_NODEJS_CONNECTION_REUSE_ENABLED = '1';
var req;
req = new AWS.HttpRequest('http://invalid');
return http.handleRequest(req, { timeout: 1, agent: undefined }, null, function(err) {
expect(AWS.NodeHttpClient.agent).not.to.be.undefined;
expect(AWS.NodeHttpClient.sslAgent).to.be.undefined;
if (httpModule.globalAgent && httpModule.globalAgent.keepAlive !== undefined) {
//keepAlive is introduced only after v0.12
expect(AWS.NodeHttpClient.agent.keepAlive).to.equal(true);
}
expect(req.stream.agent.keepAlive).to.equal(true);
delete process.env.AWS_NODEJS_CONNECTION_REUSE_ENABLED;
return done();
});
});
it('should conform connection reuse opt-in environment variable', function(done) {
process.env.AWS_NODEJS_CONNECTION_REUSE_ENABLED = '1';
var req;
req = new AWS.HttpRequest('http://invalid');
return http.handleRequest(req, { timeout: 1 }, null, function(err) {
expect(AWS.NodeHttpClient.agent).not.to.be.undefined;
expect(AWS.NodeHttpClient.sslAgent).to.be.undefined;
if (httpModule.globalAgent && httpModule.globalAgent.keepAlive !== undefined) {
//keepAlive is introduced only after v0.12
expect(AWS.NodeHttpClient.agent.keepAlive).to.equal(true);
}
delete process.env.AWS_NODEJS_CONNECTION_REUSE_ENABLED;
return done();
});
});
it('should conform connection reuse opt-in environment variable when using https', function(done) {
process.env.AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1;
var req;
req = new AWS.HttpRequest('https://invalid');
return http.handleRequest(req, { timeout: 1 }, null, function(err) {
expect(AWS.NodeHttpClient.agent).to.be.undefined;
expect(AWS.NodeHttpClient.sslAgent).not.to.be.undefined;
if (httpModule.globalAgent && httpModule.globalAgent.keepAlive !== undefined) {
//keepAlive is introduced only after v0.12
expect(AWS.NodeHttpClient.sslAgent.keepAlive).to.equal(true);
}
delete process.env.AWS_NODEJS_CONNECTION_REUSE_ENABLED;
return done();
});
});
it('should respect tls reject unauthorized environment variable off', function(done) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
var req;
req = new AWS.HttpRequest('https://invalid');
return http.handleRequest(req, { timeout: 1 }, null, function(err) {
expect(AWS.NodeHttpClient.agent).to.be.undefined;
expect(AWS.NodeHttpClient.sslAgent).not.to.be.undefined;
expect(AWS.NodeHttpClient.sslAgent.options.rejectUnauthorized).to.equal(false);
delete process.env.NODE_TLS_REJECT_UNAUTHORIZED;
return done();
});
});
it('should respect tls reject unauthorized environment variable on', function(done) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '1';
var req;
req = new AWS.HttpRequest('https://invalid');
return http.handleRequest(req, { timeout: 1 }, null, function(err) {
expect(AWS.NodeHttpClient.agent).to.be.undefined;
expect(AWS.NodeHttpClient.sslAgent).not.to.be.undefined;
expect(AWS.NodeHttpClient.sslAgent.options.rejectUnauthorized).to.equal(true);
delete process.env.NODE_TLS_REJECT_UNAUTHORIZED;
return done();
});
});
it('should default to reject unauthorized if environment variable not present', function(done) {
var tempUnauthorized = process.env.NODE_TLS_REJECT_UNAUTHORIZED;
delete process.env.NODE_TLS_REJECT_UNAUTHORIZED;
var req;
req = new AWS.HttpRequest('https://invalid');
return http.handleRequest(req, { timeout: 1 }, null, function(err) {
expect(AWS.NodeHttpClient.agent).to.be.undefined;
expect(AWS.NodeHttpClient.sslAgent).not.to.be.undefined;
expect(AWS.NodeHttpClient.sslAgent.options.rejectUnauthorized).to.equal(true);
process.env.NODE_TLS_REJECT_UNAUTHORIZED = tempUnauthorized;
return done();
});
});
});
return describe('handleRequest', function() {
it('emits error event', function(done) {
var req;
req = new AWS.HttpRequest('http://invalid');
return http.handleRequest(req, {}, null, function(err) {
expect(err.code).to.equal('ENOTFOUND');
return done();
});
});
it('supports timeout in httpOptions', function() {
var numCalls, req;
numCalls = 0;
req = new AWS.HttpRequest('http://1.1.1.1');
return http.handleRequest(req, {
timeout: 1
}, null, function(err) {
numCalls += 1;
expect(err.code).to.equal('TimeoutError');
expect(err.message).to.equal('Connection timed out after 1ms');
return expect(numCalls).to.equal(1);
});
});
it('supports connectTimeout in httpOptions', function() {
var numCalls, req;
numCalls = 0;
req = new AWS.HttpRequest('http://10.255.255.255');
return http.handleRequest(req, {
connectTimeout: 1
}, null, function(err) {
numCalls += 1;
expect(err.code).to.equal('TimeoutError');
expect(err.message).to.equal('Socket timed out without establishing a connection');
return expect(numCalls).to.equal(1);
});
});
describe('ECONNRESET', function() {
var mockClientRequest = null;
var oldRequest = httpModule.request;
beforeEach(function() {
mockClientRequest = new EventEmitter();
mockClientRequest.setTimeout = function() {
return {};
};
mockClientRequest.abort = function() {
return {};
};
mockClientRequest.end = function() {
return {};
};
return requestSpy = helpers.spyOn(httpModule, 'request').andReturn(mockClientRequest);
});
afterEach(function() {
httpModule.request = oldRequest;
});
it('retries ECONNRESET', function() {
var req = new AWS.HttpRequest('http://10.255.255.255');
var numCalls = 0;
http.handleRequest(req, {}, null, function(err) {
numCalls += 1;
expect(err.code).to.equal('TimeoutError');
expect(err.message).to.equal('test ECONNRESET');
return expect(numCalls).to.equal(1);
});
mockClientRequest.emit('error', { code: 'ECONNRESET', message: 'test ECONNRESET' });
});
});
describe('timeout', function() {
it('is obeyed even after response headers are recieved', function(done) {
// a mock server with 'ShakyStream' allows us to simulate a period of socket inactivity
var server = httpModule.createServer(function(req, res) {
res.setHeader('Content-Type', 'application/json');
var ss = new ShakyStream({
pauseFor: 1000 // simulate 1 second pause while receiving data
});
ss.pipe(res);
}).listen(3334);
var ddb = new AWS.DynamoDB({
httpOptions: {
timeout: 100
},
endpoint: 'http://127.0.0.1:3334'
});
ddb.scan({
TableName: 'fake'
}, function(err, data) {
server.close();
expect(err.name).to.equal('TimeoutError');
done();
});
});
it('does not trigger unnecessarily', function(done) {
// a mock server with 'ShakyStream' allows us to simulate a period of socket inactivity
var server = httpModule.createServer(function(req, res) {
res.setHeader('Content-Type', 'application/json');
var ss = new ShakyStream({
pauseFor: 100 // simulate 100 ms pause while receiving data
});
ss.pipe(res);
}).listen(3334);
var ddb = new AWS.DynamoDB({
httpOptions: {
timeout: 1000
},
endpoint: 'http://127.0.0.1:3334'
});
ddb.scan({
TableName: 'fake'
}, function(err, data) {
server.close();
expect(err).to.eql(null);
done();
});
});
});
return describe('connectTimeout', function() {
var clearTimeoutSpy, mockClientRequest, oldClearTimeout, oldRequest, oldSetTimeout, requestSpy, setTimeoutSpy, timeoutId;
timeoutId = 'TIMEOUT_ID';
oldSetTimeout = global.setTimeout;
oldClearTimeout = global.clearTimeout;
setTimeoutSpy = null;
clearTimeoutSpy = null;
oldRequest = httpModule.request;
requestSpy = null;
mockClientRequest = null;
beforeEach(function() {
setTimeoutSpy = helpers.spyOn(global, 'setTimeout').andReturn(timeoutId);
clearTimeoutSpy = helpers.spyOn(global, 'clearTimeout').andCallFake(function() {
return {};
});
mockClientRequest = new EventEmitter();
mockClientRequest.setTimeout = function() {
return {};
};
mockClientRequest.end = function() {
return {};
};
return requestSpy = helpers.spyOn(httpModule, 'request').andReturn(mockClientRequest);
});
afterEach(function() {
global.setTimeout = oldSetTimeout;
global.clearTimeout = oldClearTimeout;
return httpModule.request = oldRequest;
});
it('clears timeouts once the connection has been established', function() {
var mockSocket, req;
req = new AWS.HttpRequest('http://10.255.255.255');
http.handleRequest(req, {
connectTimeout: 120000
}, null, function() {
return {};
});
mockSocket = new EventEmitter();
mockSocket.connecting = true;
mockClientRequest.emit('socket', mockSocket);
expect(setTimeoutSpy.calls.length).to.equal(1);
mockSocket.emit('connect');
expect(clearTimeoutSpy.calls.length).to.equal(1);
expect(clearTimeoutSpy.calls[0]['arguments'][0]).to.equal(timeoutId);
});
it('clears timeouts if an error is encountered', function() {
var mockSocket = new EventEmitter();
var req = new AWS.HttpRequest('http://10.255.255.255');
http.handleRequest(req, {
connectTimeout: 120000
}, null, function() {
return {};
});
mockSocket.connecting = true;
mockClientRequest.emit('socket', mockSocket);
expect(setTimeoutSpy.calls.length).to.equal(1);
mockClientRequest.emit('error', new Error('Something happened!'));
expect(clearTimeoutSpy.calls.length).to.equal(1);
expect(clearTimeoutSpy.calls[0]['arguments'][0]).to.equal(timeoutId);
});
});
});
});
}
}).call(this);