/
server.js
947 lines (842 loc) · 42.3 KB
/
server.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
const fs = require('fs');
const path = require('path');
const stream = require('stream');
const childProcess = require('child_process');
const dns = require('dns');
const _ = require('underscore');
const { expect, assert } = require('chai');
const proxy = require('proxy');
const http = require('http');
const portastic = require('portastic');
const Promise = require('bluebird');
const request = require('request');
const url = require('url');
const WebSocket = require('faye-websocket');
const { parseUrl, parseProxyAuthorizationHeader } = require('../build/tools');
const { Server, RequestError } = require('../build/server');
const { TargetServer } = require('./target_server');
/* globals process */
/*
TODO - add following tests:
- gzip Content-Encoding
- IPv6 !!!
- raw TCP connection over proxy
- HandlerForward when connected through shader proxy threw error if source socket was closed instead of response, test why.
*/
// See README.md for details
const LOCALHOST_TEST = 'localhost-test';
const sslKey = fs.readFileSync(path.join(__dirname, 'ssl.key'));
const sslCrt = fs.readFileSync(path.join(__dirname, 'ssl.crt'));
// Enable self-signed certificates
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
const NON_EXISTENT_HOSTNAME = 'non-existent-hostname';
// Prepare testing data
const DATA_CHUNKS = [];
let DATA_CHUNKS_COMBINED = '';
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < 100; i++) {
let chunk = '';
for (let i = 0; i < 10000; i++) {
chunk += chars.charAt(Math.floor(Math.random() * chars.length));
}
DATA_CHUNKS.push(chunk);
DATA_CHUNKS_COMBINED += chunk;
}
const AUTH_REALM = 'Test Proxy'; // Test space in realm string
const requestPromised = (opts) => {
return new Promise((resolve, reject) => {
const result = request(opts, (error, response, body) => {
if (error) {
/* console.log('REQUEST');
console.dir(response);
console.dir(body);
console.dir(result); */
return reject(error);
}
resolve(response, body);
});
});
};
const wait = timeout => new Promise(resolve => setTimeout(resolve, timeout));
// Opens web page in phantomjs and returns the HTML content
const phantomGet = (url, proxyUrl) => {
const phantomPath = path.join(__dirname, '../node_modules/.bin/phantomjs');
const scriptPath = path.join(__dirname, './phantom_get.js');
let proxyParams = '';
if (proxyUrl) {
const parsed = parseUrl(proxyUrl);
proxyParams += `--proxy-type=http --proxy=${parsed.hostname}:${parsed.port} `;
if (parsed.username || parsed.password) {
if ((parsed.username && !parsed.password) || (!parsed.username && parsed.password)) {
throw new Error('PhantomJS cannot handle proxy only username or password!');
}
proxyParams += `--proxy-auth=${parsed.username}:${parsed.password} `;
}
}
return new Promise((resolve, reject) => {
const cmd = `${phantomPath} --ignore-ssl-errors=true ${proxyParams} ${scriptPath} ${url}`;
childProcess.exec(cmd, (error, stdout, stderr) => {
if (error) return reject(new Error(`Cannot open page in PhantomJS: ${error}: ${stderr || stdout}`));
resolve(stdout);
});
});
};
const createTestSuite = ({
useSsl, useMainProxy, mainProxyAuth, useUpstreamProxy, upstreamProxyAuth, testCustomResponse,
}) => {
return function () {
this.timeout(30 * 1000);
let freePorts;
let targetServerPort;
let targetServer;
let upstreamProxyServer;
let upstreamProxyPort;
let upstreamProxyWasCalled = false;
let upstreamProxyRequestCount = 0;
let mainProxyServer;
let mainProxyServerStatisticsInterval;
const mainProxyServerConnections = {};
let mainProxyServerPort;
const mainProxyRequestCount = 0;
const mainProxyServerConnectionIds = [];
const mainProxyServerConnectionsClosed = [];
const mainProxyServerConnectionId2Stats = {};
let baseUrl;
let mainProxyUrl;
const getRequestOpts = (pathOrUrl) => {
return {
url: pathOrUrl[0] === '/' ? `${baseUrl}${pathOrUrl}` : pathOrUrl,
key: sslKey,
proxy: mainProxyUrl,
headers: {},
timeout: 30000,
};
};
let counter = 0;
before(() => {
return portastic.find({ min: 50000, max: 50500 }).then((ports) => {
freePorts = ports;
// Setup target HTTP server
targetServerPort = freePorts.shift();
targetServer = new TargetServer({
port: targetServerPort, useSsl, sslKey, sslCrt,
});
return targetServer.listen();
}).then(() => {
// Setup proxy chain server
if (useUpstreamProxy) {
return new Promise((resolve, reject) => {
const upstreamProxyHttpServer = http.createServer();
// Setup upstream proxy authorization
upstreamProxyHttpServer.authenticate = function (req, fn) {
upstreamProxyRequestCount++;
// Special case: no authentication required
if (!upstreamProxyAuth) {
upstreamProxyWasCalled = true;
return fn(null, true);
}
// parse the "Proxy-Authorization" header
const auth = req.headers['proxy-authorization'];
if (!auth) {
// optimization: don't invoke the child process if no
// "Proxy-Authorization" header was given
// console.log('not Proxy-Authorization');
return fn(null, false);
}
const parsed = parseProxyAuthorizationHeader(auth);
const isEqual = _.isEqual(parsed, upstreamProxyAuth);
// console.log('Parsed "Proxy-Authorization": parsed: %j expected: %j : %s', parsed, upstreamProxyAuth, isEqual);
if (isEqual) upstreamProxyWasCalled = true;
fn(null, isEqual);
};
upstreamProxyHttpServer.on('error', (err) => {
console.dir(err);
throw new Error('Upstream proxy HTTP server failed');
});
upstreamProxyPort = freePorts.shift();
upstreamProxyServer = proxy(upstreamProxyHttpServer);
upstreamProxyServer.listen(upstreamProxyPort, (err) => {
if (err) return reject(err);
resolve();
});
});
}
}).then(() => {
// Setup main proxy server
if (useMainProxy) {
mainProxyServerPort = freePorts.shift();
const opts = {
port: mainProxyServerPort,
// verbose: true, // Enable this if you want verbose logs
};
if (mainProxyAuth || useUpstreamProxy || testCustomResponse) {
opts.prepareRequestFunction = ({
request, username, password, hostname, port, isHttp, connectionId,
}) => {
const result = {
requestAuthentication: false,
upstreamProxyUrl: null,
};
// If prepareRequestFunction() will cause error, don't add to this test array as it will fail in after()
let addToMainProxyServerConnectionIds = true;
expect(request).to.be.an('object');
expect(port).to.be.an('number');
if (hostname === 'activate-error-in-prep-req-func-throw') {
throw new Error('Testing error 1');
}
if (hostname === 'activate-error-in-prep-req-func-throw-known') {
throw new RequestError('Known error 1', 501);
}
if (hostname === 'activate-error-in-prep-req-func-promise') {
return Promise.reject(new Error('Testing error 2'));
}
if (hostname === 'activate-error-in-prep-req-func-promise-known') {
throw new RequestError('Known error 2', 501);
}
if (hostname === 'test-custom-response-simple') {
result.customResponseFunction = () => {
const trgParsed = parseUrl(request.url);
expect(trgParsed).to.deep.include({
host: hostname,
path: '/some/path'
});
return {
body: 'TEST CUSTOM RESPONSE SIMPLE',
};
};
// With SSL custom responses are not supported,
// we're testing this hence this below
if (useSsl) addToMainProxyServerConnectionIds = false;
}
if (hostname === 'test-custom-response-complex') {
result.customResponseFunction = () => {
const trgParsed = parseUrl(request.url);
expect(trgParsed).to.deep.include({
hostname,
path: '/some/path?query=456',
});
expect(port).to.be.eql(1234);
return {
statusCode: 201,
headers: {
'My-Test-Header1': 'bla bla bla',
'My-Test-Header2': 'bla bla bla2',
},
body: 'TEST CUSTOM RESPONSE COMPLEX',
};
};
}
if (hostname === 'test-custom-response-long') {
result.customResponseFunction = () => {
const trgParsed = parseUrl(request.url);
expect(trgParsed).to.deep.include({
host: hostname,
path: '/'
});
return {
body: 'X'.repeat(5000000),
};
};
}
if (hostname === 'test-custom-response-promised') {
result.customResponseFunction = () => {
const trgParsed = parseUrl(request.url);
expect(trgParsed).to.deep.include({
host: hostname,
path: '/some/path'
});
return Promise.resolve().then(() => {
return {
body: 'TEST CUSTOM RESPONSE PROMISED',
};
});
};
}
if (hostname === 'test-custom-response-invalid') {
result.customResponseFunction = 'THIS IS NOT A FUNCTION';
addToMainProxyServerConnectionIds = false;
}
if (mainProxyAuth) {
if (mainProxyAuth.username !== username || mainProxyAuth.password !== password) {
result.requestAuthentication = true;
addToMainProxyServerConnectionIds = false;
// Now that authentication is requested, upstream proxy should not get used to try some invalid one
result.upstreamProxyUrl = 'http://dummy-hostname:4567';
}
}
if (useUpstreamProxy && !result.upstreamProxyUrl) {
let upstreamProxyUrl;
if (hostname === 'activate-invalid-upstream-proxy-credentials') {
upstreamProxyUrl = `http://invalid:credentials@127.0.0.1:${upstreamProxyPort}`;
} else if (hostname === 'activate-invalid-upstream-proxy-host') {
upstreamProxyUrl = 'http://dummy-hostname:1234';
} else {
let auth = '';
if (upstreamProxyAuth) auth = `${upstreamProxyAuth.username}:${upstreamProxyAuth.password}@`;
upstreamProxyUrl = `http://${auth}127.0.0.1:${upstreamProxyPort}`;
}
result.upstreamProxyUrl = upstreamProxyUrl;
}
if (addToMainProxyServerConnectionIds) {
mainProxyServerConnectionIds.push(connectionId);
mainProxyServerConnections[connectionId] = {
groups: username ? username.replace('groups-', '').split('+') : [],
token: password,
hostname,
};
}
// Sometimes return a promise, sometimes the result directly
if (counter++ % 2 === 0) return result;
return Promise.resolve(result);
};
}
opts.authRealm = AUTH_REALM;
mainProxyServer = new Server(opts);
mainProxyServer.on('connectionClosed', ({ connectionId, stats }) => {
assert.include(mainProxyServer.getConnectionIds(), connectionId.toString());
mainProxyServerConnectionsClosed.push(connectionId);
const index = mainProxyServerConnectionIds.indexOf(connectionId);
mainProxyServerConnectionIds.splice(index, 1);
mainProxyServerConnectionId2Stats[connectionId] = stats;
});
return mainProxyServer.listen();
}
})
.then(() => {
// Generate URLs
baseUrl = `${useSsl ? 'https' : 'http'}://127.0.0.1:${targetServerPort}`;
if (useMainProxy) {
let auth = '';
if (mainProxyAuth) auth = `${mainProxyAuth.username}:${mainProxyAuth.password}@`;
mainProxyUrl = `http://${auth}127.0.0.1:${mainProxyServerPort}`;
}
});
});
// Helper functions
// Tests for 502 Bad gateway or 407 Proxy Authenticate
// Unfortunately the request library throws for HTTPS and sends status code for HTTP
const testForErrorResponse = (opts, expectedStatusCode) => {
let requestError = null;
let failedRequest = null;
const onRequestFailed = ({ error, request }) => {
requestError = error;
failedRequest = request;
};
mainProxyServer.on('requestFailed', onRequestFailed);
const promise = requestPromised(opts);
if (useSsl) {
return promise.then(() => {
assert.fail();
})
.catch((err) => {
// console.dir(err);
expect(err.message).to.contain(`${expectedStatusCode}`);
});
}
return promise.then((response) => {
expect(response.statusCode).to.eql(expectedStatusCode);
if (expectedStatusCode === 500) {
expect(requestError).to.have.own.property('message');
expect(failedRequest).to.have.own.property('url');
} else {
expect(requestError).to.eql(null);
expect(failedRequest).to.eql(null);
}
return response;
})
.finally(() => {
mainProxyServer.removeListener('requestFailed', onRequestFailed);
});
};
// Replacement for it() that checks whether the tests really called the main and upstream proxies
const _it = (description, func) => {
it(description, () => {
const upstreamCount = upstreamProxyRequestCount;
const mainCount = mainProxyServer ? mainProxyServer.stats.connectRequestCount + mainProxyServer.stats.httpRequestCount : null;
return func()
.then(() => {
if (useMainProxy) expect(mainCount).to.be.below(mainProxyServer.stats.connectRequestCount + mainProxyServer.stats.httpRequestCount);
if (useUpstreamProxy) expect(upstreamCount).to.be.below(upstreamProxyRequestCount);
});
});
};
['GET', 'POST', 'PUT', 'DELETE'].forEach((method) => {
_it(`handles simple ${method} request`, () => {
const opts = getRequestOpts('/hello-world');
opts.method = method;
return requestPromised(opts)
.then((response) => {
expect(response.body).to.eql('Hello world!');
expect(response.statusCode).to.eql(200);
});
});
});
['POST', 'PUT', 'PATCH'].forEach((method) => {
_it(`handles ${method} request with payload and passes Content-Type`, () => {
const opts = getRequestOpts('/echo-payload');
opts.method = method;
opts.body = 'SOME BODY LALALALA';
opts.headers['Content-Type'] = 'text/my-test';
return requestPromised(opts)
.then((response) => {
expect(response.body).to.eql(opts.body);
expect(response.headers['content-type']).to.eql(opts.headers['Content-Type']);
expect(response.statusCode).to.eql(200);
});
});
});
// NOTE: upstream proxy cannot handle non-standard headers
if (!useUpstreamProxy) {
_it('ignores non-standard server HTTP headers', () => {
const opts = getRequestOpts('/get-non-standard-headers');
opts.method = 'GET';
return requestPromised(opts)
.then((response) => {
expect(response.body).to.eql('Hello sir!');
expect(response.statusCode).to.eql(200);
expect(response.headers).to.be.an('object');
// The server returns three headers:
// 'Invalid Header With Space': 'HeaderValue1',
// 'X-Normal-Header': 'HeaderValue2',
// 'Invalid-Header-Value': 'some\value',
// With HTTP proxy, the invalid headers should be removed, otherwise they should be present
expect(response.headers['x-normal-header']).to.eql('HeaderValue2');
if (useMainProxy && !useSsl) {
expect(response.headers['invalid header with space']).to.eql(undefined);
expect(response.headers['invalid-header-value']).to.eql(undefined);
} else {
expect(response.headers['invalid header with space']).to.eql('HeaderValue1');
expect(response.headers['invalid-header-value']).to.eql('some\value');
}
});
});
if (!useSsl) {
_it('gracefully fails on invalid HTTP status code', () => {
const opts = getRequestOpts('/get-invalid-status-code');
opts.method = 'GET';
return requestPromised(opts)
.then((response) => {
if (useMainProxy) {
expect(response.statusCode).to.eql(500);
expect(response.body).to.match(/with an invalid HTTP status code/);
} else {
expect(response.statusCode).to.eql(55);
expect(response.body).to.eql('Bad status!');
}
});
});
}
}
_it('save repeating server HTTP headers', () => {
const opts = getRequestOpts('/get-repeating-headers');
opts.method = 'GET';
return requestPromised(opts)
.then((response) => {
expect(response.body).to.eql('Hooray!');
expect(response.statusCode).to.eql(200);
expect(response.headers).to.be.an('object');
// The server returns two headers with same names:
// ... 'Repeating-Header', 'HeaderValue1' ... 'Repeating-Header', 'HeaderValue2' ...
// All headers should be present
const firstIndex = response.rawHeaders.indexOf('Repeating-Header');
expect(response.rawHeaders[firstIndex + 1]).to.eql('HeaderValue1');
const secondIndex = response.rawHeaders.indexOf('Repeating-Header', firstIndex + 1);
expect(response.rawHeaders[secondIndex + 1]).to.eql('HeaderValue2');
});
});
_it('handles large streamed POST payload', () => {
const opts = getRequestOpts('/echo-payload');
opts.headers['Content-Type'] = 'text/my-test';
opts.method = 'POST';
let chunkIndex = 0;
let intervalId;
return new Promise((resolve, reject) => {
const passThrough = new stream.PassThrough();
opts.body = passThrough;
request(opts, (error, response, body) => {
if (error) return reject(error);
expect(response.statusCode).to.eql(200);
expect(body).to.eql(DATA_CHUNKS_COMBINED);
resolve();
});
intervalId = setInterval(() => {
if (chunkIndex >= DATA_CHUNKS.length) {
passThrough.end();
return;
}
passThrough.write(DATA_CHUNKS[chunkIndex++], (err) => {
if (err) reject(err);
});
}, 1);
})
.finally(() => {
clearInterval(intervalId);
});
});
const test1MAChars = () => {
const opts = getRequestOpts('/get-1m-a-chars-together');
opts.method = 'GET';
return requestPromised(opts)
.then((response) => {
expect(response.body).to.match(/^a{1000000}$/);
expect(response.statusCode).to.eql(200);
const expectedSize = 1000000; // "a" takes one byte, so one 1 milion "a" should be 1MB
// this condition is here because some tests do not use prepareRequestFunction
// and therefore are not trackable
if (mainProxyServerConnections && Object.keys(mainProxyServerConnections).length) {
const sortedIds = Object.keys(mainProxyServerConnections).sort((a, b) => {
if (Number(a) < Number(b)) return -1;
if (Number(a) > Number(b)) return 1;
return 0;
});
const lastConnectionId = sortedIds[sortedIds.length - 1];
const stats = mainProxyServer.getConnectionStats(lastConnectionId)
|| mainProxyServerConnectionId2Stats[lastConnectionId];
// 5% range because network negotiation adds to network trafic
expect(stats.srcTxBytes).to.be.within(expectedSize, expectedSize * 1.05);
expect(stats.trgRxBytes).to.be.within(expectedSize, expectedSize * 1.05);
}
});
};
_it('handles large GET response', test1MAChars);
// TODO: Test streamed GET
// _it('handles large streamed GET response', test1MAChars);
_it('handles 301 redirect', () => {
const opts = getRequestOpts('/redirect-to-hello-world');
return requestPromised(opts)
.then((response) => {
expect(response.body).to.eql('Hello world!');
expect(response.statusCode).to.eql(200);
});
});
_it('handles basic authentication', () => {
return Promise.resolve()
.then(() => {
// First test invalid credentials
const opts = getRequestOpts('/basic-auth');
opts.url = opts.url.replace('://', '://invalid:password@');
return requestPromised(opts);
})
.then((response) => {
expect(response.body).to.eql('Unauthorized');
expect(response.statusCode).to.eql(401);
})
.then(() => {
// Then test valid ones
const opts = getRequestOpts('/basic-auth');
opts.url = opts.url.replace('://', '://john.doe:Passwd@');
return requestPromised(opts);
})
.then((response) => {
expect(response.body).to.eql('OK');
expect(response.statusCode).to.eql(200);
});
});
// NOTE: PhantomJS cannot handle proxy auth with empty user or password, both need to be present!
if (!mainProxyAuth || (mainProxyAuth.username && mainProxyAuth.password)) {
_it('handles GET request from PhantomJS', () => {
return Promise.resolve()
.then(() => {
// NOTE: use other hostname than 'localhost' or '127.0.0.1' otherwise PhantomJS would skip the proxy!
const phantomUrl = `${useSsl ? 'https' : 'http'}://${LOCALHOST_TEST}:${targetServerPort}/hello-world`;
return phantomGet(phantomUrl, mainProxyUrl);
})
.then((response) => {
expect(response).to.contain('Hello world!');
});
});
}
const testWsCall = () => {
return new Promise((resolve, reject) => {
const wsUrl = `${useSsl ? 'wss' : 'ws'}://127.0.0.1:${targetServerPort}`;
const ws = new WebSocket.Client(wsUrl, [], {
proxy: {
origin: mainProxyUrl,
tls: useSsl ? { cert: sslCrt } : null,
}
});
ws.on('error', (err) => {
ws.close();
reject(err);
});
ws.on('open', () => {
ws.send('hello world');
});
ws.on('message', (event) => {
ws.close();
resolve(event.data);
});
})
.then((data) => {
expect(data).to.eql('I received: hello world');
});
};
_it('handles web socket connection', () => {
return testWsCall(false);
});
if (useMainProxy) {
_it('returns 404 for non-existent hostname', () => {
const opts = getRequestOpts(`http://${NON_EXISTENT_HOSTNAME}`);
return requestPromised(opts)
.then((response) => {
expect(response.statusCode).to.eql(404);
});
});
it('returns 400 for direct connection to main proxy', () => {
const opts = { url: `${mainProxyUrl}` };
return requestPromised(opts)
.then((response) => {
expect(response.statusCode).to.eql(400);
});
});
_it('removes hop-by-hop headers (HTTP-only) and leaves other ones', () => {
const opts = getRequestOpts('/echo-request-info');
opts.headers['X-Test-Header'] = 'my-test-value';
opts.headers['Transfer-Encoding'] = 'identity';
return requestPromised(opts)
.then((response) => {
expect(response.statusCode).to.eql(200);
expect(response.headers['content-type']).to.eql('application/json');
const req = JSON.parse(response.body);
expect(req.headers['x-test-header']).to.eql('my-test-value');
expect(req.headers['transfer-encoding']).to.eql(useSsl ? 'identity' : undefined);
});
});
if (mainProxyAuth) {
it('returns 407 for invalid credentials', () => {
return Promise.resolve()
.then(() => {
// Test no username and password
const opts = getRequestOpts('/whatever');
opts.proxy = `http://127.0.0.1:${mainProxyServerPort}`;
return testForErrorResponse(opts, 407);
})
.then(() => {
// Test good username and invalid password
const opts = getRequestOpts('/whatever');
opts.proxy = `http://${mainProxyAuth.username}:bad-password@127.0.0.1:${mainProxyServerPort}`;
return testForErrorResponse(opts, 407);
})
.then(() => {
// Test invalid username and good password
const opts = getRequestOpts('/whatever');
opts.proxy = `http://bad-username:${mainProxyAuth.password}@127.0.0.1:${mainProxyServerPort}`;
return testForErrorResponse(opts, 407);
})
.then(() => {
// Test invalid username and good password
const opts = getRequestOpts('/whatever');
opts.proxy = `http://bad-username:bad-password@127.0.0.1:${mainProxyServerPort}`;
return testForErrorResponse(opts, 407);
})
.then((response) => {
// Check we received our authRealm
if (!useSsl) {
expect(response.headers['proxy-authenticate']).to.eql(`Basic realm="${AUTH_REALM}"`);
}
});
});
it('returns 500 on error in prepareRequestFunction', () => {
return Promise.resolve()
.then(() => {
const opts = getRequestOpts(`${useSsl ? 'https' : 'http'}://activate-error-in-prep-req-func-throw`);
return testForErrorResponse(opts, 500);
})
.then(() => {
const opts = getRequestOpts(`${useSsl ? 'https' : 'http'}://activate-error-in-prep-req-func-promise`);
return testForErrorResponse(opts, 500);
})
.then(() => {
const opts = getRequestOpts(`${useSsl ? 'https' : 'http'}://activate-error-in-prep-req-func-throw-known`);
return testForErrorResponse(opts, 501);
})
.then(() => {
const opts = getRequestOpts(`${useSsl ? 'https' : 'http'}://activate-error-in-prep-req-func-promise-known`);
return testForErrorResponse(opts, 501);
});
});
}
if (useUpstreamProxy) {
it('fails gracefully on invalid upstream proxy URL', () => {
const opts = getRequestOpts(`${useSsl ? 'https' : 'http'}://activate-invalid-upstream-proxy-host`);
return testForErrorResponse(opts, 502);
});
if (upstreamProxyAuth) {
_it('fails gracefully on invalid upstream proxy credentials', () => {
const opts = getRequestOpts(`${useSsl ? 'https' : 'http'}://activate-invalid-upstream-proxy-credentials`);
return testForErrorResponse(opts, 502);
});
}
}
if (testCustomResponse) {
if (!useSsl) {
it('supports custom response - simple', () => {
const opts = getRequestOpts('http://test-custom-response-simple/some/path');
return requestPromised(opts)
.then((response) => {
expect(response.statusCode).to.eql(200);
expect(response.body).to.eql('TEST CUSTOM RESPONSE SIMPLE');
});
});
it('supports custom response - complex', () => {
const opts = getRequestOpts('http://test-custom-response-complex:1234/some/path?query=456');
return requestPromised(opts)
.then((response) => {
expect(response.statusCode).to.eql(201);
expect(response.headers).to.deep.include({
'my-test-header1': 'bla bla bla',
'my-test-header2': 'bla bla bla2',
});
expect(response.body).to.eql('TEST CUSTOM RESPONSE COMPLEX');
});
});
it('supports custom response - long', () => {
const opts = getRequestOpts('http://test-custom-response-long');
return requestPromised(opts)
.then((response) => {
expect(response.statusCode).to.eql(200);
expect(response.headers['content-length']).to.eql('5000000');
expect(response.body.length).to.eql(5000000);
expect(response.body).to.eql('X'.repeat(5000000));
});
});
it('supports custom response - promised', () => {
const opts = getRequestOpts('http://test-custom-response-promised/some/path');
return requestPromised(opts)
.then((response) => {
expect(response.statusCode).to.eql(200);
expect(response.body).to.eql('TEST CUSTOM RESPONSE PROMISED');
});
});
it('fails on invalid custom response function', () => {
const opts = getRequestOpts('http://test-custom-response-invalid');
return testForErrorResponse(opts, 500);
});
} else {
it('does not support custom response in SSL mode', () => {
const opts = getRequestOpts('https://test-custom-response-simple/some/path');
return testForErrorResponse(opts, 500);
});
}
}
}
after(function () {
this.timeout(3 * 1000);
return wait(1000)
.then(() => {
// Ensure all handlers are removed
if (mainProxyServer) {
expect(mainProxyServer.getConnectionIds()).to.be.deep.eql([]);
}
expect(mainProxyServerConnectionIds).to.be.deep.eql([]);
const closedSomeConnectionsTwice = mainProxyServerConnectionsClosed
.reduce((duplicateConnections, id, index) => {
if (index > 0 && mainProxyServerConnectionsClosed[index - 1] === id) {
duplicateConnections.push(id);
}
return duplicateConnections;
}, []);
expect(closedSomeConnectionsTwice).to.be.deep.eql([]);
if (mainProxyServerStatisticsInterval) clearInterval(mainProxyServerStatisticsInterval);
if (mainProxyServer) {
// NOTE: we need to forcibly close pending connections,
// because e.g. on 502 errors in HTTPS mode, the request library
// doesn't close the connection and this would timeout
return mainProxyServer.close(true);
}
})
.then(() => {
if (upstreamProxyServer) {
// NOTE: We used to wait for upstream proxy connections to close,
// but for HTTPS, in Node 10+, they linger for some reason...
// return Promise.promisify(upstreamProxyServer.close).bind(upstreamProxyServer)();
upstreamProxyServer.close();
}
})
.then(() => {
if (targetServer) {
return targetServer.close();
}
});
});
};
};
describe(`Test ${LOCALHOST_TEST} setup`, () => {
it('works', () => {
return Promise.promisify(dns.lookup).bind(dns)(LOCALHOST_TEST, { family: 4 })
.then((address) => {
// If this fails, see README.md !!!
expect(address).to.eql('127.0.0.1');
});
});
});
// Test direct connection to target server to ensure our tests are correct
describe('Server (HTTP -> Target)', createTestSuite({
useSsl: false,
useMainProxy: false,
}));
describe('Server (HTTPS -> Target)', createTestSuite({
useSsl: true,
useMainProxy: false,
}));
// Run all combinations of test parameters
const useSslVariants = [
false,
true,
];
const mainProxyAuthVariants = [
null,
{ username: 'user1', password: 'pass1' },
{ username: 'user2', password: '' },
{ username: '', password: 'pass3' },
];
const useUpstreamProxyVariants = [
true,
false,
];
const upstreamProxyAuthVariants = [
null,
{ type: 'Basic', username: 'userA', password: '' },
{ type: 'Basic', username: 'userB', password: 'passA' },
];
useSslVariants.forEach((useSsl) => {
mainProxyAuthVariants.forEach((mainProxyAuth) => {
const baseDesc = `Server (${useSsl ? 'HTTPS' : 'HTTP'} -> Main proxy`;
// Test custom response separately (it doesn't use upstream proxies)
describe(`${baseDesc} -> Target + custom responses)`, createTestSuite({
useMainProxy: true,
useSsl,
mainProxyAuth,
testCustomResponse: true,
}));
useUpstreamProxyVariants.forEach((useUpstreamProxy) => {
// If useUpstreamProxy is not used, only try one variant of upstreamProxyAuth
let variants = upstreamProxyAuthVariants;
if (!useUpstreamProxy) variants = [null];
variants.forEach((upstreamProxyAuth) => {
let desc = `${baseDesc} `;
if (mainProxyAuth) {
if (!mainProxyAuth) {
desc += 'public ';
} else if (mainProxyAuth.username && mainProxyAuth.password) desc += 'with username:password ';
else desc += 'with username only ';
}
if (useUpstreamProxy) {
desc += '-> Upstream proxy ';
if (!upstreamProxyAuth) {
desc += 'public ';
} else if (upstreamProxyAuth.username && upstreamProxyAuth.password) desc += 'with username:password ';
else desc += 'with username only ';
}
desc += '-> Target)';
describe(desc, createTestSuite({
useMainProxy: true,
useSsl,
useUpstreamProxy,
mainProxyAuth,
upstreamProxyAuth,
}));
});
});
});
});