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