|
| 1 | +import { ExpressAdapter } from '@nestjs/platform-express'; |
| 2 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 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( |
| 46 | + 'app.listen needs to be called before calling app.getUrl', |
| 47 | + ); |
| 48 | + } |
| 49 | + }); |
| 50 | +}); |
0 commit comments