Skip to content

Commit 7818a4d

Browse files
merge(): resolve conflicts
2 parents 9265743 + 6893bd6 commit 7818a4d

10 files changed

Lines changed: 271 additions & 18316 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import {
2+
Controller,
3+
Get,
4+
INestApplication,
5+
MiddlewareConsumer,
6+
Module,
7+
} from '@nestjs/common';
8+
import { INestFastifyApplication } from '@nestjs/common/interfaces/nest-fastify-application.interface';
9+
import { FastifyAdapter } from '@nestjs/core';
10+
import { Test } from '@nestjs/testing';
11+
import { expect } from 'chai';
12+
import { ApplicationModule } from '../src/app.module';
13+
14+
const RETURN_VALUE = 'test';
15+
const SCOPED_VALUE = 'test_scoped';
16+
17+
@Controller()
18+
class TestController {
19+
@Get('test')
20+
test() {
21+
return '';
22+
}
23+
}
24+
25+
@Module({
26+
imports: [ApplicationModule],
27+
controllers: [TestController],
28+
})
29+
class TestModule {
30+
configure(consumer: MiddlewareConsumer) {
31+
consumer
32+
.apply((req, res, next) => res.end(SCOPED_VALUE))
33+
.forRoutes(TestController)
34+
.apply((req, res, next) => res.end(RETURN_VALUE))
35+
.forRoutes('*');
36+
}
37+
}
38+
39+
describe('Middleware (FastifyAdapter)', () => {
40+
let app: INestFastifyApplication & INestApplication;
41+
42+
beforeEach(async () => {
43+
app = (await Test.createTestingModule({
44+
imports: [TestModule],
45+
}).compile()).createNestApplication(new FastifyAdapter());
46+
47+
await app.init();
48+
});
49+
50+
it(`forRoutes(*)`, () => {
51+
return app
52+
.inject({
53+
method: 'GET',
54+
url: '/hello',
55+
})
56+
.then(({ payload }) => expect(payload).to.be.eql(RETURN_VALUE));
57+
});
58+
59+
it(`forRoutes(TestController)`, () => {
60+
return app
61+
.inject({
62+
method: 'GET',
63+
url: '/test',
64+
})
65+
.then(({ payload }) => expect(payload).to.be.eql(SCOPED_VALUE));
66+
});
67+
68+
afterEach(async () => {
69+
await app.close();
70+
});
71+
});
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import {
2+
Controller,
3+
Get,
4+
INestApplication,
5+
MiddlewareConsumer,
6+
Module,
7+
} from '@nestjs/common';
8+
import { Test } from '@nestjs/testing';
9+
import * as request from 'supertest';
10+
import { ApplicationModule } from '../src/app.module';
11+
12+
const RETURN_VALUE = 'test';
13+
const SCOPED_VALUE = 'test_scoped';
14+
const WILDCARD_VALUE = 'test_wildcard';
15+
16+
@Controller()
17+
class TestController {
18+
@Get('test')
19+
test() {
20+
return RETURN_VALUE;
21+
}
22+
23+
@Get('tests/wildcard_nested')
24+
wildcard_nested() {
25+
return RETURN_VALUE;
26+
}
27+
}
28+
29+
@Module({
30+
imports: [ApplicationModule],
31+
controllers: [TestController],
32+
})
33+
class TestModule {
34+
configure(consumer: MiddlewareConsumer) {
35+
consumer
36+
.apply((req, res, next) => res.send(WILDCARD_VALUE))
37+
.forRoutes('tests/*')
38+
.apply((req, res, next) => res.send(SCOPED_VALUE))
39+
.forRoutes(TestController)
40+
.apply((req, res, next) => res.send(RETURN_VALUE))
41+
.forRoutes('*');
42+
}
43+
}
44+
45+
describe('Middleware', () => {
46+
let app: INestApplication;
47+
48+
beforeEach(async () => {
49+
app = (await Test.createTestingModule({
50+
imports: [TestModule],
51+
}).compile()).createNestApplication();
52+
53+
await app.init();
54+
});
55+
56+
it(`forRoutes(*)`, () => {
57+
return request(app.getHttpServer())
58+
.get('/hello')
59+
.expect(200, RETURN_VALUE);
60+
});
61+
62+
it(`forRoutes(TestController)`, () => {
63+
return request(app.getHttpServer())
64+
.get('/test')
65+
.expect(200, SCOPED_VALUE);
66+
});
67+
68+
it(`forRoutes(tests/*)`, () => {
69+
return request(app.getHttpServer())
70+
.get('/tests/wildcard')
71+
.expect(200, WILDCARD_VALUE);
72+
});
73+
74+
afterEach(async () => {
75+
await app.close();
76+
});
77+
});

0 commit comments

Comments
 (0)