/
router.spec.ts
199 lines (171 loc) · 5.8 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
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
import { createProxyMiddleware, createApp, createAppWithPath } from './_utils';
import { ErrorRequestHandler } from 'express';
import * as request from 'supertest';
import { getLocal, generateCACertificate, Mockttp } from 'mockttp';
const untrustedCACert = generateCACertificate({ bits: 1024 });
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
describe('E2E router', () => {
let targetServerA: Mockttp;
let targetServerB: Mockttp;
let targetServerC: Mockttp;
beforeEach(async () => {
targetServerA = getLocal({ https: await untrustedCACert });
targetServerB = getLocal({ https: await untrustedCACert });
targetServerC = getLocal({ https: await untrustedCACert });
await targetServerA
.anyRequest()
.thenPassThrough({ ignoreHostCertificateErrors: ['localhost'] });
await targetServerB
.anyRequest()
.thenPassThrough({ ignoreHostCertificateErrors: ['localhost'] });
await targetServerC
.anyRequest()
.thenPassThrough({ ignoreHostCertificateErrors: ['localhost'] });
await targetServerA
.anyRequest()
.thenCallback(({ protocol }) => ({ body: protocol === 'https' ? 'A' : 'NOT HTTPS A' }));
await targetServerB
.anyRequest()
.thenCallback(({ protocol }) => ({ body: protocol === 'https' ? 'B' : 'NOT HTTPS B' }));
await targetServerC
.anyRequest()
.thenCallback(({ protocol }) => ({ body: protocol === 'https' ? 'C' : 'NOT HTTPS C' }));
await targetServerA.start(6001);
await targetServerB.start(6002);
await targetServerC.start(6003);
});
afterEach(async () => {
await targetServerA.stop();
await targetServerB.stop();
await targetServerC.stop();
});
describe('router with req', () => {
it('should work with a string', async () => {
const app = createApp(
createProxyMiddleware({
target: 'https://localhost:6001',
secure: false,
changeOrigin: true,
router(req) {
return 'https://localhost:6003';
}
})
);
const agent = request(app);
const response = await agent.get('/api').expect(200);
expect(response.text).toBe('C');
});
it('should work with an object', async () => {
const app = createApp(
createProxyMiddleware({
target: 'https://localhost:6001',
secure: false,
changeOrigin: true,
router(req) {
return { host: 'localhost', port: 6003, protocol: 'https:' };
}
})
);
const agent = request(app);
const response = await agent.get('/api').expect(200);
expect(response.text).toBe('C');
});
it('should work with an async callback', async () => {
const app = createApp(
createProxyMiddleware({
target: 'https://localhost:6001',
secure: false,
changeOrigin: true,
router: async req => {
return new Promise(resolve =>
resolve({ host: 'localhost', port: 6003, protocol: 'https:' })
);
}
})
);
const agent = request(app);
const response = await agent.get('/api').expect(200);
expect(response.text).toBe('C');
});
it('should handle promise rejection in router', async () => {
const app = createApp(
createProxyMiddleware({
target: 'https://localhost:6001',
secure: false,
changeOrigin: true,
router: async req => {
throw new Error('An error thrown in the router');
}
})
);
const errorHandler: ErrorRequestHandler = (err: Error, req, res, next) => {
res.status(502).send(err.message);
};
app.use(errorHandler);
const agent = request(app);
const response = await agent.get('/api').expect(502);
expect(response.text).toBe('An error thrown in the router');
});
it('missing a : will cause it to use http', async () => {
const app = createApp(
createProxyMiddleware({
target: 'https://localhost:6001',
secure: false,
changeOrigin: true,
router: async req => {
return new Promise(resolve =>
resolve({ host: 'localhost', port: 6003, protocol: 'https' })
);
}
})
);
const agent = request(app);
const response = await agent.get('/api').expect(200);
expect(response.text).toBe('NOT HTTPS C');
});
});
describe('router with proxyTable', () => {
let agent;
beforeEach(() => {
const app = createAppWithPath(
'/',
createProxyMiddleware({
target: 'https://localhost:6001',
secure: false,
changeOrigin: true,
router: {
'alpha.localhost:6000': 'https://localhost:6001',
'beta.localhost:6000': 'https://localhost:6002',
'localhost:6000/api': 'https://localhost:6003'
}
})
);
agent = request(app);
});
it('should proxy to option.target', async () => {
const response = await agent.get('/api').expect(200);
expect(response.text).toBe('A');
});
it('should proxy when host is "alpha.localhost"', async () => {
const response = await agent
.get('/api')
.set('host', 'alpha.localhost:6000')
.expect(200);
expect(response.text).toBe('A');
});
it('should proxy when host is "beta.localhost"', async () => {
const response = await agent
.get('/api')
.set('host', 'beta.localhost:6000')
.expect(200);
expect(response.text).toBe('B');
});
it('should proxy with host & path config: "localhost:6000/api"', async () => {
const response = await agent
.get('/api')
.set('host', 'localhost:6000')
.expect(200);
expect(response.text).toBe('C');
});
});
});