-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathserver.js
More file actions
169 lines (135 loc) · 3.57 KB
/
server.js
File metadata and controls
169 lines (135 loc) · 3.57 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
'use strict'
const http = require('http')
const async = require('async')
class Mocks {
constructor (routes, opts) {
this.routes = this.parseRouting(routes)
this.debug = opts.debug
}
parseRouting (routes) {
if (!Array.isArray(routes)) {
routes = [routes]
}
routes = routes.map((route) => {
if (route.route && route.match) {
throw new Error(
'shorthand .route must be used without .match'
)
}
if (route.route) {
route.match = (data, match) => {
match({ route: route.route })
}
}
if (typeof route.reply !== 'function') {
const msg = route.reply
route.reply = (reply) => {
reply(msg, 200)
}
}
return route
})
return routes
}
getMock (req, res, data, cb) {
const match = (route, cb) => {
route.match(data, (filters) => {
if (filters.route && filters.route !== req.url) {
return cb(null, false)
}
if (filters.method && filters.method !== req.method) {
return cb(null, false)
}
if (!filters.custom) {
filters.custom = (req, res, cb) => {
return cb(null, true)
}
}
filters.custom(req, res, (err, res) => {
if (err) return cb(err)
if (res === false) return cb(null, false)
if (!filters.payload) return cb(null, res)
filters.payload(data, cb)
})
})
}
async.filter(this.routes, match, cb)
}
stubReply (req, res, mock) {
mock.reply((data, code) => {
if (code) {
res.statusCode = code
}
if (typeof data === 'object') {
data = JSON.stringify(data)
}
res.end(data)
})
}
handle (req, res) {
const data = []
req.on('data', (buf) => {
data.push(buf)
})
req.on('end', () => {
const str = Buffer.concat(data).toString()
this.debug && console.log('[DEBUG] bfx-svc-test-helper:', str)
let parsed
try {
parsed = JSON.parse(str)
} catch (e) {
parsed = str
}
this.getMock(req, res, parsed, (err, matches) => {
if (err) {
console.error('bfx-svc-test-helper: route matching error')
throw err
}
if (matches.length === 0) {
throw new Error(`bfx-svc-test-helper: no route matched (${req.url})`)
}
if (matches.length > 1) {
throw new Error(`bfx-svc-test-helper: multiple routes matched (${req.url})`)
}
const mock = matches[0]
if (!mock.test) mock.test = (req, res, payload, cb) => { cb(null) }
mock.test(req, res, parsed, (err) => {
if (err) {
console.error('bfx-svc-test-helper: client request test failed')
throw err
}
this.stubReply(req, res, mock)
})
})
})
}
}
class Server {
constructor (mocks, opts) {
this.opts = opts
this.mocks = mocks
this.server = null
this.port = this.opts.port
}
start (cb = () => {}) {
return new Promise((resolve, reject) => {
this.server = http.createServer(this.mocks.handle.bind(this.mocks)).listen(this.port, () => {
resolve()
cb(null)
})
})
}
stop (cb = () => {}) {
return new Promise((resolve, reject) => {
this.server.close(() => {
resolve()
cb(null)
})
})
}
}
function createServer (mocks, opts) {
const m = typeof mocks === 'function' ? mocks() : new Mocks(mocks, opts)
return new Server(m, opts)
}
module.exports = createServer