|
| 1 | +import { assert, assertEq } from 'functionalscript/fs/asserts/module.f.js' |
| 2 | +import { utf8, utf8ToString } from 'functionalscript/fs/text/module.f.js' |
| 3 | +import { vec8 } from 'functionalscript/fs/types/bit_vec/module.f.js' |
| 4 | +import type { IncomingMessage } from 'functionalscript/fs/effects/node/module.f.js' |
| 5 | +import { virtual, emptyState } from 'functionalscript/fs/effects/node/virtual/module.f.js' |
| 6 | +import { listener } from './server.f.ts' |
| 7 | + |
| 8 | +const request = (url: string): IncomingMessage => ({ |
| 9 | + method: 'GET', |
| 10 | + url, |
| 11 | + headers: {}, |
| 12 | + body: vec8(0n), |
| 13 | +}) |
| 14 | + |
| 15 | +const runRequest = (url: string, root = emptyState.root) => |
| 16 | + virtual({ ...emptyState, root })(listener(request(url))) |
| 17 | + |
| 18 | +const bodyText = (url: string, root = emptyState.root) => { |
| 19 | + const [state, response] = runRequest(url, root) |
| 20 | + return [state, response, utf8ToString(response.body)] as const |
| 21 | +} |
| 22 | + |
| 23 | +const includes = (s: string) => (part: string) => { |
| 24 | + assert(s.includes(part), [part, s]) |
| 25 | +} |
| 26 | + |
| 27 | +export const proof = { |
| 28 | + servesFiles: () => { |
| 29 | + const file = utf8('body { color: green; }\n') |
| 30 | + const [state, response] = runRequest('/main.css', { 'main.css': file }) |
| 31 | + |
| 32 | + assertEq(response.status, 200) |
| 33 | + assertEq(utf8ToString(response.body), 'body { color: green; }\n') |
| 34 | + includes(state.stdout)('reading ./main.css\n') |
| 35 | + includes(state.stdout)('served: 23 bytes\n') |
| 36 | + }, |
| 37 | + ignoresQueryString: () => { |
| 38 | + const file = utf8('ok') |
| 39 | + const [_, response] = runRequest('/main.css?cache=bust', { 'main.css': file }) |
| 40 | + |
| 41 | + assertEq(response.status, 200) |
| 42 | + assertEq(utf8ToString(response.body), 'ok') |
| 43 | + }, |
| 44 | + rendersDirectoryListing: () => { |
| 45 | + const [state, response, body] = bodyText('/docs', { |
| 46 | + docs: { |
| 47 | + 'a.txt': utf8('A'), |
| 48 | + nested: {}, |
| 49 | + }, |
| 50 | + }) |
| 51 | + |
| 52 | + assertEq(response.status, 200) |
| 53 | + includes(body)('<link rel="stylesheet" href="/main.css">') |
| 54 | + includes(body)('<pre><a href="/docs/a.txt">a.txt</a>\n<a href="/docs/nested">nested/</a>\n</pre>') |
| 55 | + includes(state.stdout)('reading ./docs\n') |
| 56 | + includes(state.stdout)('served: ') |
| 57 | + }, |
| 58 | + missingPaths404: () => { |
| 59 | + const [state, response] = runRequest('/missing') |
| 60 | + |
| 61 | + assertEq(response.status, 404) |
| 62 | + assertEq(utf8ToString(response.body), '404 not found') |
| 63 | + assertEq(state.stdout, 'reading ./missing\nserved: 13 bytes\n') |
| 64 | + }, |
| 65 | +} |
0 commit comments