-
Notifications
You must be signed in to change notification settings - Fork 865
/
Copy pathrouter.spec.ts
161 lines (139 loc) · 5.22 KB
/
router.spec.ts
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
import { getTarget } from '../../src/router';
describe('router unit test', () => {
let fakeReq;
let config;
let result;
let proxyOptionWithRouter;
beforeEach(() => {
fakeReq = {
headers: {
host: 'localhost',
},
url: '/',
};
config = {
target: 'http://localhost:6000',
};
});
describe('router.getTarget from function', () => {
let request;
beforeEach(() => {
proxyOptionWithRouter = {
target: 'http://localhost:6000',
router(req) {
request = req;
return 'http://foobar.com:666';
},
};
result = getTarget(fakeReq, proxyOptionWithRouter);
});
describe('custom dynamic router function', () => {
it('should provide the request object for dynamic routing', () => {
expect(request.headers.host).toBe('localhost');
expect(request.url).toBe('/');
});
it('should return new target', () => {
return expect(result).resolves.toBe('http://foobar.com:666');
});
});
});
describe('router.getTarget from async function', () => {
let request;
beforeEach(() => {
proxyOptionWithRouter = {
target: 'http://localhost:6000',
async router(req) {
request = req;
return 'http://foobar.com:666';
},
};
result = getTarget(fakeReq, proxyOptionWithRouter);
});
describe('custom dynamic router async function', () => {
it('should provide the request object for dynamic routing', () => {
expect(request.headers.host).toBe('localhost');
expect(request.url).toBe('/');
});
it('should return new target', () => {
return expect(result).resolves.toBe('http://foobar.com:666');
});
});
});
describe('router.getTarget from table', () => {
beforeEach(() => {
proxyOptionWithRouter = {
target: 'http://localhost:6000',
router: {
'alpha.localhost': 'http://localhost:6001',
'beta.localhost': 'http://localhost:6002',
'gamma.localhost/api': 'http://localhost:6003',
'gamma.localhost': 'http://localhost:6004',
'/rest': 'http://localhost:6005',
'/some/specific/path': 'http://localhost:6006',
'/some': 'http://localhost:6007',
},
};
});
describe('without router config', () => {
it('should return the normal target when router not present in config', () => {
result = getTarget(fakeReq, config);
return expect(result).resolves.toBeUndefined();
});
});
describe('with just the host in router config', () => {
it('should target http://localhost:6001 when for router:"alpha.localhost"', () => {
fakeReq.headers.host = 'alpha.localhost';
result = getTarget(fakeReq, proxyOptionWithRouter);
return expect(result).resolves.toBe('http://localhost:6001');
});
it('should target http://localhost:6002 when for router:"beta.localhost"', () => {
fakeReq.headers.host = 'beta.localhost';
result = getTarget(fakeReq, proxyOptionWithRouter);
return expect(result).resolves.toBe('http://localhost:6002');
});
});
describe('with host and host + path config', () => {
it('should target http://localhost:6004 without path', () => {
fakeReq.headers.host = 'gamma.localhost';
result = getTarget(fakeReq, proxyOptionWithRouter);
return expect(result).resolves.toBe('http://localhost:6004');
});
it('should target http://localhost:6003 exact path match', () => {
fakeReq.headers.host = 'gamma.localhost';
fakeReq.url = '/api';
result = getTarget(fakeReq, proxyOptionWithRouter);
return expect(result).resolves.toBe('http://localhost:6003');
});
it('should target http://localhost:6004 when contains path', () => {
fakeReq.headers.host = 'gamma.localhost';
fakeReq.url = '/api/books/123';
result = getTarget(fakeReq, proxyOptionWithRouter);
return expect(result).resolves.toBe('http://localhost:6003');
});
});
describe('with just the path', () => {
it('should target http://localhost:6005 with just a path as router config', () => {
fakeReq.url = '/rest';
result = getTarget(fakeReq, proxyOptionWithRouter);
return expect(result).resolves.toBe('http://localhost:6005');
});
it('should target http://localhost:6005 with just a path as router config', () => {
fakeReq.url = '/rest/deep/path';
result = getTarget(fakeReq, proxyOptionWithRouter);
return expect(result).resolves.toBe('http://localhost:6005');
});
it('should target http://localhost:6000 path in not present in router config', () => {
fakeReq.url = '/unknown-path';
result = getTarget(fakeReq, proxyOptionWithRouter);
return expect(result).resolves.toBeUndefined();
});
});
describe('matching order of router config', () => {
it('should return first matching target when similar paths are configured', () => {
fakeReq.url = '/some/specific/path';
result = getTarget(fakeReq, proxyOptionWithRouter);
return expect(result).resolves.toBe('http://localhost:6006');
});
});
});
});