Switch main's route handlers to be static functions (#2712)

This commit is contained in:
Sebastian Malton authored and GitHub committed 2021-05-12 08:08:42 -04:00
1 parent b373411a16
commit f4ff28e51c
11 files changed
+173 -196

No files matched your search

+19 -17
View File
@@ -1,40 +1,42 @@
import { Router } from "../router";
const staticRoot = __dirname;
class TestRouter extends Router {
protected resolveStaticRootPath() {
return staticRoot;
}
}
describe("Router", () => {
it("blocks path traversal attacks", async () => {
const router = new TestRouter();
const res = {
const response: any = {
statusCode: 200,
end: jest.fn()
};
await router.handleStaticFile("../index.ts", res as any, {} as any, 0);
await (Router as any).handleStaticFile({
params: {
path: "../index.ts",
},
response,
raw: {},
});
expect(res.statusCode).toEqual(404);
expect(response.statusCode).toEqual(404);
});
it("serves files under static root", async () => {
const router = new TestRouter();
const res = {
const response: any = {
statusCode: 200,
write: jest.fn(),
setHeader: jest.fn(),
end: jest.fn()
};
const req = {
const req: any = {
url: ""
};
await router.handleStaticFile("router.test.ts", res as any, req as any, 0);
await (Router as any).handleStaticFile({
params: {
path: "router.test.ts",
},
response,
raw: { req },
});
expect(res.statusCode).toEqual(200);
expect(response.statusCode).toEqual(200);
});
});