-
Notifications
You must be signed in to change notification settings - Fork 40
/
start.js
92 lines (77 loc) · 2.4 KB
/
start.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
var talkback
if (process.env.USE_NPM) {
talkback = require("talkback")
console.log("Using NPM talkback")
} else {
talkback = require("../../dist")
}
var host = "https://api.github.com"
function fallbackMode(req) {
if (req.url.includes("/mytest")) {
return talkback.Options.FallbackMode.PROXY
}
return talkback.Options.FallbackMode.NOT_FOUND
}
function bodyMatcher(tape, req) {
if (tape.meta.tag === "fake-post") {
var tapeBody = JSON.parse(tape.req.body.toString())
var reqBody = JSON.parse(req.body.toString())
return tapeBody.username === reqBody.username
}
return false
}
function urlMatcher(tape, req) {
if (tape.meta.tag === "orgs-wildcard") {
return !!req.url.match(/\/orgs\/[a-zA-Z0-9]/)
}
return false
}
var requestStartTime = {}
function requestDecorator(req, context) {
requestStartTime[context.id] = new Date().getTime()
const acceptEncoding = req.headers["accept-encoding"]
if (acceptEncoding && acceptEncoding.includes("test")) {
delete req.headers["accept-encoding"]
}
return req
}
function tapeDecorator(tape, context) {
var originalDurationMs = new Date().getTime() - requestStartTime[context.id]
delete requestStartTime[context.id]
tape.meta.originalDurationMs = originalDurationMs
tape.meta.latency = [Math.floor(0.5*originalDurationMs), Math.floor(1.5*originalDurationMs)]
return tape
}
function responseDecorator(tape, req, context) {
if (tape.meta.tag === "auth") {
var tapeBody = JSON.parse(tape.res.body.toString())
var expiration = new Date()
expiration.setDate(expiration.getDate() + 1)
var expirationEpoch = Math.floor(expiration.getTime() / 1000)
tapeBody.expiration = expirationEpoch
var newBody = JSON.stringify(tapeBody)
tape.res.body = Buffer.from(newBody)
}
return tape
}
var server = talkback({
host: host,
path: __dirname + "/tapes",
record: process.env.RECORD === "true" ? talkback.Options.RecordMode.NEW : talkback.Options.RecordMode.DISABLED,
fallbackMode: fallbackMode,
debug: false,
name: "Example - Server",
ignoreQueryParams: ["t"],
ignoreHeaders: ["user-agent"],
bodyMatcher: bodyMatcher,
urlMatcher: urlMatcher,
requestDecorator: requestDecorator,
responseDecorator: responseDecorator,
tapeDecorator: tapeDecorator,
https: {
enabled: true,
keyPath: __dirname + "/httpsCert/localhost.key",
certPath: __dirname + "/httpsCert/localhost.crt"
}
})
server.start()