backend-app-api: add routing test for DefaultRootHttpRouter

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-04-06 11:57:11 +02:00
parent 9d74e68f79
commit 40548a634d
@@ -14,6 +14,8 @@
* limitations under the License.
*/
import express from 'express';
import request from 'supertest';
import { DefaultRootHttpRouter } from './DefaultRootHttpRouter';
describe('DefaultRootHttpRouter', () => {
@@ -54,4 +56,37 @@ describe('DefaultRootHttpRouter', () => {
'indexPath option may not be an empty string',
);
});
it('will always prioritize non-index paths', async () => {
const router = DefaultRootHttpRouter.create({ indexPath: '/x' });
const app = express();
app.use(router.handler());
const routerX = express.Router();
routerX.get('/a', (_req, res) => res.status(201).end());
const routerY = express.Router();
routerY.get('/a', (_req, res) => res.status(202).end());
await request(app).get('/').expect(404);
await request(app).get('/a').expect(404);
await request(app).get('/x/a').expect(404);
await request(app).get('/y/a').expect(404);
router.use('/x', routerX);
await request(app).get('/').expect(404);
await request(app).get('/a').expect(201);
await request(app).get('/x/a').expect(201);
await request(app).get('/y/a').expect(404);
router.use('/y', routerY);
await request(app).get('/').expect(404);
await request(app).get('/a').expect(201);
await request(app).get('/x/a').expect(201);
await request(app).get('/y/a').expect(202);
expect('test').toBe('test');
});
});