forked from request/request
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-bearer-auth.js
187 lines (170 loc) Β· 4.27 KB
/
test-bearer-auth.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
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
'use strict'
var assert = require('assert')
var http = require('http')
var request = require('../index')
var tape = require('tape')
var numBearerRequests = 0
var bearerServer
tape('setup', function (t) {
bearerServer = http.createServer(function (req, res) {
numBearerRequests++
var ok
if (req.headers.authorization) {
if (req.headers.authorization === 'Bearer theToken') {
ok = true
} else {
// Bad auth header, don't send back WWW-Authenticate header
ok = false
}
} else {
// No auth header, send back WWW-Authenticate header
ok = false
res.setHeader('www-authenticate', 'Bearer realm="Private"')
}
if (req.url === '/post/') {
var expectedContent = 'data_key=data_value'
req.on('data', function (data) {
assert.equal(data, expectedContent)
})
assert.equal(req.method, 'POST')
assert.equal(req.headers['content-length'], '' + expectedContent.length)
assert.equal(req.headers['content-type'], 'application/x-www-form-urlencoded')
}
if (ok) {
res.end('ok')
} else {
res.statusCode = 401
res.end('401')
}
}).listen(0, function () {
bearerServer.url = 'http://localhost:' + this.address().port
t.end()
})
})
tape('bearer auth', function (t) {
request({
'method': 'GET',
'uri': bearerServer.url + '/test/',
'auth': {
'bearer': 'theToken',
'sendImmediately': false
}
}, function (error, res, body) {
t.error(error)
t.equal(res.statusCode, 200)
t.equal(numBearerRequests, 2)
t.end()
})
})
tape('bearer auth with default sendImmediately', function (t) {
// If we don't set sendImmediately = false, request will send bearer auth
request({
'method': 'GET',
'uri': bearerServer.url + '/test2/',
'auth': {
'bearer': 'theToken'
}
}, function (error, res, body) {
t.error(error)
t.equal(res.statusCode, 200)
t.equal(numBearerRequests, 3)
t.end()
})
})
tape('', function (t) {
request({
'method': 'POST',
'form': { 'data_key': 'data_value' },
'uri': bearerServer.url + '/post/',
'auth': {
'bearer': 'theToken',
'sendImmediately': false
}
}, function (error, res, body) {
t.error(error)
t.equal(res.statusCode, 200)
t.equal(numBearerRequests, 5)
t.end()
})
})
tape('using .auth, sendImmediately = false', function (t) {
request
.get(bearerServer.url + '/test/')
.auth(null, null, false, 'theToken')
.on('response', function (res) {
t.equal(res.statusCode, 200)
t.equal(numBearerRequests, 7)
t.end()
})
})
tape('using .auth, sendImmediately = true', function (t) {
request
.get(bearerServer.url + '/test/')
.auth(null, null, true, 'theToken')
.on('response', function (res) {
t.equal(res.statusCode, 200)
t.equal(numBearerRequests, 8)
t.end()
})
})
tape('bearer is a function', function (t) {
request({
'method': 'GET',
'uri': bearerServer.url + '/test/',
'auth': {
'bearer': function () { return 'theToken' },
'sendImmediately': false
}
}, function (error, res, body) {
t.error(error)
t.equal(res.statusCode, 200)
t.equal(numBearerRequests, 10)
t.end()
})
})
tape('bearer is a function, path = test2', function (t) {
// If we don't set sendImmediately = false, request will send bearer auth
request({
'method': 'GET',
'uri': bearerServer.url + '/test2/',
'auth': {
'bearer': function () { return 'theToken' }
}
}, function (error, res, body) {
t.error(error)
t.equal(res.statusCode, 200)
t.equal(numBearerRequests, 11)
t.end()
})
})
tape('no auth method', function (t) {
request({
'method': 'GET',
'uri': bearerServer.url + '/test2/',
'auth': {
'bearer': undefined
}
}, function (error, res, body) {
t.equal(error.message, 'no auth mechanism defined')
t.end()
})
})
tape('null bearer', function (t) {
request({
'method': 'GET',
'uri': bearerServer.url + '/test2/',
'auth': {
'bearer': null
}
}, function (error, res, body) {
t.error(error)
t.equal(res.statusCode, 401)
t.equal(numBearerRequests, 13)
t.end()
})
})
tape('cleanup', function (t) {
bearerServer.close(function () {
t.end()
})
})