diff options
Diffstat (limited to 'server/tests/shared/mock-servers')
-rw-r--r-- | server/tests/shared/mock-servers/index.ts | 8 | ||||
-rw-r--r-- | server/tests/shared/mock-servers/mock-429.ts | 33 | ||||
-rw-r--r-- | server/tests/shared/mock-servers/mock-email.ts | 61 | ||||
-rw-r--r-- | server/tests/shared/mock-servers/mock-http.ts | 23 | ||||
-rw-r--r-- | server/tests/shared/mock-servers/mock-instances-index.ts | 46 | ||||
-rw-r--r-- | server/tests/shared/mock-servers/mock-joinpeertube-versions.ts | 34 | ||||
-rw-r--r-- | server/tests/shared/mock-servers/mock-object-storage.ts | 41 | ||||
-rw-r--r-- | server/tests/shared/mock-servers/mock-plugin-blocklist.ts | 36 | ||||
-rw-r--r-- | server/tests/shared/mock-servers/mock-proxy.ts | 24 | ||||
-rw-r--r-- | server/tests/shared/mock-servers/shared.ts | 33 |
10 files changed, 0 insertions, 339 deletions
diff --git a/server/tests/shared/mock-servers/index.ts b/server/tests/shared/mock-servers/index.ts deleted file mode 100644 index 1fa983116..000000000 --- a/server/tests/shared/mock-servers/index.ts +++ /dev/null | |||
@@ -1,8 +0,0 @@ | |||
1 | export * from './mock-429' | ||
2 | export * from './mock-email' | ||
3 | export * from './mock-http' | ||
4 | export * from './mock-instances-index' | ||
5 | export * from './mock-joinpeertube-versions' | ||
6 | export * from './mock-object-storage' | ||
7 | export * from './mock-plugin-blocklist' | ||
8 | export * from './mock-proxy' | ||
diff --git a/server/tests/shared/mock-servers/mock-429.ts b/server/tests/shared/mock-servers/mock-429.ts deleted file mode 100644 index 1fc20b079..000000000 --- a/server/tests/shared/mock-servers/mock-429.ts +++ /dev/null | |||
@@ -1,33 +0,0 @@ | |||
1 | import express from 'express' | ||
2 | import { Server } from 'http' | ||
3 | import { getPort, randomListen, terminateServer } from './shared' | ||
4 | |||
5 | export class Mock429 { | ||
6 | private server: Server | ||
7 | private responseSent = false | ||
8 | |||
9 | async initialize () { | ||
10 | const app = express() | ||
11 | |||
12 | app.get('/', (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
13 | |||
14 | if (!this.responseSent) { | ||
15 | this.responseSent = true | ||
16 | |||
17 | // Retry after 5 seconds | ||
18 | res.header('retry-after', '2') | ||
19 | return res.sendStatus(429) | ||
20 | } | ||
21 | |||
22 | return res.sendStatus(200) | ||
23 | }) | ||
24 | |||
25 | this.server = await randomListen(app) | ||
26 | |||
27 | return getPort(this.server) | ||
28 | } | ||
29 | |||
30 | terminate () { | ||
31 | return terminateServer(this.server) | ||
32 | } | ||
33 | } | ||
diff --git a/server/tests/shared/mock-servers/mock-email.ts b/server/tests/shared/mock-servers/mock-email.ts deleted file mode 100644 index 6eda2dfda..000000000 --- a/server/tests/shared/mock-servers/mock-email.ts +++ /dev/null | |||
@@ -1,61 +0,0 @@ | |||
1 | import MailDev from '@peertube/maildev' | ||
2 | import { parallelTests, randomInt } from '@shared/core-utils' | ||
3 | |||
4 | class MockSmtpServer { | ||
5 | |||
6 | private static instance: MockSmtpServer | ||
7 | private started = false | ||
8 | private maildev: any | ||
9 | private emails: object[] | ||
10 | |||
11 | private constructor () { } | ||
12 | |||
13 | collectEmails (emailsCollection: object[]) { | ||
14 | return new Promise<number>((res, rej) => { | ||
15 | const port = parallelTests() ? randomInt(1025, 2000) : 1025 | ||
16 | this.emails = emailsCollection | ||
17 | |||
18 | if (this.started) { | ||
19 | return res(undefined) | ||
20 | } | ||
21 | |||
22 | this.maildev = new MailDev({ | ||
23 | ip: '127.0.0.1', | ||
24 | smtp: port, | ||
25 | disableWeb: true, | ||
26 | silent: true | ||
27 | }) | ||
28 | |||
29 | this.maildev.on('new', email => { | ||
30 | this.emails.push(email) | ||
31 | }) | ||
32 | |||
33 | this.maildev.listen(err => { | ||
34 | if (err) return rej(err) | ||
35 | |||
36 | this.started = true | ||
37 | |||
38 | return res(port) | ||
39 | }) | ||
40 | }) | ||
41 | } | ||
42 | |||
43 | kill () { | ||
44 | if (!this.maildev) return | ||
45 | |||
46 | this.maildev.close() | ||
47 | |||
48 | this.maildev = null | ||
49 | MockSmtpServer.instance = null | ||
50 | } | ||
51 | |||
52 | static get Instance () { | ||
53 | return this.instance || (this.instance = new this()) | ||
54 | } | ||
55 | } | ||
56 | |||
57 | // --------------------------------------------------------------------------- | ||
58 | |||
59 | export { | ||
60 | MockSmtpServer | ||
61 | } | ||
diff --git a/server/tests/shared/mock-servers/mock-http.ts b/server/tests/shared/mock-servers/mock-http.ts deleted file mode 100644 index b7a019e07..000000000 --- a/server/tests/shared/mock-servers/mock-http.ts +++ /dev/null | |||
@@ -1,23 +0,0 @@ | |||
1 | import express from 'express' | ||
2 | import { Server } from 'http' | ||
3 | import { getPort, randomListen, terminateServer } from './shared' | ||
4 | |||
5 | export class MockHTTP { | ||
6 | private server: Server | ||
7 | |||
8 | async initialize () { | ||
9 | const app = express() | ||
10 | |||
11 | app.get('/*', (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
12 | return res.sendStatus(200) | ||
13 | }) | ||
14 | |||
15 | this.server = await randomListen(app) | ||
16 | |||
17 | return getPort(this.server) | ||
18 | } | ||
19 | |||
20 | terminate () { | ||
21 | return terminateServer(this.server) | ||
22 | } | ||
23 | } | ||
diff --git a/server/tests/shared/mock-servers/mock-instances-index.ts b/server/tests/shared/mock-servers/mock-instances-index.ts deleted file mode 100644 index 598b007f1..000000000 --- a/server/tests/shared/mock-servers/mock-instances-index.ts +++ /dev/null | |||
@@ -1,46 +0,0 @@ | |||
1 | import express from 'express' | ||
2 | import { Server } from 'http' | ||
3 | import { getPort, randomListen, terminateServer } from './shared' | ||
4 | |||
5 | export class MockInstancesIndex { | ||
6 | private server: Server | ||
7 | |||
8 | private readonly indexInstances: { host: string, createdAt: string }[] = [] | ||
9 | |||
10 | async initialize () { | ||
11 | const app = express() | ||
12 | |||
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) | ||
15 | |||
16 | return next() | ||
17 | }) | ||
18 | |||
19 | app.get('/api/v1/instances/hosts', (req: express.Request, res: express.Response) => { | ||
20 | const since = req.query.since | ||
21 | |||
22 | const filtered = this.indexInstances.filter(i => { | ||
23 | if (!since) return true | ||
24 | |||
25 | return i.createdAt > since | ||
26 | }) | ||
27 | |||
28 | return res.json({ | ||
29 | total: filtered.length, | ||
30 | data: filtered | ||
31 | }) | ||
32 | }) | ||
33 | |||
34 | this.server = await randomListen(app) | ||
35 | |||
36 | return getPort(this.server) | ||
37 | } | ||
38 | |||
39 | addInstance (host: string) { | ||
40 | this.indexInstances.push({ host, createdAt: new Date().toISOString() }) | ||
41 | } | ||
42 | |||
43 | terminate () { | ||
44 | return terminateServer(this.server) | ||
45 | } | ||
46 | } | ||
diff --git a/server/tests/shared/mock-servers/mock-joinpeertube-versions.ts b/server/tests/shared/mock-servers/mock-joinpeertube-versions.ts deleted file mode 100644 index 502f4e2f5..000000000 --- a/server/tests/shared/mock-servers/mock-joinpeertube-versions.ts +++ /dev/null | |||
@@ -1,34 +0,0 @@ | |||
1 | import express from 'express' | ||
2 | import { Server } from 'http' | ||
3 | import { getPort, randomListen } from './shared' | ||
4 | |||
5 | export class MockJoinPeerTubeVersions { | ||
6 | private server: Server | ||
7 | private latestVersion: string | ||
8 | |||
9 | async initialize () { | ||
10 | const app = express() | ||
11 | |||
12 | app.use('/', (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
13 | if (process.env.DEBUG) console.log('Receiving request on mocked server %s.', req.url) | ||
14 | |||
15 | return next() | ||
16 | }) | ||
17 | |||
18 | app.get('/versions.json', (req: express.Request, res: express.Response) => { | ||
19 | return res.json({ | ||
20 | peertube: { | ||
21 | latestVersion: this.latestVersion | ||
22 | } | ||
23 | }) | ||
24 | }) | ||
25 | |||
26 | this.server = await randomListen(app) | ||
27 | |||
28 | return getPort(this.server) | ||
29 | } | ||
30 | |||
31 | setLatestVersion (latestVersion: string) { | ||
32 | this.latestVersion = latestVersion | ||
33 | } | ||
34 | } | ||
diff --git a/server/tests/shared/mock-servers/mock-object-storage.ts b/server/tests/shared/mock-servers/mock-object-storage.ts deleted file mode 100644 index ae76c4f3f..000000000 --- a/server/tests/shared/mock-servers/mock-object-storage.ts +++ /dev/null | |||
@@ -1,41 +0,0 @@ | |||
1 | import express from 'express' | ||
2 | import got, { RequestError } from 'got' | ||
3 | import { Server } from 'http' | ||
4 | import { pipeline } from 'stream' | ||
5 | import { ObjectStorageCommand } from '@shared/server-commands' | ||
6 | import { getPort, randomListen, terminateServer } from './shared' | ||
7 | |||
8 | export class MockObjectStorageProxy { | ||
9 | private server: Server | ||
10 | |||
11 | async initialize () { | ||
12 | const app = express() | ||
13 | |||
14 | app.get('/:bucketName/:path(*)', (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
15 | const url = `http://${req.params.bucketName}.${ObjectStorageCommand.getMockEndpointHost()}/${req.params.path}` | ||
16 | |||
17 | if (process.env.DEBUG) { | ||
18 | console.log('Receiving request on mocked server %s.', req.url) | ||
19 | console.log('Proxifying request to %s', url) | ||
20 | } | ||
21 | |||
22 | return pipeline( | ||
23 | got.stream(url, { throwHttpErrors: false }), | ||
24 | res, | ||
25 | (err: RequestError) => { | ||
26 | if (!err) return | ||
27 | |||
28 | console.error('Pipeline failed.', err) | ||
29 | } | ||
30 | ) | ||
31 | }) | ||
32 | |||
33 | this.server = await randomListen(app) | ||
34 | |||
35 | return getPort(this.server) | ||
36 | } | ||
37 | |||
38 | terminate () { | ||
39 | return terminateServer(this.server) | ||
40 | } | ||
41 | } | ||
diff --git a/server/tests/shared/mock-servers/mock-plugin-blocklist.ts b/server/tests/shared/mock-servers/mock-plugin-blocklist.ts deleted file mode 100644 index 5d6e01816..000000000 --- a/server/tests/shared/mock-servers/mock-plugin-blocklist.ts +++ /dev/null | |||
@@ -1,36 +0,0 @@ | |||
1 | import express, { Request, Response } from 'express' | ||
2 | import { Server } from 'http' | ||
3 | import { getPort, randomListen, terminateServer } from './shared' | ||
4 | |||
5 | type BlocklistResponse = { | ||
6 | data: { | ||
7 | value: string | ||
8 | action?: 'add' | 'remove' | ||
9 | updatedAt?: string | ||
10 | }[] | ||
11 | } | ||
12 | |||
13 | export class MockBlocklist { | ||
14 | private body: BlocklistResponse | ||
15 | private server: Server | ||
16 | |||
17 | async initialize () { | ||
18 | const app = express() | ||
19 | |||
20 | app.get('/blocklist', (req: Request, res: Response) => { | ||
21 | return res.json(this.body) | ||
22 | }) | ||
23 | |||
24 | this.server = await randomListen(app) | ||
25 | |||
26 | return getPort(this.server) | ||
27 | } | ||
28 | |||
29 | replace (body: BlocklistResponse) { | ||
30 | this.body = body | ||
31 | } | ||
32 | |||
33 | terminate () { | ||
34 | return terminateServer(this.server) | ||
35 | } | ||
36 | } | ||
diff --git a/server/tests/shared/mock-servers/mock-proxy.ts b/server/tests/shared/mock-servers/mock-proxy.ts deleted file mode 100644 index e741d6735..000000000 --- a/server/tests/shared/mock-servers/mock-proxy.ts +++ /dev/null | |||
@@ -1,24 +0,0 @@ | |||
1 | import { createServer, Server } from 'http' | ||
2 | import { createProxy } from 'proxy' | ||
3 | import { getPort, terminateServer } from './shared' | ||
4 | |||
5 | class MockProxy { | ||
6 | private server: Server | ||
7 | |||
8 | initialize () { | ||
9 | return new Promise<number>(res => { | ||
10 | this.server = createProxy(createServer()) | ||
11 | this.server.listen(0, () => res(getPort(this.server))) | ||
12 | }) | ||
13 | } | ||
14 | |||
15 | terminate () { | ||
16 | return terminateServer(this.server) | ||
17 | } | ||
18 | } | ||
19 | |||
20 | // --------------------------------------------------------------------------- | ||
21 | |||
22 | export { | ||
23 | MockProxy | ||
24 | } | ||
diff --git a/server/tests/shared/mock-servers/shared.ts b/server/tests/shared/mock-servers/shared.ts deleted file mode 100644 index 235642439..000000000 --- a/server/tests/shared/mock-servers/shared.ts +++ /dev/null | |||
@@ -1,33 +0,0 @@ | |||
1 | import { Express } from 'express' | ||
2 | import { Server } from 'http' | ||
3 | import { AddressInfo } from 'net' | ||
4 | |||
5 | function randomListen (app: Express) { | ||
6 | return new Promise<Server>(res => { | ||
7 | const server = app.listen(0, () => res(server)) | ||
8 | }) | ||
9 | } | ||
10 | |||
11 | function getPort (server: Server) { | ||
12 | const address = server.address() as AddressInfo | ||
13 | |||
14 | return address.port | ||
15 | } | ||
16 | |||
17 | function terminateServer (server: Server) { | ||
18 | if (!server) return Promise.resolve() | ||
19 | |||
20 | return new Promise<void>((res, rej) => { | ||
21 | server.close(err => { | ||
22 | if (err) return rej(err) | ||
23 | |||
24 | return res() | ||
25 | }) | ||
26 | }) | ||
27 | } | ||
28 | |||
29 | export { | ||
30 | randomListen, | ||
31 | getPort, | ||
32 | terminateServer | ||
33 | } | ||