]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/shared/mock-servers/mock-instances-index.ts
Move test functions outside extra-utils
[github/Chocobozzz/PeerTube.git] / server / tests / shared / mock-servers / mock-instances-index.ts
CommitLineData
41fb13c3 1import express from 'express'
cf0c8ee5 2import { Server } from 'http'
c55e3d72 3import { getPort, randomListen, terminateServer } from './shared'
6f1b4fa4
C
4
5export class MockInstancesIndex {
cf0c8ee5
C
6 private server: Server
7
a1587156 8 private readonly indexInstances: { host: string, createdAt: string }[] = []
6f1b4fa4 9
d1bfbdeb
C
10 async initialize () {
11 const app = express()
6f1b4fa4 12
d1bfbdeb
C
13 app.use('/', (req: express.Request, res: express.Response, next: express.NextFunction) => {
14 if (process.env.DEBUG) console.log('Receiving request on mocked server %s.', req.url)
6f1b4fa4 15
d1bfbdeb
C
16 return next()
17 })
6f1b4fa4 18
d1bfbdeb
C
19 app.get('/api/v1/instances/hosts', (req: express.Request, res: express.Response) => {
20 const since = req.query.since
6f1b4fa4 21
d1bfbdeb
C
22 const filtered = this.indexInstances.filter(i => {
23 if (!since) return true
6f1b4fa4 24
d1bfbdeb 25 return i.createdAt > since
6f1b4fa4
C
26 })
27
d1bfbdeb
C
28 return res.json({
29 total: filtered.length,
30 data: filtered
31 })
6f1b4fa4 32 })
d1bfbdeb
C
33
34 this.server = await randomListen(app)
35
36 return getPort(this.server)
6f1b4fa4
C
37 }
38
39 addInstance (host: string) {
40 this.indexInstances.push({ host, createdAt: new Date().toISOString() })
41 }
cf0c8ee5
C
42
43 terminate () {
44 return terminateServer(this.server)
45 }
6f1b4fa4 46}