-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
81 lines (75 loc) · 2.2 KB
/
test.js
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
const Tom = require('test-runner').Tom
const Redirect = require('./')
const Lws = require('lws')
const a = require('assert').strict
const fetch = require('node-fetch')
const tom = module.exports = new Tom()
tom.test('http -> https', async function () {
const port = 8000 + this.index
const lws = Lws.create({
stack: Redirect,
port,
redirect: 'http -> https'
})
const response = await fetch(`http://localhost:${port}/one`, { redirect: 'manual' })
lws.server.close()
a.equal(response.status, 302)
a.equal(response.headers.get('location'), 'https://localhost:8001/one')
})
tom.test('port', async function () {
const port = 8000 + this.index
const lws = Lws.create({
stack: Redirect,
port,
redirect: `:${port} -> :9000`
})
const response = await fetch(`http://localhost:${port}/one`, { redirect: 'manual' })
lws.server.close()
a.equal(response.status, 302)
a.equal(response.headers.get('location'), 'http://localhost:9000/one')
})
tom.test('schema and port', async function () {
const port = 8000 + this.index
const lws = Lws.create({
stack: Redirect,
port,
redirect: [
'http -> https',
`:${port} -> :9000`
]
})
const response = await fetch(`http://localhost:${port}/one`, { redirect: 'manual' })
lws.server.close()
a.equal(response.status, 302)
a.equal(response.headers.get('location'), 'https://localhost:9000/one')
})
tom.test('no redirect match', async function () {
const port = 8000 + this.index
const lws = Lws.create({
stack: Redirect,
port,
redirect: 'something -> anotherthing'
})
const response = await fetch(`http://localhost:${port}/one`, { redirect: 'manual' })
lws.server.close()
a.equal(response.status, 404)
})
tom.test('no redirect match, next middleware invoked', async function () {
const port = 8000 + this.index
class One {
middleware () {
return async (ctx, next) => {
ctx.response.status = 405
await next()
}
}
}
const lws = Lws.create({
stack: [ Redirect, One ],
port,
redirect: 'something -> anotherthing'
})
const response = await fetch(`http://localhost:${port}/one`, { redirect: 'manual' })
lws.server.close()
a.equal(response.status, 405)
})