/
middleware.js
117 lines (100 loc) · 2.86 KB
/
middleware.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
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
var t = require('assert')
var request = require('request-compose').extend({
Request: {cookie: require('request-cookie').Request},
Response: {cookie: require('request-cookie').Response},
}).client
var port = {oauth2: 5001, app: 5002}
var url = {
oauth2: (path) => `http://localhost:${port.oauth2}${path}`,
app: (path) => `http://localhost:${port.app}${path}`,
}
var provider = require('./util/provider')
var client = require('./util/client')
describe('middleware', () => {
var server = {oauth2: null}
before(async () => {
server.oauth2 = await provider.oauth2(port.oauth2)
})
after((done) => {
server.oauth2.close(done)
})
describe('path prefix', () => {
var server
var config = {
defaults: {
protocol: 'http', host: `localhost:${port.app}`, callback: '/',
path: '/prefix',
},
oauth2: {
authorize_url: url.oauth2('/authorize_url'),
access_url: url.oauth2('/access_url'),
oauth: 2,
}
}
;['express-prefix', 'koa-prefix', 'hapi-prefix'].forEach((consumer) => {
describe(consumer, () => {
before(async () => {
var obj = await client[consumer](config, port.app)
server = obj.server
})
after((done) => {
server[/express|koa/.test(consumer) ? 'close' : 'stop'](done)
})
it('success', async () => {
var {body: {response}} = await request({
url: url.app('/prefix/connect/oauth2'),
cookie: {},
})
t.deepEqual(response, {
access_token: 'token',
refresh_token: 'refresh',
raw: {
access_token: 'token',
refresh_token: 'refresh',
expires_in: '3600'
}
})
})
})
})
})
describe('third-party middlewares', () => {
var server
var config = {
defaults: {
protocol: 'http', host: `localhost:${port.app}`, callback: '/',
},
oauth2: {
authorize_url: url.oauth2('/authorize_url'),
access_url: url.oauth2('/access_url'),
oauth: 2,
}
}
;['koa', 'koa-mount', 'express', 'express-cookie'].forEach((consumer) => {
describe(consumer, () => {
before(async () => {
var obj = await client[consumer](config, port.app)
server = obj.server
})
after((done) => {
server.close(done)
})
it('success', async () => {
var {body: {response}} = await request({
url: url.app('/connect/oauth2'),
cookie: {},
})
t.deepEqual(response, {
access_token: 'token',
refresh_token: 'refresh',
raw: {
access_token: 'token',
refresh_token: 'refresh',
expires_in: '3600'
}
})
})
})
})
})
})