http-proxy-middleware 0.17.0 → 0.17.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +21 -6
  3. package/lib/handlers.js +13 -1
  4. package/lib/index.js +33 -10
  5. package/lib/logger.js +2 -2
  6. package/package.json +16 -12
  7. package/.editorconfig +0 -18
  8. package/.jscsrc +0 -8
  9. package/.npmignore +0 -28
  10. package/.travis.yml +0 -16
  11. package/CONTRIBUTING.md +0 -61
  12. package/ISSUE_TEMPLATE.md +0 -21
  13. package/examples/README.md +0 -33
  14. package/examples/browser-sync/index.js +0 -29
  15. package/examples/connect/index.js +0 -29
  16. package/examples/express/index.js +0 -28
  17. package/examples/websocket/index.html +0 -96
  18. package/examples/websocket/index.js +0 -41
  19. package/recipes/README.md +0 -110
  20. package/recipes/basic.md +0 -32
  21. package/recipes/context-matching.md +0 -99
  22. package/recipes/corporate-proxy.md +0 -21
  23. package/recipes/delay.md +0 -37
  24. package/recipes/logLevel.md +0 -40
  25. package/recipes/logProvider.md +0 -77
  26. package/recipes/modify-post.md +0 -74
  27. package/recipes/pathRewrite.md +0 -93
  28. package/recipes/proxy-events.md +0 -74
  29. package/recipes/router.md +0 -83
  30. package/recipes/servers.md +0 -252
  31. package/recipes/shorthand.md +0 -63
  32. package/recipes/virtual-hosts.md +0 -18
  33. package/recipes/websocket.md +0 -41
  34. package/test/e2e/_utils.js +0 -21
  35. package/test/e2e/http-proxy-middleware.spec.js +0 -672
  36. package/test/e2e/path-rewriter.spec.js +0 -98
  37. package/test/e2e/router.spec.js +0 -128
  38. package/test/e2e/websocket.spec.js +0 -150
  39. package/test/unit/_libs.js +0 -8
  40. package/test/unit/config-factory.spec.js +0 -154
  41. package/test/unit/context-matcher.spec.js +0 -250
  42. package/test/unit/handlers.spec.js +0 -126
  43. package/test/unit/logger.spec.js +0 -259
  44. package/test/unit/path-rewriter.spec.js +0 -143
  45. package/test/unit/router.spec.js +0 -136
@@ -1,672 +0,0 @@
1
- var utils = require('./_utils');
2
- var expect = require('chai').expect;
3
- var http = require('http');
4
-
5
- describe('E2E http-proxy-middleware', function() {
6
- var createServer;
7
- var proxyMiddleware;
8
-
9
- beforeEach(function() {
10
- createServer = utils.createServer;
11
- proxyMiddleware = utils.proxyMiddleware;
12
- });
13
-
14
- describe('http-proxy-middleware creation', function() {
15
- it('should create a middleware', function() {
16
- var middleware;
17
- middleware = proxyMiddleware('/api', {target: 'http://localhost:8000'});
18
- expect(middleware).to.be.a('function');
19
- });
20
- });
21
-
22
- describe('context matching', function() {
23
- describe('do not proxy', function() {
24
- var isSkipped;
25
-
26
- beforeEach(function() {
27
- isSkipped = false;
28
-
29
- var middleware;
30
-
31
- var mockReq = {url: '/foo/bar', originalUrl: '/foo/bar'};
32
- var mockRes = {};
33
- var mockNext = function() {
34
- // mockNext will be called when request is not proxied
35
- isSkipped = true;
36
- };
37
-
38
- middleware = proxyMiddleware('/api', {target: 'http://localhost:8000'});
39
- middleware(mockReq, mockRes, mockNext);
40
- });
41
-
42
- it('should not proxy requests when request url does not match context' , function() {
43
- expect(isSkipped).to.be.true;
44
- });
45
-
46
- });
47
- });
48
-
49
- describe('http-proxy-middleware in actual server', function() {
50
-
51
- describe('basic setup, requests to target', function() {
52
- var proxyServer, targetServer;
53
- var targetHeaders;
54
- var targetUrl;
55
- var responseBody;
56
-
57
- beforeEach(function(done) {
58
- var mw_proxy = proxyMiddleware('/api', {target: 'http://localhost:8000'});
59
-
60
- var mw_target = function(req, res, next) {
61
- targetUrl = req.url; // store target url.
62
- targetHeaders = req.headers; // store target headers.
63
- res.write('HELLO WEB'); // respond with 'HELLO WEB'
64
- res.end();
65
- };
66
-
67
- proxyServer = createServer(3000, mw_proxy);
68
- targetServer = createServer(8000, mw_target);
69
-
70
- http.get('http://localhost:3000/api/b/c/d;p?q=1&r=[2,3]#s"', function(res) {
71
- res.on('data', function(chunk) {
72
- responseBody = chunk.toString();
73
- done();
74
- });
75
- });
76
- });
77
-
78
- afterEach(function() {
79
- proxyServer.close();
80
- targetServer.close();
81
- });
82
-
83
- it('should have the same headers.host value', function() {
84
- expect(targetHeaders.host).to.equal('localhost:3000');
85
- });
86
-
87
- it('should have proxied the uri-path and uri-query, but not the uri-hash', function() {
88
- expect(targetUrl).to.equal('/api/b/c/d;p?q=1&r=[2,3]');
89
- });
90
-
91
- it('should have response body: "HELLO WEB"', function() {
92
- expect(responseBody).to.equal('HELLO WEB');
93
- });
94
- });
95
-
96
- describe('custom context matcher/filter', function() {
97
- var proxyServer, targetServer;
98
- var targetHeaders;
99
- var targetUrl;
100
- var responseBody;
101
-
102
- var filterPath, filterReq;
103
-
104
- beforeEach(function(done) {
105
- var filter = function(path, req) {
106
- filterPath = path;
107
- filterReq = req;
108
- return true;
109
- };
110
-
111
- var mw_proxy = proxyMiddleware(filter, {target: 'http://localhost:8000'});
112
-
113
- var mw_target = function(req, res, next) {
114
- targetUrl = req.url; // store target url.
115
- targetHeaders = req.headers; // store target headers.
116
- res.write('HELLO WEB'); // respond with 'HELLO WEB'
117
- res.end();
118
- };
119
-
120
- proxyServer = createServer(3000, mw_proxy);
121
- targetServer = createServer(8000, mw_target);
122
-
123
- http.get('http://localhost:3000/api/b/c/d', function(res) {
124
- res.on('data', function(chunk) {
125
- responseBody = chunk.toString();
126
- done();
127
- });
128
- });
129
- });
130
-
131
- afterEach(function() {
132
- proxyServer.close();
133
- targetServer.close();
134
- });
135
-
136
- it('should have response body: "HELLO WEB"', function() {
137
- expect(responseBody).to.equal('HELLO WEB');
138
- });
139
-
140
- it('should provide the url path in the first argument', function() {
141
- expect(filterPath).to.equal('/api/b/c/d');
142
- });
143
-
144
- it('should provide the req object in the second argument', function() {
145
- expect(filterReq.method).to.equal('GET');
146
- });
147
- });
148
-
149
- describe('multi path', function() {
150
- var proxyServer, targetServer;
151
- var targetHeaders;
152
- var response, responseBody;
153
-
154
- beforeEach(function() {
155
- var mw_proxy = proxyMiddleware(['/api', '/ajax'], {target: 'http://localhost:8000'});
156
-
157
- var mw_target = function(req, res, next) {
158
- res.write(req.url); // respond with req.url
159
- res.end();
160
- };
161
-
162
- proxyServer = createServer(3000, mw_proxy);
163
- targetServer = createServer(8000, mw_target);
164
- });
165
-
166
- afterEach(function() {
167
- proxyServer.close();
168
- targetServer.close();
169
- });
170
-
171
- describe('request to path A, configured', function() {
172
- beforeEach(function(done) {
173
- http.get('http://localhost:3000/api/some/endpoint', function(res) {
174
- response = res;
175
- res.on('data', function(chunk) {
176
- responseBody = chunk.toString();
177
- done();
178
- });
179
- });
180
- });
181
-
182
- it('should proxy to path A', function() {
183
- expect(response.statusCode).to.equal(200);
184
- expect(responseBody).to.equal('/api/some/endpoint');
185
- });
186
- });
187
-
188
- describe('request to path B, configured', function() {
189
- beforeEach(function(done) {
190
- http.get('http://localhost:3000/ajax/some/library', function(res) {
191
- response = res;
192
- res.on('data', function(chunk) {
193
- responseBody = chunk.toString();
194
- done();
195
- });
196
- });
197
- });
198
-
199
- it('should proxy to path B', function() {
200
- expect(response.statusCode).to.equal(200);
201
- expect(responseBody).to.equal('/ajax/some/library');
202
- });
203
- });
204
-
205
- describe('request to path C, not configured', function() {
206
- beforeEach(function(done) {
207
- http.get('http://localhost:3000/lorum/ipsum', function(res) {
208
- response = res;
209
- res.on('data', function(chunk) {
210
- responseBody = chunk.toString();
211
- done();
212
- });
213
- });
214
- });
215
-
216
- it('should not proxy to this path', function() {
217
- expect(response.statusCode).to.equal(404);
218
- });
219
- });
220
-
221
- });
222
-
223
- describe('wildcard path matching', function() {
224
- var proxyServer, targetServer;
225
- var targetHeaders;
226
- var response, responseBody;
227
-
228
- beforeEach(function() {
229
- var mw_proxy = proxyMiddleware('/api/**', {target: 'http://localhost:8000'});
230
-
231
- var mw_target = function(req, res, next) {
232
- res.write(req.url); // respond with req.url
233
- res.end();
234
- };
235
-
236
- proxyServer = createServer(3000, mw_proxy);
237
- targetServer = createServer(8000, mw_target);
238
- });
239
-
240
- beforeEach(function(done) {
241
- http.get('http://localhost:3000/api/some/endpoint', function(res) {
242
- response = res;
243
- res.on('data', function(chunk) {
244
- responseBody = chunk.toString();
245
- done();
246
- });
247
- });
248
- });
249
-
250
- afterEach(function() {
251
- proxyServer.close();
252
- targetServer.close();
253
- });
254
-
255
- it('should proxy to path', function() {
256
- expect(response.statusCode).to.equal(200);
257
- expect(responseBody).to.equal('/api/some/endpoint');
258
- });
259
- });
260
-
261
- describe('multi glob wildcard path matching', function() {
262
- var proxyServer, targetServer;
263
- var targetHeaders;
264
- var responseA, responseBodyA;
265
- var responseB, responseBodyB;
266
-
267
- beforeEach(function() {
268
- var mw_proxy = proxyMiddleware(['/**.html', '!**.json'], {target: 'http://localhost:8000'});
269
-
270
- var mw_target = function(req, res, next) {
271
- res.write(req.url); // respond with req.url
272
- res.end();
273
- };
274
-
275
- proxyServer = createServer(3000, mw_proxy);
276
- targetServer = createServer(8000, mw_target);
277
- });
278
-
279
- beforeEach(function(done) {
280
- http.get('http://localhost:3000/api/some/endpoint/index.html', function(res) {
281
- responseA = res;
282
- res.on('data', function(chunk) {
283
- responseBodyA = chunk.toString();
284
- done();
285
- });
286
- });
287
- });
288
-
289
- beforeEach(function(done) {
290
- http.get('http://localhost:3000/api/some/endpoint/data.json', function(res) {
291
- responseB = res;
292
- res.on('data', function(chunk) {
293
- responseBodyB = chunk.toString();
294
- done();
295
- });
296
- });
297
- });
298
-
299
- afterEach(function() {
300
- proxyServer.close();
301
- targetServer.close();
302
- });
303
-
304
- it('should proxy to paths ending with *.html', function() {
305
- expect(responseA.statusCode).to.equal(200);
306
- expect(responseBodyA).to.equal('/api/some/endpoint/index.html');
307
- });
308
-
309
- it('should not proxy to paths ending with *.json', function() {
310
- expect(responseB.statusCode).to.equal(404);
311
- });
312
- });
313
-
314
- describe('option.headers - additional request headers', function() {
315
- var proxyServer, targetServer;
316
- var targetHeaders;
317
-
318
- beforeEach(function(done) {
319
- var mw_proxy = proxyMiddleware('/api', {target: 'http://localhost:8000', headers: {host: 'foobar.dev'}});
320
-
321
- var mw_target = function(req, res, next) {
322
- targetHeaders = req.headers;
323
- res.end();
324
- };
325
-
326
- proxyServer = createServer(3000, mw_proxy);
327
- targetServer = createServer(8000, mw_target);
328
-
329
- http.get('http://localhost:3000/api/', function(res) {
330
- done();
331
- });
332
- });
333
-
334
- afterEach(function() {
335
- proxyServer.close();
336
- targetServer.close();
337
- });
338
-
339
- it('should send request header "host" to target server', function() {
340
- expect(targetHeaders.host).to.equal('foobar.dev');
341
- });
342
- });
343
-
344
- describe('legacy option.proxyHost', function() {
345
- var proxyServer, targetServer;
346
- var targetHeaders;
347
-
348
- beforeEach(function(done) {
349
- var mw_proxy = proxyMiddleware('/api', {target: 'http://localhost:8000', proxyHost: 'foobar.dev'});
350
-
351
- var mw_target = function(req, res, next) {
352
- targetHeaders = req.headers;
353
- res.end();
354
- };
355
-
356
- proxyServer = createServer(3000, mw_proxy);
357
- targetServer = createServer(8000, mw_target);
358
-
359
- http.get('http://localhost:3000/api/', function(res) {
360
- done();
361
- });
362
- });
363
-
364
- afterEach(function() {
365
- proxyServer.close();
366
- targetServer.close();
367
- });
368
-
369
- it('should proxy host header to target server', function() {
370
- expect(targetHeaders.host).to.equal('foobar.dev');
371
- });
372
- });
373
-
374
- describe('option.onError - Error handling', function() {
375
- var proxyServer, targetServer;
376
- var response, responseBody;
377
-
378
- describe('default', function() {
379
- beforeEach(function(done) {
380
- var mw_proxy = proxyMiddleware('/api', {target: 'http://localhost:666'}); // unreachable host on port:666
381
- var mw_target = function(req, res, next) {next();};
382
-
383
- proxyServer = createServer(3000, mw_proxy);
384
- targetServer = createServer(8000, mw_target);
385
-
386
- http.get('http://localhost:3000/api/', function(res) {
387
- response = res;
388
- done();
389
- });
390
- });
391
-
392
- afterEach(function() {
393
- proxyServer.close();
394
- targetServer.close();
395
- });
396
-
397
- it('should handle errors when host is not reachable', function() {
398
- expect(response.statusCode).to.equal(500);
399
- });
400
- });
401
-
402
- describe('custom', function() {
403
- beforeEach(function(done) {
404
- var customOnError = function(err, req, res) {
405
- res.writeHead(418); // different error code
406
- res.end('I\'m a teapot'); // no response body
407
- };
408
-
409
- var mw_proxy = proxyMiddleware('/api', {target: 'http://localhost:666', onError: customOnError}); // unreachable host on port:666
410
- var mw_target = function(req, res, next) {next();};
411
-
412
- proxyServer = createServer(3000, mw_proxy);
413
- targetServer = createServer(8000, mw_target);
414
-
415
- http.get('http://localhost:3000/api/', function(res) {
416
- response = res;
417
- res.on('data', function(chunk) {
418
- responseBody = chunk.toString();
419
- done();
420
- });
421
- });
422
- });
423
-
424
- afterEach(function() {
425
- proxyServer.close();
426
- targetServer.close();
427
- });
428
-
429
- it('should respond with custom http status code', function() {
430
- expect(response.statusCode).to.equal(418);
431
- });
432
-
433
- it('should respond with custom status message', function() {
434
- expect(responseBody).to.equal('I\'m a teapot');
435
- });
436
- });
437
- });
438
-
439
- describe('option.onProxyRes', function() {
440
- var proxyServer, targetServer;
441
- var response, responseBody;
442
-
443
- beforeEach(function(done) {
444
- var fnOnProxyRes = function(proxyRes, req, res) {
445
- proxyRes.headers['x-added'] = 'foobar'; // add custom header to response
446
- delete proxyRes.headers['x-removed'];
447
- };
448
-
449
- var mw_proxy = proxyMiddleware('/api', {
450
- target: 'http://localhost:8000',
451
- onProxyRes: fnOnProxyRes
452
- });
453
- var mw_target = function(req, res, next) {
454
- res.setHeader('x-removed', 'remove-header');
455
- res.write(req.url); // respond with req.url
456
- res.end();
457
- };
458
-
459
- proxyServer = createServer(3000, mw_proxy);
460
- targetServer = createServer(8000, mw_target);
461
-
462
- http.get('http://localhost:3000/api/foo/bar', function(res) {
463
- response = res;
464
- res.on('data', function(chunk) {
465
- responseBody = chunk.toString();
466
- done();
467
- });
468
- });
469
- });
470
-
471
- afterEach(function() {
472
- proxyServer.close();
473
- targetServer.close();
474
- });
475
-
476
- it('should add `x-added` as custom header to response"', function() {
477
- expect(response.headers['x-added']).to.equal('foobar');
478
- });
479
-
480
- it('should remove `x-removed` field from response header"', function() {
481
- expect(response.headers['x-removed']).to.equal(undefined);
482
- });
483
- });
484
-
485
- describe('option.onProxyReq', function() {
486
- var proxyServer, targetServer;
487
- var receivedRequest;
488
-
489
- beforeEach(function(done) {
490
- var fnOnProxyReq = function(proxyReq, req, res) {
491
- proxyReq.setHeader('x-added', 'foobar'); // add custom header to request
492
- };
493
-
494
- var mw_proxy = proxyMiddleware('/api', {
495
- target: 'http://localhost:8000',
496
- onProxyReq: fnOnProxyReq
497
- });
498
-
499
- var mw_target = function(req, res, next) {
500
- receivedRequest = req;
501
- res.write(req.url); // respond with req.url
502
- res.end();
503
- };
504
-
505
- proxyServer = createServer(3000, mw_proxy);
506
- targetServer = createServer(8000, mw_target);
507
-
508
- http.get('http://localhost:3000/api/foo/bar', function() {
509
- done();
510
- });
511
- });
512
-
513
- afterEach(function() {
514
- proxyServer.close();
515
- targetServer.close();
516
- });
517
-
518
- it('should add `x-added` as custom header to request"', function() {
519
- expect(receivedRequest.headers['x-added']).to.equal('foobar');
520
- });
521
- });
522
-
523
- describe('option.pathRewrite', function() {
524
- var proxyServer, targetServer;
525
- var responseBody;
526
-
527
- beforeEach(function(done) {
528
- var mw_proxy = proxyMiddleware('/api', {
529
- target: 'http://localhost:8000',
530
- pathRewrite: {
531
- '^/api': '/rest',
532
- '^/remove': ''
533
- }
534
- });
535
- var mw_target = function(req, res, next) {
536
- res.write(req.url); // respond with req.url
537
- res.end();
538
- };
539
-
540
- proxyServer = createServer(3000, mw_proxy);
541
- targetServer = createServer(8000, mw_target);
542
-
543
- http.get('http://localhost:3000/api/foo/bar', function(res) {
544
- res.on('data', function(chunk) {
545
- responseBody = chunk.toString();
546
- done();
547
- });
548
- });
549
- });
550
-
551
- afterEach(function() {
552
- proxyServer.close();
553
- targetServer.close();
554
- });
555
-
556
- it('should have rewritten path from "/api/foo/bar" to "/rest/foo/bar"', function() {
557
- expect(responseBody).to.equal('/rest/foo/bar');
558
- });
559
- });
560
-
561
- describe('shorthand usage', function() {
562
- var proxyServer, targetServer;
563
- var responseBody;
564
-
565
- beforeEach(function(done) {
566
- var mw_proxy = proxyMiddleware('http://localhost:8000/api');
567
- var mw_target = function(req, res, next) {
568
- res.write(req.url); // respond with req.url
569
- res.end();
570
- };
571
-
572
- proxyServer = createServer(3000, mw_proxy);
573
- targetServer = createServer(8000, mw_target);
574
-
575
- http.get('http://localhost:3000/api/foo/bar', function(res) {
576
- res.on('data', function(chunk) {
577
- responseBody = chunk.toString();
578
- done();
579
- });
580
- });
581
- });
582
-
583
- afterEach(function() {
584
- proxyServer.close();
585
- targetServer.close();
586
- });
587
-
588
- it('should have proxy with shorthand configuration', function() {
589
- expect(responseBody).to.equal('/api/foo/bar');
590
- });
591
- });
592
-
593
- describe('express with path + proxy', function() {
594
- var proxyServer, targetServer;
595
- var responseBody;
596
-
597
- beforeEach(function(done) {
598
- var mw_proxy = proxyMiddleware('http://localhost:8000');
599
- var mw_target = function(req, res, next) {
600
- res.write(req.url); // respond with req.url
601
- res.end();
602
- };
603
-
604
- proxyServer = createServer(3000, mw_proxy, '/api');
605
- targetServer = createServer(8000, mw_target);
606
-
607
- http.get('http://localhost:3000/api/foo/bar', function(res) {
608
- res.on('data', function(chunk) {
609
- responseBody = chunk.toString();
610
- done();
611
- });
612
- });
613
- });
614
-
615
- afterEach(function() {
616
- proxyServer.close();
617
- targetServer.close();
618
- });
619
-
620
- it('should proxy to target with the baseUrl', function() {
621
- expect(responseBody).to.equal('/api/foo/bar');
622
- });
623
-
624
- });
625
-
626
- describe('option.logLevel & option.logProvider', function() {
627
- var proxyServer, targetServer;
628
- var responseBody;
629
- var logMessage;
630
-
631
- beforeEach(function(done) {
632
- var customLogger = function(message) {
633
- logMessage = message;
634
- };
635
-
636
- var mw_proxy = proxyMiddleware('http://localhost:8000/api', {
637
- logLevel: 'info',
638
- logProvider: function(provider) {
639
- provider.debug = customLogger;
640
- provider.info = customLogger;
641
- return provider;
642
- }
643
- });
644
- var mw_target = function(req, res, next) {
645
- res.write(req.url); // respond with req.url
646
- res.end();
647
- };
648
-
649
- proxyServer = createServer(3000, mw_proxy);
650
- targetServer = createServer(8000, mw_target);
651
-
652
- http.get('http://localhost:3000/api/foo/bar', function(res) {
653
- res.on('data', function(chunk) {
654
- responseBody = chunk.toString();
655
- done();
656
- });
657
- });
658
- });
659
-
660
- afterEach(function() {
661
- proxyServer.close();
662
- targetServer.close();
663
- });
664
-
665
- it('should have logged messages', function() {
666
- expect(logMessage).not.to.equal(undefined);
667
- });
668
- });
669
-
670
- });
671
- });
672
-