-
Notifications
You must be signed in to change notification settings - Fork 6
/
mock_server.js
133 lines (129 loc) · 4.33 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
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
import fs from 'fs'
import http from 'http'
import path from 'path'
let args = process.argv.slice(1)
let scenarios = path.resolve(args[0], '..', '..', 'scenarios')
let config = JSON.parse(fs.readFileSync(args[1]).toString())
function listener (req, res) {
// reject non-POST requests
if ('POST' !== req.method) {
res.writeHead(405)
res.end(`${req.method} not allowed`)
return
}
// reject requests without a body
if (!req.headers['content-length']) {
res.writeHead(411)
res.end('payload required')
return
}
// reject if unknown endpoint
let stripEcosystem = req.url.slice(0, req.url.lastIndexOf('/'))
if (!Object.values(config['endpoints']).includes(stripEcosystem)) {
res.writeHead(404)
res.end(`unknown endpoint ${req.url}`)
return
}
// iterate over all supported ecosystems
let accept = req.headers['accept']
for (let ecosystem in config['ecosystems']) {
// handle component analysis for current ecosystem
if (`${config['endpoints']['component']}/${ecosystem}` === req.url) {
// reject unsupported media types requests
if (!'application/json' === accept) {
res.writeHead(415)
res.end(`${accept} is not supported`)
return
}
// load expected request from file
let requestBodyFile = config['ecosystems'][ecosystem]['component']['request']['body']
let expectedRequestBody = fs.readFileSync(
path.resolve(scenarios, ecosystem, requestBodyFile)
)
// load return response from file
let responseFile = config['ecosystems'][ecosystem]['component']['response']['json']
let componentResponse = fs.readFileSync(
path.resolve(scenarios, ecosystem, responseFile)
)
// operate on body
req.on('data', data => {
if (expectedRequestBody.toString().replace(/\s+/g, '')
=== data.toString().replace(/\s+/g, '')) {
// if body as expected
res.setHeader('ContentType', 'application/json')
res.end(componentResponse.toString())
return
}
res.writeHead(400)
res.end('payload not as expected')
})
}
// handle stack analysis for current ecosystem
if (`${config['endpoints']['stack']}/${ecosystem}` === req.url) {
// reject unsupported media types requests
if (!['application/json', 'text/html'].includes(accept)) {
res.writeHead(415)
res.end(`${accept} is not supported`)
return
}
// if requested stack json report
if ('application/json' === accept) {
// load expected request from file
let requestBodyFile = config['ecosystems'][ecosystem]['stack']['request']['body']
let expectedRequestBody = fs.readFileSync(
path.resolve(scenarios, ecosystem, requestBodyFile)
)
// load return response from file
let responseFile = config['ecosystems'][ecosystem]['stack']['response']['json']
let stackResponse = fs.readFileSync(
path.resolve(scenarios, ecosystem, responseFile)
)
// operate on body
req.on('data', data => {
if (expectedRequestBody.toString().replace(/\s+/g, '')
=== data.toString().replace(/\s+/g, '')) {
// if body as expected
res.setHeader('ContentType', 'application/json')
res.end(stackResponse.toString())
return
}
res.writeHead(400)
res.end('payload not as expected')
})
}
// if requested stack html report
if ('text/html' === accept) {
// load expected request from file
let requestBodyFile = config['ecosystems'][ecosystem]['stack']['request']['body']
let expectedRequestBody = fs.readFileSync(
path.resolve(scenarios, ecosystem, requestBodyFile)
)
// load return response from file
let responseFile = config['ecosystems'][ecosystem]['stack']['response']['html']
let stackResponse = fs.readFileSync(
path.resolve(scenarios, ecosystem, responseFile)
)
// operate on body
req.on('data', data => {
if (expectedRequestBody.toString().replace(/\s+/g, '')
=== data.toString().replace(/\s+/g, '')) {
// if body as expected
res.setHeader('ContentType', 'text/html')
res.end(stackResponse.toString())
return
}
res.writeHead(400)
res.end('payload not as expected')
})
}
}
}
}
// create and start the server
let server = http.createServer(listener)
server.listen(config['port'], config['host'])
// close server after configured seconds
server.close(async () => {
await new Promise(resolve => setTimeout(resolve, config['up_for'] * 1000))
process.exit(0)
})