Skip to content

Commit

Permalink
fix(core): add integration test for improving middleware route exclusion
Browse files Browse the repository at this point in the history
  • Loading branch information
sapenlei committed Nov 29, 2024
1 parent 20af885 commit 9704336
Show file tree
Hide file tree
Showing 2 changed files with 292 additions and 122 deletions.
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

0 comments on commit 9704336

Please sign in to comment.