forked from elastic/apm-agent-rum-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-servers.js
125 lines (108 loc) · 3.78 KB
/
test-servers.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
/**
* MIT License
*
* Copyright (c) 2017-present, Elasticsearch BV
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
const express = require('express')
const serveIndex = require('serve-index')
const proxy = require('express-http-proxy')
const { join } = require('path')
const { runIntegrationTest } = require('./integration-test')
const { DEFAULT_APM_SERVER_URL } = require('./test-config')
function startBackendAgentServer(port = 8003) {
const express = require('express')
const app = express()
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
if (req.method === 'OPTIONS') {
res.header(
'Access-Control-Allow-Headers',
'Origin, traceparent, tracestate, Content-Type, Accept'
)
}
next()
})
function dTRespond(req, res) {
const header = req.headers['traceparent']
let payload = { noHeader: true }
if (header) {
const splited = header.split('-')
payload = {
version: splited[0],
traceId: splited[1],
parentId: splited[2],
flags: splited[3]
}
console.log('traceparent:', header)
}
res.setHeader('Content-Type', 'application/json')
res.send(JSON.stringify(payload))
}
app.post('/data', dTRespond)
app.post('/fetch', dTRespond)
const server = app.listen(port)
console.log('[Backend Server] - serving on: ', port)
return server
}
function startApmServerProxy(port = 8001) {
const serverUrl = DEFAULT_APM_SERVER_URL
let proxyApp = express()
proxyApp.use(proxy(serverUrl))
const proxyServer = proxyApp.listen(port)
console.log(`[APM Server Proxy] - For ${serverUrl} on: ${port}`)
return proxyServer
}
function startTestServers(path = join(__dirname, '../'), port = 8000) {
const app = express()
const staticPath = path
app.get('/healthcheck', function (req, res) {
res.send('OK')
})
app.get('/run_integration_test', async function (req, res) {
const echo = req.query.echo
try {
const result = await runIntegrationTest(
`http://localhost:${port}/test/e2e/general-usecase/`
)
if (echo) {
return res.send(echo)
}
res.send(result)
} catch (err) {
/**
* This implies a failure in running the Integration test and we
* return 500 instead of success status code 200
*/
res.status(500).send(err.message)
}
})
app.use(express.static(staticPath), serveIndex(staticPath, { icons: false }))
const staticServer = app.listen(port)
console.log('[Static Server] - serving on: ', staticPath, port)
const backendAgentServer = startBackendAgentServer()
const apmServerProxy = startApmServerProxy()
return [staticServer, backendAgentServer, apmServerProxy]
}
module.exports = {
startBackendAgentServer,
startTestServers
}