Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Improve middleware route exclusion for dynamic endpoints #14194

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 147 additions & 60 deletions integration/hello-world/e2e/exclude-middleware-fastify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,80 +50,167 @@ class TestController {
return RETURN_VALUE;
}

@Get('overview/all')
overviewAll() {
return RETURN_VALUE;
}

@Get('overview/:id')
overviewById() {
return RETURN_VALUE;
}
}

@Module({
imports: [AppModule],
controllers: [TestController],
})
class TestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply((req, res, next) => res.end(MIDDLEWARE_VALUE))
.exclude('test', 'overview/:id', 'wildcard/(.*)', {
path: 'middleware',
method: RequestMethod.POST,
})
.forRoutes('*');
@Get('multiple/exclude')
multipleExclude() {
return RETURN_VALUE;
}
}

describe('Exclude middleware (fastify)', () => {
let app: INestApplication;

beforeEach(async () => {
app = (
await Test.createTestingModule({
imports: [TestModule],
}).compile()
).createNestApplication<NestFastifyApplication>(new FastifyAdapter());

await app.init();
await app.getHttpAdapter().getInstance().ready();
});

it(`should exclude "/test" endpoint`, () => {
return request(app.getHttpServer()).get('/test').expect(200, RETURN_VALUE);
});

it(`should not exclude "/test/test" endpoint`, () => {
return request(app.getHttpServer())
.get('/test/test')
.expect(200, MIDDLEWARE_VALUE);
});
function createTestModule<T = any>(
forRoutesArg: string | (new (...args: any[]) => T),
) {
@Module({
imports: [AppModule],
controllers: [TestController],
})
class TestModuleBase {
configure(consumer: MiddlewareConsumer) {
consumer
.apply((req, res, next) => res.end(MIDDLEWARE_VALUE))
.exclude('test', 'overview/:id', 'wildcard/(.*)', {
path: 'middleware',
method: RequestMethod.POST,
})
.exclude('multiple/exclude')
.forRoutes(forRoutesArg);
}
}

it(`should not exclude "/test2" endpoint`, () => {
return request(app.getHttpServer())
.get('/test2')
.expect(200, MIDDLEWARE_VALUE);
});
return TestModuleBase;
}

it(`should run middleware for "/middleware" endpoint`, () => {
return request(app.getHttpServer())
.get('/middleware')
.expect(200, MIDDLEWARE_VALUE);
});
const TestModule = createTestModule('*');
const TestModule2 = createTestModule(TestController);

it(`should exclude POST "/middleware" endpoint`, () => {
return request(app.getHttpServer())
.post('/middleware')
.expect(201, RETURN_VALUE);
});
describe('Exclude middleware (fastify)', () => {
let app: INestApplication;

it(`should exclude "/overview/:id" endpoint (by param)`, () => {
return request(app.getHttpServer())
.get('/overview/1')
.expect(200, RETURN_VALUE);
describe('forRoutes is *', () => {
beforeEach(async () => {
app = (
await Test.createTestingModule({
imports: [TestModule],
}).compile()
).createNestApplication<NestFastifyApplication>(new FastifyAdapter());

await app.init();
await app.getHttpAdapter().getInstance().ready();
});

it(`should exclude "/test" endpoint`, () => {
return request(app.getHttpServer())
.get('/test')
.expect(200, RETURN_VALUE);
});

it(`should not exclude "/test2" endpoint`, () => {
return request(app.getHttpServer())
.get('/test2')
.expect(200, MIDDLEWARE_VALUE);
});

it(`should run middleware for "/middleware" endpoint`, () => {
return request(app.getHttpServer())
.get('/middleware')
.expect(200, MIDDLEWARE_VALUE);
});

it(`should exclude POST "/middleware" endpoint`, () => {
return request(app.getHttpServer())
.post('/middleware')
.expect(201, RETURN_VALUE);
});

it(`should exclude "/overview/:id" endpoint (by param)`, () => {
return request(app.getHttpServer())
.get('/overview/1')
.expect(200, RETURN_VALUE);
});

it(`should exclude "/wildcard/overview" endpoint (by wildcard)`, () => {
return request(app.getHttpServer())
.get('/wildcard/overview')
.expect(200, RETURN_VALUE);
});

it(`should exclude "/multiple/exclude" endpoint`, () => {
return request(app.getHttpServer())
.get('/multiple/exclude')
.expect(200, RETURN_VALUE);
});
});

it(`should exclude "/wildcard/overview" endpoint (by wildcard)`, () => {
return request(app.getHttpServer())
.get('/wildcard/overview')
.expect(200, RETURN_VALUE);
describe('forRoutes is Controller', () => {
let app: INestApplication;

beforeEach(async () => {
app = (
await Test.createTestingModule({
imports: [TestModule2],
}).compile()
).createNestApplication<NestFastifyApplication>(new FastifyAdapter());

await app.init();
await app.getHttpAdapter().getInstance().ready();
});

it(`should exclude "/test" endpoint`, () => {
return request(app.getHttpServer())
.get('/test')
.expect(200, RETURN_VALUE);
});

it(`should not exclude "/test2" endpoint`, () => {
return request(app.getHttpServer())
.get('/test2')
.expect(200, MIDDLEWARE_VALUE);
});

it(`should run middleware for "/middleware" endpoint`, () => {
return request(app.getHttpServer())
.get('/middleware')
.expect(200, MIDDLEWARE_VALUE);
});

it(`should exclude POST "/middleware" endpoint`, () => {
return request(app.getHttpServer())
.post('/middleware')
.expect(201, RETURN_VALUE);
});

it(`should exclude "/overview/:id" endpoint (by param)`, () => {
return request(app.getHttpServer())
.get('/overview/1')
.expect(200, RETURN_VALUE);
});

it(`should not exclude "/overvview/all" endpoint`, () => {
return request(app.getHttpServer())
.get('/overview/all')
.expect(200, MIDDLEWARE_VALUE);
});

it(`should exclude "/wildcard/overview" endpoint (by wildcard)`, () => {
return request(app.getHttpServer())
.get('/wildcard/overview')
.expect(200, MIDDLEWARE_VALUE);
});

it(`should exclude "/multiple/exclude" endpoint`, () => {
return request(app.getHttpServer())
.get('/multiple/exclude')
.expect(200, RETURN_VALUE);
});
});

afterEach(async () => {
Expand Down
Loading
Loading