Repository navigation
Expand file tree
/
Copy pathserver.ts
More file actions
103 lines (94 loc) · 3.4 KB
/
Copy pathserver.ts
File metadata and controls
103 lines (94 loc) · 3.4 KB
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
import express, { Express } from 'express';
import { Server } from 'http';
import { resolve as resolvePath } from 'path';
let _app: Express;
let _server: Server;
const start = async (dir: string) => {
return new Promise<void>(resolve => {
_app = express();
// Serve the JavaScript files from the specific folder, since we are using browser
// based ES6 modules, this also means that we can just request the js/index.js file
// and all other relative paths are resolved from that file.
_app.use(
'/js',
express.static(`./test/e2e/generated/${dir}/`, {
extensions: ['', 'js'],
index: 'index.js',
})
);
_app.use(
'/js',
express.static(`./test/e2e/generated/${dir}/`, {
extensions: ['', 'js'],
index: 'index.js',
})
);
// Serve static assets
_app.get('/runtime.js', (req, res) => {
res.sendFile(resolvePath(`./test/e2e/generated/${dir}/runtime.js`));
});
_app.get('/polyfills.js', (req, res) => {
res.sendFile(resolvePath(`./test/e2e/generated/${dir}/polyfills.js`));
});
_app.get('/vendor.js', (req, res) => {
res.sendFile(resolvePath(`./test/e2e/generated/${dir}/vendor.js`));
});
_app.get('/main.js', (req, res) => {
res.sendFile(resolvePath(`./test/e2e/generated/${dir}/main.js`));
});
_app.get('/styles.css', (req, res) => {
res.sendFile(resolvePath(`./test/e2e/generated/${dir}/styles.css`));
});
_app.get('/favicon.ico', (req, res) => {
res.sendFile(resolvePath(`./test/e2e/generated/${dir}/favicon.ico`));
});
_app.get('/', (req, res) => {
res.sendFile(resolvePath(`./test/e2e/generated/${dir}/index.html`));
});
// Register an 'echo' server for testing error codes. This will just grab the
// status code from the query and return the default response (and text) from Express.
// See the spec files for more information.
_app.all('/base/api/v1.0/error', (req, res) => {
const status = parseInt(String(req.query.status));
res.status(status).json({
status,
message: 'hello world',
});
});
// Register an 'echo' server that just returns all data from the API calls.
// Although this might not be a 'correct' response, we can use this to test
// the majority of API calls.
_app.all('/base/api/v1.0/*', (req, res) => {
setTimeout(() => {
res.json({
method: req.method,
protocol: req.protocol,
hostname: req.hostname,
path: req.path,
url: req.url,
query: req.query,
body: req.body,
headers: req.headers,
});
}, 100);
});
_server = _app.listen(3000, () => {
resolve();
});
});
};
const stop = async () => {
return new Promise<void>((resolve, reject) => {
_server.close(err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
};
export default {
start,
stop,
};