/
path-rewriter.spec.ts
90 lines (76 loc) · 2.35 KB
/
path-rewriter.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
import http from 'http';
import { createServer, proxyMiddleware } from './_utils';
describe('E2E pathRewrite', () => {
let targetMiddleware;
let targetData;
beforeEach(() => {
targetData = {};
targetMiddleware = (req, res, next) => {
targetData.url = req.url; // store target url.
targetData.headers = req.headers; // store target headers.
res.write(req.url); // respond with target url.
res.end();
};
});
let proxyServer;
let targetServer;
beforeEach(() => {
targetServer = createServer(8000, targetMiddleware);
});
afterEach(() => {
// tslint:disable-next-line: no-unused-expression
proxyServer && proxyServer.close();
targetServer.close();
});
describe('Rewrite paths with rules table', () => {
beforeEach(() => {
const proxyConfig = {
target: 'http://localhost:8000',
pathRewrite: {
'^/foobar/api/': '/api/'
}
};
const proxy = proxyMiddleware(proxyConfig);
proxyServer = createServer(3000, proxy);
});
beforeEach(done => {
http.get('http://localhost:3000/foobar/api/lorum/ipsum', res => {
done();
});
});
it('should remove "/foobar" from path', () => {
expect(targetData.url).toBe('/api/lorum/ipsum');
});
});
describe('Rewrite paths with function', () => {
let originalPath;
let pathRewriteReqObject;
beforeEach(() => {
const proxyConfig = {
target: 'http://localhost:8000',
pathRewrite(path, req) {
originalPath = path;
pathRewriteReqObject = req;
return path.replace('/foobar', '');
}
};
const proxy = proxyMiddleware(proxyConfig);
proxyServer = createServer(3000, proxy);
});
beforeEach(done => {
http.get('http://localhost:3000/foobar/api/lorum/ipsum', res => {
done();
});
});
it('should remove "/foobar" from path', () => {
expect(targetData.url).toBe('/api/lorum/ipsum');
});
it('should provide the `path` parameter with the unmodified path value', () => {
expect(originalPath).toBe('/foobar/api/lorum/ipsum');
});
it('should provide the `req` object as second parameter of the rewrite function', () => {
expect(pathRewriteReqObject.method).toBe('GET');
expect(pathRewriteReqObject.url).toBe('/api/lorum/ipsum');
});
});
});