-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.test.ts
282 lines (260 loc) · 7.63 KB
/
server.test.ts
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import {
assert,
assertEquals,
TextProtoReader,
BufReader,
Args,
Server,
} from './deps.ts'
import { appendReloadScript, encode, decode } from './utils/utils.ts'
import serve from './mod.ts'
let server: Deno.Process<Deno.RunOptions & { stdout: 'piped' }>
let port = 6060
const { test } = Deno
async function setup(args?: Args): Promise<void> {
const cmd = [
Deno.execPath(),
'run',
'--allow-read',
'--allow-net',
'--allow-write',
'./mod.ts',
'./demo',
]
args && args.c && cmd.push('-c')
args && args.n && cmd.push('-n')
args && args.l && cmd.push('-l')
args && args.before && cmd.push(`--before=${args.before}`)
if (args && args.p) {
port = args.p
} else {
port = 6060
}
cmd.push(`-p${port}`)
server = await Deno.run({
cmd,
stdout: 'piped',
stderr: 'null',
})
if (!server.stdout) throw Error
assert(server.stdout != null)
const r = new TextProtoReader(new BufReader(server.stdout))
await r.readLine()
}
async function tearDown(): Promise<void> {
server.close()
await Deno.readAll(server.stdout!)
server.stdout!.close()
}
test('should serve on given port', async (): Promise<void> => {
await setup({ _: ['./demo'], p: 7001 })
try {
const res = await fetch(`http://localhost:${port}`)
await res.text()
assertEquals(port, 7001)
assertEquals(res.status, 200)
assert(res.headers.has('content-type'))
} finally {
await tearDown()
}
})
test('handleRouteRequest should return index.html', async (): Promise<void> => {
await setup()
try {
const res = await fetch(`http://localhost:${port}`)
const file = await res.text()
assertEquals(res.status, 200)
assert(res.headers.has('content-type'))
assert(file.includes(`<div id="denoliver">`))
} finally {
await tearDown()
}
})
test('handleDirRequest should return a directory if list is true', async (): Promise<void> => {
await setup({ _: ['./demo'], l: true })
try {
const res = await fetch(`http://localhost:${port}/src`)
const file = await res.text()
assertEquals(res.status, 200)
assert(res.headers.has('content-type'))
assert(file.includes(`<title>denoliver - /src</title>`))
assert(file.includes(`test.md`))
} finally {
await tearDown()
}
})
test('index.html should contain reload script', async (): Promise<void> => {
await setup()
try {
const res = await fetch(`http://localhost:${port}`)
const file = await res.text()
assertEquals(res.status, 200)
assert(res.headers.has('content-type'))
assert(file.includes(appendReloadScript('', 6060, '127.0.0.1', false)))
} finally {
await tearDown()
}
})
test('given no reload option index.html should not contain reload script', async (): Promise<void> => {
await setup({ _: ['./demo'], n: true })
try {
const res = await fetch(`http://localhost:${port}`)
const file = await res.text()
assertEquals(res.status, 200)
assert(res.headers.has('content-type'))
assert(!file.includes(appendReloadScript('', 6060, '127.0.0.1', false)))
} finally {
await tearDown()
}
})
test('/any/other/route should return index.html', async (): Promise<void> => {
await setup()
try {
const res = await fetch(`http://localhost:${port}/any/other/route`)
const file = await res.text()
assertEquals(res.status, 200)
assert(res.headers.has('content-type'))
assert(file.includes(`<div id="denoliver">`))
} finally {
await tearDown()
}
})
test('/style.css should return style.css from ./demo', async (): Promise<void> => {
await setup()
try {
const res = await fetch(`http://localhost:${port}/style.css`)
const file = await res.text()
assertEquals(res.status, 200)
assert(res.headers.has('content-type'))
const localFile = new TextDecoder().decode(
await Deno.readFile('./demo/style.css')
)
assert(file, localFile)
} finally {
await tearDown()
}
})
test('given a path to file not found should return 404', async (): Promise<void> => {
await setup()
try {
const res = await fetch(`http://localhost:${port}/does-not-exist.js`)
const file = await res.text()
assertEquals(res.status, 404)
assertEquals(res.statusText, 'Not Found')
assert(file.includes('<title>404</title>'))
} finally {
await tearDown()
}
})
test('when cors enabled response should have access control header', async (): Promise<void> => {
await setup({ _: ['./demo'], c: true })
try {
const res = await fetch(`http://localhost:${port}`)
const file = await res.text()
assertEquals(res.status, 200)
assert(res.headers.has('content-type'))
assert(res.headers.has('access-control-allow-origin'))
assert(file.includes(`<div id="denoliver">`))
} finally {
await tearDown()
}
})
/* Programmatic use */
let denoliver: Server
test({
name: 'should be able to be used programmaticaly',
fn: async (): Promise<void> => {
try {
denoliver = await serve({ root: './demo', cors: true })
const res = await fetch(`http://localhost:8080`)
const file = await res.text()
assertEquals(res.status, 200)
assert(res.headers.has('content-type'))
assert(res.headers.has('access-control-allow-origin'))
assert(file.includes(`<div id="denoliver">`))
} finally {
await denoliver.close()
}
},
})
test({
name: 'options can be passed to the serve function',
fn: async (): Promise<void> => {
try {
denoliver = await serve({ root: './demo', cors: true, port: 4000 })
const res = await fetch(`http://localhost:4000`)
const file = await res.text()
assertEquals(res.status, 200)
assert(res.headers.has('content-type'))
assert(res.headers.has('access-control-allow-origin'))
assert(file.includes(`<div id="denoliver">`))
} finally {
await denoliver.close()
}
},
})
test({
name: 'options are read from config file if it exists',
fn: async (): Promise<void> => {
const config = `
{
"root": "./demo",
"disableReload": false,
"silent": false,
"port": 6060,
"debug": false,
"secure": false,
"cors": true,
"entryPoint": "index.html"
}
`
const encoded = encode(config)
await Deno.writeFile(`./demo/denoliver.json`, encoded)
await setup()
try {
const res = await fetch(`http://localhost:6060`)
const text = await res.text()
assertEquals(res.status, 200)
assert(res.headers.has('content-type'))
assert(res.headers.has('access-control-allow-origin'))
assert(text.includes(`<div id="denoliver">`))
} finally {
await Deno.remove(`./demo/denoliver.json`)
await tearDown()
}
},
})
test('before intercepts requests', async (): Promise<void> => {
await setup({ _: ['./demo'], l: true, before: 'before.ts' })
try {
const res = await fetch(`http://localhost:${port}`)
await res.text()
assertEquals(res.status, 200)
} finally {
await server.close()
const s = decode(await server.output())
assert(s.includes('Before Request Interceptor'))
}
})
// Re-enable this test when this has been resolved:
// https://github.com/denoland/deno/issues/5760
// test('should be able to be used programmaticaly using HTTPS', async (): Promise<
// void
// > => {
// try {
// denoliver = await serve({
// root: './demo',
// cors: true,
// secure: true,
// port: 6062,
// })
// const res = await fetch(`https://localhost:6062`)
// const file = await res.text()
// assertEquals(res.status, 200)
// assert(res.headers.has('content-type'))
// assert(res.headers.has('access-control-allow-origin'))
// assert(file.includes(`<div id="denoliver">`))
// } finally {
// denoliver.close()
// }
// })