fetch-test.js
10.7 KB
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
/* eslint no-unused-expressions:0 */
/* globals afterEach, beforeEach, describe, it */
'use strict';
var chai = require('chai');
var expect = chai.expect;
//var http = require('http');
var fetch = require('../lib/fetch');
var http = require('http');
var https = require('https');
chai.config.includeStack = true;
var HTTP_PORT = 9998;
var HTTPS_PORT = 9993;
var USE_ALLOC = typeof Buffer.alloc === 'function';
var httpsOptions = {
key: '-----BEGIN RSA PRIVATE KEY-----\n' +
'MIIEpAIBAAKCAQEA6Z5Qqhw+oWfhtEiMHE32Ht94mwTBpAfjt3vPpX8M7DMCTwHs\n' +
'1xcXvQ4lQ3rwreDTOWdoJeEEy7gMxXqH0jw0WfBx+8IIJU69xstOyT7FRFDvA1yT\n' +
'RXY2yt9K5s6SKken/ebMfmZR+03ND4UFsDzkz0FfgcjrkXmrMF5Eh5UXX/+9YHeU\n'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1HGB+LCBJvnQbDg0\n' +
'BBoMjGv8Mk90uGCeljpRAgMBAAEwDQYJKoZIhvcNAQELBQADggEBABXm8GPdY0sc\n' +
'mMUFlgDqFzcevjdGDce0QfboR+M7WDdm512Jz2SbRTgZD/4na42ThODOZz9z1AcM\n' +
'zLgx2ZNZzVhBz0odCU4JVhOCEks/OzSyKeGwjIb4JAY7dh+Kju1+6MNfQJ4r1Hza\n' +
'SVXH0+JlpJDaJ73NQ2JyfqELmJ1mTcptkA/N6rQWhlzycTBSlfogwf9xawgVPATP\n' +
'4AuwgjHl12JI2HVVs1gu65Y3slvaHRCr0B4+Kg1GYNLLcbFcK+NEHrHmPxy9TnTh\n' +
'Zwp1dsNQU+Xkylz8IUANWSLHYZOMtN2e5SKIdwTtl5C8YxveuY8YKb1gDExnMraT\n' +
'VGXQDqPleug=\n' +
'-----END CERTIFICATE-----'
};
describe('fetch tests', function () {
this.timeout(10000); // eslint-disable-line
var httpServer, httpsServer;
beforeEach(function (done) {
httpServer = http.createServer(function (req, res) {
switch (req.url) {
case '/redirect6':
res.writeHead(302, {
Location: '/redirect5'
});
res.end();
break;
case '/redirect5':
res.writeHead(302, {
Location: '/redirect4'
});
res.end();
break;
case '/redirect4':
res.writeHead(302, {
Location: '/redirect3'
});
res.end();
break;
case '/redirect3':
res.writeHead(302, {
Location: '/redirect2'
});
res.end();
break;
case '/redirect2':
res.writeHead(302, {
Location: '/redirect1'
});
res.end();
break;
case '/redirect1':
res.writeHead(302, {
Location: '/'
});
res.end();
break;
case '/gzip':
res.writeHead(200, {
'Content-Type': 'text/plain',
'Content-Encoding': 'gzip'
});
var str = 'H4sIAAAAAAAAA/NIzcnJVwjPL8pJUfAICQngAgCwsOrsEQAAAA==';
var strSize = Buffer.byteLength(str, 'base64');
var strBuf = USE_ALLOC ? Buffer.alloc(strSize, str, 'base64') : new Buffer(str, 'base64');
res.end(strBuf);
break;
case '/invalid':
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.end('Hello World HTTP\n');
break;
case '/auth':
res.writeHead(200, {
'Content-Type': 'text/plain'
});
var auth = req.headers.authorization.split(' ').pop();
var authSize = Buffer.byteLength(auth, 'base64');
var authBuf = USE_ALLOC ? Buffer.alloc(authSize, auth, 'base64') : new Buffer(auth, 'base64');
res.end(authBuf);
break;
default:
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end('Hello World HTTP\n');
}
});
httpsServer = https.createServer(httpsOptions, function (req, res) {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end('Hello World HTTPS\n');
});
httpServer.listen(HTTP_PORT, function () {
httpsServer.listen(HTTPS_PORT, done);
});
});
afterEach(function (done) {
httpServer.close(function () {
httpsServer.close(done);
});
});
it('should fetch HTTP data', function (done) {
var req = new fetch.FetchStream('http://localhost:' + HTTP_PORT);
var buf = [];
req.on('data', function (chunk) {
buf.push(chunk);
});
req.on('end', function () {
expect(Buffer.concat(buf).toString()).to.equal('Hello World HTTP\n');
done();
});
});
it('should fetch HTTPS data', function (done) {
var req = new fetch.FetchStream('https://localhost:' + HTTPS_PORT, {
rejectUnauthorized: false
});
var buf = [];
req.on('data', function (chunk) {
buf.push(chunk);
});
req.on('end', function () {
expect(Buffer.concat(buf).toString()).to.equal('Hello World HTTPS\n');
done();
});
});
it('should fail on self signed HTTPS certificate', function (done) {
var req = new fetch.FetchStream('https://localhost:' + HTTPS_PORT);
req.on('error', function (err) {
expect(err).to.exist;
done();
});
req.on('data', function () {
expect(false).to.be.true;
});
req.on('end', function () {
expect(false).to.be.true;
});
});
it('should fetch HTTP data with redirects', function (done) {
var req = new fetch.FetchStream('http://localhost:' + HTTP_PORT + '/redirect3');
var buf = [];
req.on('data', function (chunk) {
buf.push(chunk);
});
req.on('end', function () {
expect(Buffer.concat(buf).toString()).to.equal('Hello World HTTP\n');
done();
});
});
it('should not follow too many redirects', function (done) {
var req = new fetch.FetchStream('http://localhost:' + HTTP_PORT + '/redirect6', {
maxRedirects: 5
});
req.on('meta', function (meta) {
expect(meta.status).to.equal(302);
});
var buf = [];
req.on('data', function (chunk) {
buf.push(chunk);
});
req.on('end', function () {
done();
});
});
it('should unzip compressed HTTP data', function (done) {
var req = new fetch.FetchStream('http://localhost:' + HTTP_PORT + '/gzip');
var buf = [];
req.on('data', function (chunk) {
buf.push(chunk);
});
req.on('end', function () {
expect(Buffer.concat(buf).toString()).to.equal('Hello World HTTP\n');
done();
});
});
it('should return error for unresolved host', function (done) {
var req = new fetch.FetchStream('http://asfhaskhhgbjdsfhgbsdjgk');
var buf = [];
req.on('data', function (chunk) {
buf.push(chunk);
});
req.on('error', function (err) {
expect(err).to.exist;
done();
});
req.on('end', function () {});
});
it('should handle basic HTTP auth', function (done) {
var req = new fetch.FetchStream('http://user:pass@localhost:' + HTTP_PORT + '/auth');
var buf = [];
req.on('data', function (chunk) {
buf.push(chunk);
});
req.on('end', function () {
expect(Buffer.concat(buf).toString()).to.equal('user:pass');
done();
});
});
it('should return error for invalid protocol', function (done) {
var req = new fetch.FetchStream('http://localhost:' + HTTPS_PORT, {
timeout: 1000
});
var buf = [];
req.on('data', function (chunk) {
buf.push(chunk);
});
req.on('error', function (err) {
expect(err).to.exist;
done();
});
req.on('end', function () {});
});
});