-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock-server.js
50 lines (40 loc) · 1.21 KB
/
mock-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
(function() {
/**
* Return a Promise that resolves with the specified value, after the specified timeout.
* @param {Any} resolveWithValue Value with which to resolve Promise.
* @param {Integer} timeout Number of milliseconds after which to resolve Promise
* @returns {Promise}
*/
function delay(resolveWithValue, timeout) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(resolveWithValue);
}, timeout);
});
}
var mock = XHRMock;
mock.setup();
mock.get('story', (req, res) => {
var response = res.status(200).body(JSON.stringify({
title: 'My Story',
chapters: ['chapter1', 'chapter2', 'chapter3', 'chapter4']
}));
return delay(response, Math.random() * 5000);
});
var chapters = {
chapter1: '1. Humpty Dumpty sat on a wall,',
chapter2: '2. Humpty Dumpty had a great fall;',
chapter3: '3. All the king\'s horses and all the king\'s men',
chapter4: '4. Couldn\'t put Humpty together again.'
};
mock.get(/^chapter/, (req, res) => {
var query = req.url().query;
var id = query && query.id;
var text = chapters[id];
var response = res.status(200).body(JSON.stringify({
id,
text
}));
return delay(response, Math.random() * 5000);
});
})();