-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
78 lines (73 loc) · 1.77 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
const Tom = require('test-runner').Tom
const a = require('assert').strict
const fetch = require('node-fetch')
const Lws = require('lws')
const BasicAuth = require('./')
const tom = module.exports = new Tom()
tom.test('no config', async function () {
const port = 9000 + this.index
class One {
middleware () {
return function (ctx, next) {
ctx.body = 'one'
next()
}
}
}
const lws = Lws.create({
port,
stack: [ BasicAuth, One ]
})
const response = await fetch(`http://localhost:${port}/`)
lws.server.close()
a.equal(response.status, 200)
const body = await response.text()
a.equal(body, 'one')
})
tom.test('username and password: unauthorised', async function () {
const port = 9000 + this.index
class One {
middleware () {
return function (ctx, next) {
ctx.body = 'one'
next()
}
}
}
const lws = Lws.create({
port,
stack: [ BasicAuth, One ],
authUser: 'user',
authPass: 'password'
})
const response = await fetch(`http://localhost:${port}/`)
lws.server.close()
a.equal(response.status, 401)
})
tom.test('username and password: authorised', async function () {
const port = 9000 + this.index
class One {
middleware () {
return function (ctx, next) {
ctx.body = 'one'
next()
}
}
}
const lws = Lws.create({
port,
stack: [ BasicAuth, One ],
authUser: 'user',
authPass: 'password'
})
const response = await fetch(`http://user:password@localhost:${port}/`)
lws.server.close()
a.equal(response.status, 200)
const body = await response.text()
a.equal(body, 'one')
})
tom.test('description and optionDefinitions', function () {
const plugin = new BasicAuth()
plugin.description()
plugin.optionDefinitions()
})