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.
- package/CHANGELOG.md +12 -0
- package/README.md +21 -6
- package/lib/handlers.js +13 -1
- package/lib/index.js +33 -10
- package/lib/logger.js +2 -2
- package/package.json +16 -12
- package/.editorconfig +0 -18
- package/.jscsrc +0 -8
- package/.npmignore +0 -28
- package/.travis.yml +0 -16
- package/CONTRIBUTING.md +0 -61
- package/ISSUE_TEMPLATE.md +0 -21
- package/examples/README.md +0 -33
- package/examples/browser-sync/index.js +0 -29
- package/examples/connect/index.js +0 -29
- package/examples/express/index.js +0 -28
- package/examples/websocket/index.html +0 -96
- package/examples/websocket/index.js +0 -41
- package/recipes/README.md +0 -110
- package/recipes/basic.md +0 -32
- package/recipes/context-matching.md +0 -99
- package/recipes/corporate-proxy.md +0 -21
- package/recipes/delay.md +0 -37
- package/recipes/logLevel.md +0 -40
- package/recipes/logProvider.md +0 -77
- package/recipes/modify-post.md +0 -74
- package/recipes/pathRewrite.md +0 -93
- package/recipes/proxy-events.md +0 -74
- package/recipes/router.md +0 -83
- package/recipes/servers.md +0 -252
- package/recipes/shorthand.md +0 -63
- package/recipes/virtual-hosts.md +0 -18
- package/recipes/websocket.md +0 -41
- package/test/e2e/_utils.js +0 -21
- package/test/e2e/http-proxy-middleware.spec.js +0 -672
- package/test/e2e/path-rewriter.spec.js +0 -98
- package/test/e2e/router.spec.js +0 -128
- package/test/e2e/websocket.spec.js +0 -150
- package/test/unit/_libs.js +0 -8
- package/test/unit/config-factory.spec.js +0 -154
- package/test/unit/context-matcher.spec.js +0 -250
- package/test/unit/handlers.spec.js +0 -126
- package/test/unit/logger.spec.js +0 -259
- package/test/unit/path-rewriter.spec.js +0 -143
- package/test/unit/router.spec.js +0 -136
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
var expect = require('chai').expect;
|
|
2
|
-
var pathRewriter = require('./_libs').pathRewriter;
|
|
3
|
-
|
|
4
|
-
describe('Path rewriting', function() {
|
|
5
|
-
var rewriter;
|
|
6
|
-
var result;
|
|
7
|
-
var config;
|
|
8
|
-
|
|
9
|
-
describe('Rewrite rules configuration and usage', function() {
|
|
10
|
-
|
|
11
|
-
beforeEach(function() {
|
|
12
|
-
config = {
|
|
13
|
-
'^/api/old': '/api/new',
|
|
14
|
-
'^/remove': '',
|
|
15
|
-
'invalid': 'path/new',
|
|
16
|
-
'/valid': '/path/new',
|
|
17
|
-
'/some/specific/path': '/awe/some/specific/path',
|
|
18
|
-
'/some': '/awe/some'
|
|
19
|
-
};
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
beforeEach(function() {
|
|
23
|
-
rewriter = pathRewriter.create(config);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it('should rewrite path', function() {
|
|
27
|
-
result = rewriter('/api/old/index.json');
|
|
28
|
-
expect(result).to.equal('/api/new/index.json');
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it('should remove path', function() {
|
|
32
|
-
result = rewriter('/remove/old/index.json');
|
|
33
|
-
expect(result).to.equal('/old/index.json');
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('should leave path intact', function() {
|
|
37
|
-
result = rewriter('/foo/bar/index.json');
|
|
38
|
-
expect(result).to.equal('/foo/bar/index.json');
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('should not rewrite path when config-key does not match url with test(regex)', function() {
|
|
42
|
-
result = rewriter('/invalid/bar/foo.json');
|
|
43
|
-
expect(result).to.equal('/path/new/bar/foo.json');
|
|
44
|
-
expect(result).to.not.equal('/invalid/new/bar/foo.json');
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('should rewrite path when config-key does match url with test(regex)', function() {
|
|
48
|
-
result = rewriter('/valid/foo/bar.json');
|
|
49
|
-
expect(result).to.equal('/path/new/foo/bar.json');
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it('should return first match when similar paths are configured', function() {
|
|
53
|
-
result = rewriter('/some/specific/path/bar.json');
|
|
54
|
-
expect(result).to.equal('/awe/some/specific/path/bar.json');
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
describe('Rewrite rule: add base path to requests', function() {
|
|
59
|
-
|
|
60
|
-
beforeEach(function() {
|
|
61
|
-
config = {
|
|
62
|
-
'^/': '/extra/base/path/'
|
|
63
|
-
};
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
beforeEach(function() {
|
|
67
|
-
rewriter = pathRewriter.create(config);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('should add base path to requests', function() {
|
|
71
|
-
result = rewriter('/api/books/123');
|
|
72
|
-
expect(result).to.equal('/extra/base/path/api/books/123');
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
describe('Rewrite function', function() {
|
|
77
|
-
var rewriter;
|
|
78
|
-
|
|
79
|
-
beforeEach(function() {
|
|
80
|
-
rewriter = function(fn) {
|
|
81
|
-
var rewriteFn = pathRewriter.create(fn);
|
|
82
|
-
var requestPath = '/123/456';
|
|
83
|
-
return rewriteFn(requestPath);
|
|
84
|
-
};
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it('should return unmodified path', function() {
|
|
88
|
-
var rewriteFn = function(path) {
|
|
89
|
-
return path;
|
|
90
|
-
};
|
|
91
|
-
expect(rewriter(rewriteFn)).to.equal('/123/456');
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('should return alternative path', function() {
|
|
95
|
-
var rewriteFn = function(path) {
|
|
96
|
-
return '/foo/bar';
|
|
97
|
-
};
|
|
98
|
-
expect(rewriter(rewriteFn)).to.equal('/foo/bar');
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it('should return replaced path', function() {
|
|
102
|
-
var rewriteFn = function(path) {
|
|
103
|
-
return path.replace('/456', '/789');
|
|
104
|
-
};
|
|
105
|
-
expect(rewriter(rewriteFn)).to.equal('/123/789');
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
describe('Invalid configuration', function() {
|
|
110
|
-
var badFn;
|
|
111
|
-
|
|
112
|
-
beforeEach(function() {
|
|
113
|
-
badFn = function(config) {
|
|
114
|
-
return function() {
|
|
115
|
-
pathRewriter.create(config);
|
|
116
|
-
};
|
|
117
|
-
};
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it('should return undefined when no config is provided', function() {
|
|
121
|
-
expect((badFn())()).to.equal(undefined);
|
|
122
|
-
expect((badFn(null)())).to.equal(undefined);
|
|
123
|
-
expect((badFn(undefined)())).to.equal(undefined);
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
it('should throw when bad config is provided', function() {
|
|
127
|
-
expect(badFn(123)).to.throw(Error);
|
|
128
|
-
expect(badFn('abc')).to.throw(Error);
|
|
129
|
-
expect(badFn([])).to.throw(Error);
|
|
130
|
-
expect(badFn([1,2,3])).to.throw(Error);
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
it('should not throw when empty Object config is provided', function() {
|
|
134
|
-
expect(badFn({})).to.not.throw(Error);
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
it('should not throw when function config is provided', function() {
|
|
138
|
-
expect(badFn(function() {})).to.not.throw(Error);
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
});
|
|
142
|
-
});
|
|
143
|
-
|
package/test/unit/router.spec.js
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
var expect = require('chai').expect;
|
|
2
|
-
var router = require('./_libs').router;
|
|
3
|
-
|
|
4
|
-
describe('router unit test', function() {
|
|
5
|
-
var req, config, result;
|
|
6
|
-
|
|
7
|
-
beforeEach(function() {
|
|
8
|
-
req = {
|
|
9
|
-
headers: {
|
|
10
|
-
host: 'localhost'
|
|
11
|
-
},
|
|
12
|
-
url: '/'
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
config = {
|
|
16
|
-
target: 'http://localhost:6000'
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
describe('router.getTarget from function', function() {
|
|
22
|
-
var request;
|
|
23
|
-
|
|
24
|
-
beforeEach(function() {
|
|
25
|
-
proxyOptionWithRouter = {
|
|
26
|
-
target: 'http://localhost:6000',
|
|
27
|
-
router: function(req) {
|
|
28
|
-
request = req;
|
|
29
|
-
return 'http://foobar.com:666';
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
result = router.getTarget(req, proxyOptionWithRouter);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
describe('custom dynamic router function', function() {
|
|
37
|
-
it('should provide the request object for dynamic routing', function() {
|
|
38
|
-
expect(request.headers.host).to.equal('localhost');
|
|
39
|
-
expect(request.url).to.equal('/');
|
|
40
|
-
});
|
|
41
|
-
it('should return new target', function() {
|
|
42
|
-
expect(result).to.equal('http://foobar.com:666');
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
describe('router.getTarget from table', function() {
|
|
48
|
-
beforeEach(function() {
|
|
49
|
-
proxyOptionWithRouter = {
|
|
50
|
-
target: 'http://localhost:6000',
|
|
51
|
-
router: {
|
|
52
|
-
'alpha.localhost': 'http://localhost:6001',
|
|
53
|
-
'beta.localhost': 'http://localhost:6002',
|
|
54
|
-
'gamma.localhost/api': 'http://localhost:6003',
|
|
55
|
-
'gamma.localhost': 'http://localhost:6004',
|
|
56
|
-
'/rest': 'http://localhost:6005',
|
|
57
|
-
'/some/specific/path': 'http://localhost:6006',
|
|
58
|
-
'/some': 'http://localhost:6007'
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
describe('without router config', function() {
|
|
64
|
-
it('should return the normal target when router not present in config', function() {
|
|
65
|
-
result = router.getTarget(req, config);
|
|
66
|
-
expect(result).to.equal(undefined);
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
describe('with just the host in router config', function() {
|
|
71
|
-
it('should target http://localhost:6001 when for router:"alpha.localhost"', function() {
|
|
72
|
-
req.headers.host = 'alpha.localhost';
|
|
73
|
-
result = router.getTarget(req, proxyOptionWithRouter);
|
|
74
|
-
expect(result).to.equal('http://localhost:6001');
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('should target http://localhost:6002 when for router:"beta.localhost"', function() {
|
|
78
|
-
req.headers.host = 'beta.localhost';
|
|
79
|
-
result = router.getTarget(req, proxyOptionWithRouter);
|
|
80
|
-
expect(result).to.equal('http://localhost:6002');
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
describe('with host and host + path config', function() {
|
|
85
|
-
it('should target http://localhost:6004 without path', function() {
|
|
86
|
-
req.headers.host = 'gamma.localhost';
|
|
87
|
-
result = router.getTarget(req, proxyOptionWithRouter);
|
|
88
|
-
expect(result).to.equal('http://localhost:6004');
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it('should target http://localhost:6003 exact path match', function() {
|
|
92
|
-
req.headers.host = 'gamma.localhost';
|
|
93
|
-
req.url = '/api';
|
|
94
|
-
result = router.getTarget(req, proxyOptionWithRouter);
|
|
95
|
-
expect(result).to.equal('http://localhost:6003');
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it('should target http://localhost:6004 when contains path', function() {
|
|
99
|
-
req.headers.host = 'gamma.localhost';
|
|
100
|
-
req.url = '/api/books/123';
|
|
101
|
-
result = router.getTarget(req, proxyOptionWithRouter);
|
|
102
|
-
expect(result).to.equal('http://localhost:6003');
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
describe('with just the path', function() {
|
|
107
|
-
it('should target http://localhost:6005 with just a path as router config', function() {
|
|
108
|
-
req.url = '/rest';
|
|
109
|
-
result = router.getTarget(req, proxyOptionWithRouter);
|
|
110
|
-
expect(result).to.equal('http://localhost:6005');
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
it('should target http://localhost:6005 with just a path as router config', function() {
|
|
114
|
-
req.url = '/rest/deep/path';
|
|
115
|
-
result = router.getTarget(req, proxyOptionWithRouter);
|
|
116
|
-
expect(result).to.equal('http://localhost:6005');
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
it('should target http://localhost:6000 path in not present in router config', function() {
|
|
120
|
-
req.url = '/unknow-path';
|
|
121
|
-
result = router.getTarget(req, proxyOptionWithRouter);
|
|
122
|
-
expect(result).to.equal(undefined);
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
describe('matching order of router config', function() {
|
|
127
|
-
it('should return first matching target when similar paths are configured', function() {
|
|
128
|
-
req.url = '/some/specific/path';
|
|
129
|
-
result = router.getTarget(req, proxyOptionWithRouter);
|
|
130
|
-
expect(result).to.equal('http://localhost:6006');
|
|
131
|
-
});
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
});
|