-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathdelayed_run.js
57 lines (43 loc) · 1.36 KB
/
delayed_run.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
'use strict'
var debug = require('debug-log')('mockbin')
module.exports = function (req, res, next) {
this.client.get('bin:' + req.params.uuid, function (err, value) {
if (err) {
debug(err)
throw err
}
// delayed bin response
var delay = req.params.ms ? parseInt(req.params.ms, 10) : 200
if (delay > 60000) {
delay = 60000
}
if (value) {
var har = JSON.parse(value)
// log interaction & send the appropriate response based on HAR
this.client.rpush('log:' + req.params.uuid, JSON.stringify(req.har.log.entries[0]))
this.client.ltrim('log:' + req.params.uuid, 0, 100)
setTimeout(function () {
// headers
har.headers.map(function (header) {
res.set(header.name, header.value)
})
// delay header
res.set("mockbin-delay", delay)
// cookies
har.cookies.map(function (cookie) {
res.cookie(cookie.name, cookie.value)
})
// status
res.httpVersion = har.httpVersion.split('/')[1]
res.statusCode = har.status || 200
res.statusMessage = har.statusText || 'OK'
// special condition
if (har.redirectURL !== '') {
res.location(har.redirectURL)
}
res.body = har.content.text ? har.content.text : null
next()
}, delay)
}
}.bind(this))
}