Skip to content

Commit 396febc

Browse files
Merge branch 'get-app-url' of https://github.com/jmcdo29/nest into jmcdo29-get-app-url
2 parents 810e74c + 19677b5 commit 396febc

14 files changed

Lines changed: 1804 additions & 1 deletion

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# dependencies
2+
/node_modules
3+
4+
# IDE
5+
/.idea
6+
/.awcache
7+
/.vscode
8+
9+
# misc
10+
npm-debug.log
11+
12+
# example
13+
/quick-start
14+
15+
# tests
16+
/test
17+
/coverage
18+
/.nyc_output
19+
20+
# dist
21+
/dist
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { ExpressAdapter } from '@nestjs/platform-express';
3+
import { expect } from 'chai';
4+
import * as express from 'express';
5+
import { AppModule } from '../src/app.module';
6+
import { randomPort } from './utils';
7+
8+
describe('Get-Url (Express Application)', () => {
9+
let testModule: TestingModule;
10+
let port: number;
11+
12+
beforeEach(async () => {
13+
testModule = await Test.createTestingModule({
14+
imports: [AppModule],
15+
}).compile();
16+
});
17+
18+
beforeEach(async () => {
19+
port = await randomPort();
20+
});
21+
22+
it('should be able to get the IPv6 address', async () => {
23+
const app = testModule.createNestApplication(new ExpressAdapter(express()));
24+
await app.listen(port);
25+
expect(await app.getUrl()).to.be.eql(`http://[::1]:${port}`);
26+
await app.close();
27+
});
28+
it('should be able to get the IPv4 address', async () => {
29+
const app = testModule.createNestApplication(new ExpressAdapter(express()));
30+
await app.listen(port, '127.0.0.5');
31+
expect(await app.getUrl()).to.be.eql(`http://127.0.0.5:${port}`);
32+
await app.close();
33+
});
34+
it('should return 127.0.0.1 for 0.0.0.0', async () => {
35+
const app = testModule.createNestApplication(new ExpressAdapter(express()));
36+
await app.listen(port, '0.0.0.0');
37+
expect(await app.getUrl()).to.be.eql(`http://127.0.0.1:${port}`);
38+
await app.close();
39+
});
40+
it('should throw an error for calling getUrl before listen', async () => {
41+
const app = testModule.createNestApplication(new ExpressAdapter(express()));
42+
try {
43+
await app.getUrl();
44+
} catch (err) {
45+
expect(err).to.be.eql('app.listen needs to be called before calling app.getUrl');
46+
}
47+
});
48+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { FastifyAdapter } from '@nestjs/platform-fastify';
3+
import { expect } from 'chai';
4+
import { AppModule } from '../src/app.module';
5+
import { randomPort } from './utils';
6+
7+
describe('Get-Url (Fastify Application)', () => {
8+
let testModule: TestingModule;
9+
let port: number;
10+
11+
beforeEach(async () => {
12+
testModule = await Test.createTestingModule({
13+
imports: [AppModule],
14+
}).compile();
15+
});
16+
17+
beforeEach(async () => {
18+
port = await randomPort();
19+
});
20+
21+
it('should be able to get the IPv4 address', async () => {
22+
const app = testModule.createNestApplication(new FastifyAdapter());
23+
await app.listen(port, '127.0.0.5');
24+
expect(await app.getUrl()).to.be.eql(`http://127.0.0.5:${port}`);
25+
await app.close();
26+
});
27+
it('should return 127.0.0.1 for 0.0.0.0', async () => {
28+
const app = testModule.createNestApplication(new FastifyAdapter());
29+
await app.listen(port, '0.0.0.0');
30+
expect(await app.getUrl()).to.be.eql(`http://127.0.0.1:${port}`);
31+
await app.close();
32+
});
33+
it('should throw an error for calling getUrl before listen', async () => {
34+
const app = testModule.createNestApplication(new FastifyAdapter());
35+
try {
36+
await app.getUrl();
37+
} catch (err) {
38+
expect(err).to.be.eql('app.listen needs to be called before calling app.getUrl');
39+
}
40+
});
41+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as net from 'net';
2+
3+
export let port: number;
4+
5+
export async function randomPort(): Promise<number> {
6+
const server = net.createServer();
7+
return new Promise((resolve, reject) => {
8+
if (port) {
9+
resolve(port);
10+
}
11+
server.listen(0, () => {
12+
port = (server.address() as net.AddressInfo).port;
13+
server.close();
14+
resolve(port);
15+
});
16+
});
17+
}

0 commit comments

Comments
 (0)