forked from freeCodeCamp/boilerplate-mochachai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
119 lines (105 loc) · 2.57 KB
/
server.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
'use strict'
const express = require('express');
const app = express();
const cors = require('cors');
const runner = require('./test-runner');
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.get('/', function (req, res) {
res.sendFile(__dirname + '/views/index.html');
})
app.use(express.static(__dirname + '/public'));
app.get('/hello', function (req, res) {
const name = req.query.name || 'Guest';
res.type('txt').send('hello ' + name);
})
const travellers = function (req, res) {
let data = {};
if (req.body && req.body.surname) {
switch (req.body.surname.toLowerCase()) {
case 'polo':
data = {
name: 'Marco',
surname: 'Polo',
dates: '1254 - 1324'
};
break;
case 'colombo':
data = {
name: 'Cristoforo',
surname: 'Colombo',
dates: '1451 - 1506'
};
break;
case 'vespucci':
data = {
name: 'Amerigo',
surname: 'Vespucci',
dates: '1454 - 1512'
};
break;
case 'da verrazzano':
case 'verrazzano':
data = {
name: 'Giovanni',
surname: 'da Verrazzano',
dates: '1485 - 1528'
};
break;
default:
data = {
name: 'unknown'
}
}
}
res.json(data);
};
app.route('/travellers')
.put(travellers);
let error;
app.get('/_api/get-tests', cors(), function (req, res, next) {
if (error)
return res.json({ status: 'unavailable' });
next();
},
function (req, res, next) {
if (!runner.report) return next();
res.json(testFilter(runner.report, req.query.type, req.query.n));
},
function (req, res) {
runner.on('done', function (report) {
process.nextTick(() => res.json(testFilter(runner.report, req.query.type, req.query.n)));
});
});
const port = process.env.PORT || 3000;
app.listen(port, function () {
console.log("Listening on port " + port);
console.log('Running Tests...');
setTimeout(function () {
try {
runner.run();
} catch (e) {
error = e;
console.log('Tests are not valid:');
console.log(error);
}
}, 1500);
});
module.exports = app; // for testing
function testFilter(tests, type, n) {
let out;
switch (type) {
case 'unit':
out = tests.filter(t => t.context.match('Unit Tests'));
break;
case 'functional':
out = tests.filter(t => t.context.match('Functional Tests'));
break;
default:
out = tests;
}
if (n !== undefined) {
return out[n] || out;
}
return out;
}