diff options
author | Chocobozzz <me@florianbigard.com> | 2021-10-22 14:31:38 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-10-22 14:31:38 +0200 |
commit | d1bfbdeb203b0e4f37e9468861c690171156ee29 (patch) | |
tree | 2a6051ef212b964eb2ded1021a625acd0ba32053 /shared/extra-utils/mock-servers/utils.ts | |
parent | 5480933b7f088bf099b25fb467faace814f0da58 (diff) | |
download | PeerTube-d1bfbdeb203b0e4f37e9468861c690171156ee29.tar.gz PeerTube-d1bfbdeb203b0e4f37e9468861c690171156ee29.tar.zst PeerTube-d1bfbdeb203b0e4f37e9468861c690171156ee29.zip |
Random listen for mocked servers
Diffstat (limited to 'shared/extra-utils/mock-servers/utils.ts')
-rw-r--r-- | shared/extra-utils/mock-servers/utils.ts | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/shared/extra-utils/mock-servers/utils.ts b/shared/extra-utils/mock-servers/utils.ts index 64d94c868..235642439 100644 --- a/shared/extra-utils/mock-servers/utils.ts +++ b/shared/extra-utils/mock-servers/utils.ts | |||
@@ -1,4 +1,18 @@ | |||
1 | import { Express } from 'express' | ||
1 | import { Server } from 'http' | 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 | } | ||
2 | 16 | ||
3 | function terminateServer (server: Server) { | 17 | function terminateServer (server: Server) { |
4 | if (!server) return Promise.resolve() | 18 | if (!server) return Promise.resolve() |
@@ -13,5 +27,7 @@ function terminateServer (server: Server) { | |||
13 | } | 27 | } |
14 | 28 | ||
15 | export { | 29 | export { |
30 | randomListen, | ||
31 | getPort, | ||
16 | terminateServer | 32 | terminateServer |
17 | } | 33 | } |