| 8 | const testValidPaths = new Set(["example.txt", "index.html"]); |
| 9 | |
| 10 | export const staticFilesTests = async () => { |
| 11 | const endpoint = await staticFiles({ |
| 12 | dir: "./", |
| 13 | route: "/route", |
| 14 | validPathsProvider: () => Promise.resolve(testValidPaths), |
| 15 | fileReader: () => Promise.resolve(testFileContent), |
| 16 | contentTypeResolver: () => testContentType, |
| 17 | headersProvider: () => testHeaders, |
| 18 | }); |
| 19 | |
| 20 | { |
| 21 | const result = await endpoint.accept({ url: "/wrong" } as any); |
| 22 | strictEqual(!result, true, "Should not accept wrong route"); |
| 23 | } |
| 24 | |
| 25 | { |
| 26 | const result = await endpoint.accept({ url: "/route/other.txt" } as any); |
| 27 | strictEqual(!result, true, "Should not accept invalid path"); |
| 28 | } |
| 29 | |
| 30 | { |
| 31 | const result = await endpoint.accept({ url: "/route" } as any); |
| 32 | strictEqual(result, "index.html", "Should rewrite to index.html"); |
| 33 | } |
| 34 | |
| 35 | { |
| 36 | const request = { url: "/route/example.txt" } as any; |
| 37 | const acceptResult = await endpoint.accept(request); |
| 38 | strictEqual(acceptResult, "example.txt", "Should accept correct route"); |
| 39 | |
| 40 | const handleResult = await endpoint.handle(acceptResult); |
| 41 | deepStrictEqual( |
| 42 | handleResult, |
| 43 | { |
| 44 | status: OK, |
| 45 | body: testFileContent, |
| 46 | headers: { |
| 47 | "Content-Type": testContentType, |
| 48 | ...testHeaders, |
| 49 | }, |
| 50 | }, |
| 51 | "Should handle valid request correctly", |
| 52 | ); |
| 53 | } |
| 54 | }; |