]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/mock-servers/mock-instances-index.ts
Add peertube short link import test
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / mock-servers / mock-instances-index.ts
1 import express from 'express'
2 import { Server } from 'http'
3 import { randomInt } from '@shared/core-utils'
4 import { terminateServer } from './utils'
5
6 export class MockInstancesIndex {
7 private server: Server
8
9 private readonly indexInstances: { host: string, createdAt: string }[] = []
10
11 initialize () {
12 return new Promise<number>(res => {
13 const app = express()
14
15 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)
17
18 return next()
19 })
20
21 app.get('/api/v1/instances/hosts', (req: express.Request, res: express.Response) => {
22 const since = req.query.since
23
24 const filtered = this.indexInstances.filter(i => {
25 if (!since) return true
26
27 return i.createdAt > since
28 })
29
30 return res.json({
31 total: filtered.length,
32 data: filtered
33 })
34 })
35
36 const port = 42000 + randomInt(1, 1000)
37 this.server = app.listen(port, () => res(port))
38 })
39 }
40
41 addInstance (host: string) {
42 this.indexInstances.push({ host, createdAt: new Date().toISOString() })
43 }
44
45 terminate () {
46 return terminateServer(this.server)
47 }
48 }