-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The interceptor should work when attached to a global agent (#11)
Signed-off-by: Matteo Collina <[email protected]>
- Loading branch information
Showing
2 changed files
with
62 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
'use strict' | ||
|
||
const test = require('node:test') | ||
const assert = require('node:assert') | ||
const http = require('node:http') | ||
const { once, EventEmitter } = require('node:events') | ||
const { request, Agent, setGlobalDispatcher, getGlobalDispatcher } = require('undici') | ||
const { createDecoder } = require('fast-jwt') | ||
const { createOAuthInterceptor } = require('../') | ||
const { createToken } = require('./helper') | ||
|
||
const originalGlobalDispatcher = getGlobalDispatcher() | ||
test.afterEach(() => setGlobalDispatcher(originalGlobalDispatcher)) | ||
|
||
test('get an access token if no token provided', async (t) => { | ||
let accessToken = '' | ||
const mainServer = http.createServer((req, res) => { | ||
assert.ok(req.headers.authorization.length > 'Bearer '.length) | ||
assert.strictEqual(req.headers.authorization, `Bearer ${accessToken}`) | ||
res.writeHead(200) | ||
res.end() | ||
}) | ||
mainServer.listen(0) | ||
|
||
const tokenServer = http.createServer((req, res) => { | ||
assert.strictEqual(req.method, 'POST') | ||
assert.strictEqual(req.url, '/token') | ||
|
||
accessToken = createToken({ name: 'access' }, { expiresIn: '1d' }) | ||
res.writeHead(200) | ||
res.end(JSON.stringify({ access_token: accessToken })) | ||
}) | ||
tokenServer.listen(0) | ||
|
||
t.after(() => { | ||
mainServer.close() | ||
tokenServer.close() | ||
}) | ||
|
||
const refreshToken = createToken( | ||
{ name: 'refresh' }, | ||
{ expiresIn: '1d', iss: `http://localhost:${tokenServer.address().port}` } | ||
) | ||
|
||
const dispatcher = new Agent({ | ||
interceptors: { | ||
Pool: [createOAuthInterceptor({ | ||
refreshToken, | ||
retryOnStatusCodes: [401], | ||
origins: [`http://localhost:${mainServer.address().port}`], | ||
clientId: 'client-id' | ||
})] | ||
} | ||
}) | ||
|
||
setGlobalDispatcher(dispatcher) | ||
|
||
const { statusCode } = await request(`http://localhost:${mainServer.address().port}`) | ||
assert.strictEqual(statusCode, 200) | ||
}) |