-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(http-adapter) Added the application's global prefix to the er…
…ror and not found handlers, cors and body parser registrations, and enabled the adapters to register them based on prefix.
- Loading branch information
Showing
10 changed files
with
246 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { ExpressAdapter } from '@nestjs/platform-express'; | ||
import { Test } from '@nestjs/testing'; | ||
import * as express from 'express'; | ||
import * as request from 'supertest'; | ||
import { ApplicationModule } from '../src/app.module'; | ||
|
||
describe('Hello world (express instance with multiple applications)', () => { | ||
let server; | ||
let apps: INestApplication[]; | ||
|
||
beforeEach(async () => { | ||
const module1 = await Test.createTestingModule({ | ||
imports: [ApplicationModule], | ||
}).compile(); | ||
const module2 = await Test.createTestingModule({ | ||
imports: [ApplicationModule], | ||
}).compile(); | ||
|
||
const adapter = new ExpressAdapter(express()); | ||
|
||
apps = [ | ||
module1.createNestApplication(adapter), | ||
module2.createNestApplication(adapter).setGlobalPrefix('/app2'), | ||
]; | ||
await Promise.all(apps.map(app => app.init())); | ||
|
||
server = adapter.getInstance(); | ||
}); | ||
|
||
it(`/GET`, () => { | ||
return request(server) | ||
.get('/hello') | ||
.expect(200) | ||
.expect('Hello world!'); | ||
}); | ||
|
||
it(`/GET (app2)`, () => { | ||
return request(server) | ||
.get('/app2/hello') | ||
.expect(200) | ||
.expect('Hello world!'); | ||
}); | ||
|
||
it(`/GET (Promise/async)`, () => { | ||
return request(server) | ||
.get('/hello/async') | ||
.expect(200) | ||
.expect('Hello world!'); | ||
}); | ||
|
||
it(`/GET (app2 Promise/async)`, () => { | ||
return request(server) | ||
.get('/app2/hello/async') | ||
.expect(200) | ||
.expect('Hello world!'); | ||
}); | ||
|
||
it(`/GET (Observable stream)`, () => { | ||
return request(server) | ||
.get('/hello/stream') | ||
.expect(200) | ||
.expect('Hello world!'); | ||
}); | ||
|
||
it(`/GET (app2 Observable stream)`, () => { | ||
return request(server) | ||
.get('/app2/hello/stream') | ||
.expect(200) | ||
.expect('Hello world!'); | ||
}); | ||
|
||
afterEach(async () => { | ||
await Promise.all(apps.map(app => app.close())); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { | ||
FastifyAdapter, | ||
NestFastifyApplication, | ||
} from '@nestjs/platform-fastify'; | ||
import { Test } from '@nestjs/testing'; | ||
import { expect } from 'chai'; | ||
import { ApplicationModule } from '../src/app.module'; | ||
|
||
describe('Hello world (fastify adapter with multiple applications)', () => { | ||
let adapter: FastifyAdapter; | ||
let apps: NestFastifyApplication[]; | ||
|
||
beforeEach(async () => { | ||
const module1 = await Test.createTestingModule({ | ||
imports: [ApplicationModule], | ||
}).compile(); | ||
const module2 = await Test.createTestingModule({ | ||
imports: [ApplicationModule], | ||
}).compile(); | ||
|
||
adapter = new FastifyAdapter(); | ||
|
||
apps = [ | ||
module1.createNestApplication<NestFastifyApplication>(adapter), | ||
module2 | ||
.createNestApplication<NestFastifyApplication>(adapter) | ||
.setGlobalPrefix('/app2'), | ||
]; | ||
await Promise.all(apps.map(app => app.init())); | ||
}); | ||
|
||
it(`/GET`, () => { | ||
return adapter | ||
.inject({ | ||
method: 'GET', | ||
url: '/hello', | ||
}) | ||
.then(({ payload }) => expect(payload).to.be.eql('Hello world!')); | ||
}); | ||
|
||
it(`/GET (app2)`, () => { | ||
return adapter | ||
.inject({ | ||
method: 'GET', | ||
url: '/app2/hello', | ||
}) | ||
.then(({ payload }) => expect(payload).to.be.eql('Hello world!')); | ||
}); | ||
|
||
it(`/GET (Promise/async)`, () => { | ||
return adapter | ||
.inject({ | ||
method: 'GET', | ||
url: '/hello/async', | ||
}) | ||
.then(({ payload }) => expect(payload).to.be.eql('Hello world!')); | ||
}); | ||
|
||
it(`/GET (app2 Promise/async)`, () => { | ||
return adapter | ||
.inject({ | ||
method: 'GET', | ||
url: '/app2/hello/async', | ||
}) | ||
.then(({ payload }) => expect(payload).to.be.eql('Hello world!')); | ||
}); | ||
|
||
it(`/GET (Observable stream)`, () => { | ||
return adapter | ||
.inject({ | ||
method: 'GET', | ||
url: '/hello/stream', | ||
}) | ||
.then(({ payload }) => expect(payload).to.be.eql('Hello world!')); | ||
}); | ||
|
||
it(`/GET (app2 Observable stream)`, () => { | ||
return adapter | ||
.inject({ | ||
method: 'GET', | ||
url: '/app2/hello/stream', | ||
}) | ||
.then(({ payload }) => expect(payload).to.be.eql('Hello world!')); | ||
}); | ||
|
||
afterEach(async () => { | ||
await Promise.all(apps.map(app => app.close())); | ||
await adapter.close(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.