-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.test.js
328 lines (288 loc) · 8.43 KB
/
index.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
const nock = require('nock');
const axios = require('axios');
const Promise = require('bluebird');
const plugin = require('./index');
describe('axios-token-interceptor', () => {
describe('tokens', () => {
test('should support promises', () => {
const options = {
getToken: () => Promise.resolve('abc')
};
return plugin(options)({ headers: {} }).then((config) => {
expect(config).toEqual({
headers: {
Authorization: 'Bearer abc'
}
});
});
});
test('should support strings', () => {
const options = {
getToken: () => 'def'
};
return plugin(options)({ headers: {} }).then((config) => {
expect(config).toEqual({
headers: {
Authorization: 'Bearer def'
}
});
});
});
test('should support static tokens', () => {
const options = {
token: 'my-token'
};
return plugin(options)({ headers: {} }).then((config) => {
expect(config).toEqual({
headers: {
Authorization: 'Bearer my-token'
}
});
});
});
test('should support the cache provider', () => {
const cache = plugin.tokenCache(() => Promise.resolve('abc'), {
maxAge: 100
});
const options = {
getToken: cache
};
return plugin(options)({ headers: {} }).then((config) => {
expect(config).toEqual({
headers: {
Authorization: 'Bearer abc'
}
});
});
});
});
describe('header', () => {
test('should set a custom header', () => {
const options = {
header: 'X-Api-Key',
getToken: () => Promise.resolve('abc')
};
return plugin(options)({ headers: {} }).then((config) => {
expect(config).toEqual({
headers: {
'X-Api-Key': 'Bearer abc'
}
});
});
});
test('should set a custom header value', () => {
const options = {
headerFormatter: (token) => `Token:${token}`,
getToken: () => Promise.resolve('abc')
};
return plugin(options)({ headers: {} }).then((config) => {
expect(config).toEqual({
headers: {
Authorization: 'Token:abc'
}
});
});
});
});
describe('cache', () => {
test('should use the maxAge setting', () => {
const getToken = jest
.fn()
.mockReturnValueOnce(Promise.resolve('token1'))
.mockReturnValueOnce(Promise.resolve('token2'));
const options = {
getToken: plugin.tokenCache(getToken, {
maxAge: 100
})
};
const interceptor = plugin(options);
return interceptor({ headers: {} })
.then((config) => {
expect(config).toEqual({
headers: {
Authorization: 'Bearer token1'
}
});
})
.then(() => Promise.delay(50))
.then(() => interceptor({ headers: {} }))
.then((config) => {
expect(config).toEqual({
headers: {
Authorization: 'Bearer token1'
}
});
})
.then(() => Promise.delay(100))
.then(() => interceptor({ headers: {} }))
.then((config) => {
expect(config).toEqual({
headers: {
Authorization: 'Bearer token2'
}
});
});
});
test('should use the getMaxAge setting', () => {
const getToken = jest
.fn()
.mockReturnValueOnce(
Promise.resolve({ access_token: 'token1', expires_in: 50 })
)
.mockReturnValueOnce(
Promise.resolve({ access_token: 'token2', expires_in: 100 })
);
const options = {
headerFormatter: (token) => `Bearer ${token.access_token}`,
getToken: plugin.tokenCache(getToken, {
getMaxAge: (token) => token.expires_in
})
};
const interceptor = plugin(options);
return interceptor({ headers: {} })
.then((config) => {
expect(config).toEqual({
headers: {
Authorization: 'Bearer token1'
}
});
})
.then(() => Promise.delay(20))
.then(() => interceptor({ headers: {} }))
.then((config) => {
expect(config).toEqual({
headers: {
Authorization: 'Bearer token1'
}
});
})
.then(() => Promise.delay(40))
.then(() => interceptor({ headers: {} }))
.then((config) => {
expect(config).toEqual({
headers: {
Authorization: 'Bearer token2'
}
});
});
});
test('should support reset', () => {
const getToken = jest
.fn()
.mockReturnValueOnce(
Promise.resolve({ access_token: 'token1', expires_in: 50 })
)
.mockReturnValueOnce(
Promise.resolve({ access_token: 'token2', expires_in: 100 })
);
const cache = plugin.tokenCache(getToken, {
getMaxAge: (token) => token.expires_in
});
const options = {
headerFormatter: (token) => `Bearer ${token.access_token}`,
getToken: cache
};
const interceptor = plugin(options);
return interceptor({ headers: {} })
.then((config) => {
expect(config).toEqual({
headers: {
Authorization: 'Bearer token1'
}
});
cache.reset();
})
.then(() => interceptor({ headers: {} }))
.then((config) => {
expect(config).toEqual({
headers: {
Authorization: 'Bearer token2'
}
});
});
});
test('should not make concurrent calls for a cache miss', () => {
const getToken = jest.fn().mockReturnValueOnce(Promise.resolve('token1'));
const options = {
getToken: plugin.tokenCache(getToken, {
maxAge: 100
})
};
const interceptor = plugin(options);
return Promise.all([
interceptor({ headers: {} }),
interceptor({ headers: {} })
]).spread((config1, config2) => {
expect(config1).toEqual({
headers: {
Authorization: 'Bearer token1'
}
});
expect(config2).toEqual({
headers: {
Authorization: 'Bearer token1'
}
});
});
});
test('should handle errors correctly', () => {
const getToken = () => Promise.reject(new Error('unable to fetch token'));
const options = {
getToken: plugin.tokenCache(getToken, {
maxAge: 100
})
};
const interceptor = plugin(options);
return interceptor({ headers: {} }).catch((err) => {
expect(err.message).toEqual('unable to fetch token');
});
});
});
describe('axios', () => {
test('GET: should send the header to the api', () => {
const options = {
getToken: () => Promise.resolve('abc')
};
const instance = axios.create({
baseURL: 'https://api.example.com'
});
instance.interceptors.request.use(plugin(options));
const request = nock('https://api.example.com')
.matchHeader('Authorization', 'Bearer abc')
.get('/foo')
.reply(200);
return instance.get('/foo').then(() => {
expect(request.isDone()).toBeTruthy();
});
});
test('POST: should send the header to the api', () => {
const options = {
getToken: () => Promise.resolve('abc')
};
const instance = axios.create({
baseURL: 'https://api.example.com'
});
instance.interceptors.request.use(plugin(options));
const request = nock('https://api.example.com')
.matchHeader('Authorization', 'Bearer abc')
.post('/foo')
.reply(200);
return instance.post('/foo').then(() => {
expect(request.isDone()).toBeTruthy();
});
});
test('should handle token failures correctly', () => {
expect.assertions(1);
const options = {
getToken: () => Promise.reject(new Error('unable to fetch token'))
};
const instance = axios.create({
baseURL: 'https://api.example.com'
});
instance.interceptors.request.use(plugin(options));
return instance.get('/foo').catch((err) => {
expect(err.message).toEqual('unable to fetch token');
});
});
});
});