Skip to content

Commit ceaf0ff

Browse files
committed
ope
1 parent abc60fb commit ceaf0ff

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

tests/fixture-ts/testem-proxy.cjs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* eslint-disable */
2+
const httpProxy = require('http-proxy');
3+
4+
/*
5+
This can be installed as a testem middleware to make testem run against an
6+
arbitrary real webserver at targetURL.
7+
8+
It allows testem to handle the well-known testem-specific paths and proxies
9+
everything else, while rewriting the testem-added prefix out of your
10+
"/tests/index.html" URL.
11+
*/
12+
13+
module.exports = function testemProxy(targetURL) {
14+
return function testemProxyHandler(app) {
15+
const proxy = httpProxy.createProxyServer({
16+
changeOrigin: true,
17+
ignorePath: true,
18+
});
19+
20+
proxy.on('error', (err, _req, res) => {
21+
res && res.status && res.status(500).json(err);
22+
});
23+
24+
app.all('*', (req, res, next) => {
25+
let url = req.url;
26+
if (url === '/testem.js' || url.startsWith('/testem/')) {
27+
return next();
28+
}
29+
let m = /^(\/\d+)\/tests\/index.html/.exec(url);
30+
if (m) {
31+
url = url.slice(m[1].length);
32+
}
33+
proxy.web(req, res, { target: targetURL + url });
34+
});
35+
};
36+
};

0 commit comments

Comments
 (0)