1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-06 00:07:44 +01:00
unleash.unleash/src/test/e2e/api/admin/conditional-middleware.e2e.test.ts

94 lines
2.2 KiB
TypeScript
Raw Normal View History

import express from 'express';
import { conditionalMiddleware } from '../../../../lib/middleware/conditional-middleware';
import supertest from 'supertest';
test('disabled middleware should not block paths that use the same path', async () => {
const app = express();
const path = '/api/admin/projects';
app.use(
path,
conditionalMiddleware(
() => false,
(req, res) => {
res.send({ changeRequest: 'hello' });
},
),
);
app.get(path, (req, res) => {
res.json({ projects: [] });
});
await supertest(app)
.get('/api/admin/projects')
.expect(200, { projects: [] });
});
test('should return 404 when path is not enabled', async () => {
const app = express();
const path = '/api/admin/projects';
app.use(
`${path}/change-requests`,
conditionalMiddleware(
() => false,
(req, res) => {
res.send({ changeRequest: 'hello' });
},
),
);
app.get(path, (req, res) => {
res.json({ projects: [] });
});
await supertest(app).get('/api/admin/projects/change-requests').expect(404);
});
test('should respect ordering of endpoints', async () => {
const app = express();
const path = '/api/admin/projects';
app.use(
path,
conditionalMiddleware(
() => true,
(req, res) => {
res.json({ name: 'Request changes' });
},
),
);
app.get(path, (req, res) => {
res.json({ projects: [] });
});
await supertest(app)
.get('/api/admin/projects')
.expect(200, { name: 'Request changes' });
});
test('disabled middleware should not block paths that use the same basepath', async () => {
const app = express();
const path = '/api/admin/projects';
app.use(
`${path}/change-requests`,
conditionalMiddleware(
() => false,
(req, res) => {
res.json({ name: 'Request changes' });
},
),
);
app.get(path, (req, res) => {
res.json({ projects: [] });
});
await supertest(app)
.get('/api/admin/projects')
.expect(200, { projects: [] });
});