Test Failed
Push — master ( 96bf47...0b3edf )
by Dmytro
03:06 queued 12s
created

tests/mock/index.js   A

Complexity

Total Complexity 5
Complexity/F 1.25

Size

Lines of Code 39
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 23
mnd 1
bc 1
fnc 4
dl 0
loc 39
rs 10
bpm 0.25
cpm 1.25
noi 1
c 0
b 0
f 0
1
import path from 'path';
2
import jsonServer from 'json-server';
3
import fse from 'fs-extra';
4
import * as fixtures from './fixtures';
5
6
const filesDir = path.join(__dirname, './files');
7
8
const { users, actions } = fixtures;
9
const badCode = 404;
10
11
function createMockApp() {
12
    const server = jsonServer.create();
13
    const router = jsonServer.router({ users });
14
    const middlewares = jsonServer.defaults();
15
16
    server.use(middlewares);
17
18
    server.start = async function (port) {
19
        let app;
20
21
        server.use('/api', router);
22
        server.post('/format/:format', async (req, res) => {
23
            if (req.params.format === 'xml') {
24
                res.type('application/xml');
25
26
                return res.send('<status>OK</status>');
27
            }
28
29
            if (req.params.format === 'Buffer') {
30
                const content = await fse.readFile(path.join(filesDir, '1.txt'));
31
32
                res.writeHead(200, {
33
                    'Content-Type'        : 'mimetype',
34
                    'Content-disposition' : 'attachment;filename=1.txt',
35
                    'Content-Length'      : content.length
36
                });
37
38
                return res.end(Buffer.from(content, 'binary'));
39
            }
40
41
            res.sendStatus(badCode);
42
        });
43
44
        await new Promise(res => {
45
            app = server.listen(port, res);
46
        });
47
48
        return app;
49
    };
50
51
    return server;
52
}
53
54
export default createMockApp;
55
export { fixtures, actions };
56
57