-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtestem-proxy.cjs
More file actions
35 lines (30 loc) · 999 Bytes
/
testem-proxy.cjs
File metadata and controls
35 lines (30 loc) · 999 Bytes
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
const httpProxy = require('http-proxy');
/*
This can be installed as a testem middleware to make testem run against an
arbitrary real webserver at targetURL.
It allows testem to handle the well-known testem-specific paths and proxies
everything else, while rewriting the testem-added prefix out of your
"/tests/index.html" URL.
*/
module.exports = function testemProxy(targetURL) {
return function testemProxyHandler(app) {
const proxy = httpProxy.createProxyServer({
changeOrigin: true,
ignorePath: true,
});
proxy.on('error', (err, _req, res) => {
res && res.status && res.status(500).json(err);
});
app.all('*', (req, res, next) => {
let url = req.url;
if (url === '/testem.js' || url.startsWith('/testem/')) {
return next();
}
let m = /^(\/\d+)\/tests\/index.html/.exec(url);
if (m) {
url = url.slice(m[1].length);
}
proxy.web(req, res, { target: targetURL + url });
});
};
}