-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
32 lines (25 loc) · 813 Bytes
/
index.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
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
function send(response, kwargs) {
let data = kwargs.data || ''
let status = kwargs.status || 200
let contentType = kwargs.type || 'text/plain'
if (['json', 'application/json'].includes(contentType)) {
data = JSON.stringify(data)
}
response.status(status).type(contentType).send(data)
}
exports.mockResponse = (request, response) => {
switch (request.path) {
case '/status':
return send(response, {status: request.query.code})
case '/json':
return send(response, {data: request.query, type: 'application/json'})
case '/html':
return send(response, {data: request.query.string, type: 'text/html'})
}
}