aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/mock-servers/mock-instances-index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'shared/extra-utils/mock-servers/mock-instances-index.ts')
-rw-r--r--shared/extra-utils/mock-servers/mock-instances-index.ts42
1 files changed, 20 insertions, 22 deletions
diff --git a/shared/extra-utils/mock-servers/mock-instances-index.ts b/shared/extra-utils/mock-servers/mock-instances-index.ts
index 43c2e9f6e..92b12d6f3 100644
--- a/shared/extra-utils/mock-servers/mock-instances-index.ts
+++ b/shared/extra-utils/mock-servers/mock-instances-index.ts
@@ -1,41 +1,39 @@
1import express from 'express' 1import express from 'express'
2import { Server } from 'http' 2import { Server } from 'http'
3import { randomInt } from '@shared/core-utils' 3import { getPort, randomListen, terminateServer } from './utils'
4import { terminateServer } from './utils'
5 4
6export class MockInstancesIndex { 5export class MockInstancesIndex {
7 private server: Server 6 private server: Server
8 7
9 private readonly indexInstances: { host: string, createdAt: string }[] = [] 8 private readonly indexInstances: { host: string, createdAt: string }[] = []
10 9
11 initialize () { 10 async initialize () {
12 return new Promise<number>(res => { 11 const app = express()
13 const app = express()
14 12
15 app.use('/', (req: express.Request, res: express.Response, next: express.NextFunction) => { 13 app.use('/', (req: express.Request, res: express.Response, next: express.NextFunction) => {
16 if (process.env.DEBUG) console.log('Receiving request on mocked server %s.', req.url) 14 if (process.env.DEBUG) console.log('Receiving request on mocked server %s.', req.url)
17 15
18 return next() 16 return next()
19 }) 17 })
20
21 app.get('/api/v1/instances/hosts', (req: express.Request, res: express.Response) => {
22 const since = req.query.since
23 18
24 const filtered = this.indexInstances.filter(i => { 19 app.get('/api/v1/instances/hosts', (req: express.Request, res: express.Response) => {
25 if (!since) return true 20 const since = req.query.since
26 21
27 return i.createdAt > since 22 const filtered = this.indexInstances.filter(i => {
28 }) 23 if (!since) return true
29 24
30 return res.json({ 25 return i.createdAt > since
31 total: filtered.length,
32 data: filtered
33 })
34 }) 26 })
35 27
36 const port = 42000 + randomInt(1, 1000) 28 return res.json({
37 this.server = app.listen(port, () => res(port)) 29 total: filtered.length,
30 data: filtered
31 })
38 }) 32 })
33
34 this.server = await randomListen(app)
35
36 return getPort(this.server)
39 } 37 }
40 38
41 addInstance (host: string) { 39 addInstance (host: string) {