aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/mock-servers/mock-instances-index.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-10-22 14:31:38 +0200
committerChocobozzz <me@florianbigard.com>2021-10-22 14:31:38 +0200
commitd1bfbdeb203b0e4f37e9468861c690171156ee29 (patch)
tree2a6051ef212b964eb2ded1021a625acd0ba32053 /shared/extra-utils/mock-servers/mock-instances-index.ts
parent5480933b7f088bf099b25fb467faace814f0da58 (diff)
downloadPeerTube-d1bfbdeb203b0e4f37e9468861c690171156ee29.tar.gz
PeerTube-d1bfbdeb203b0e4f37e9468861c690171156ee29.tar.zst
PeerTube-d1bfbdeb203b0e4f37e9468861c690171156ee29.zip
Random listen for mocked servers
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) {