-
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.
Refactor error handling and bring code coverage to 100% (#6)
* Refactor error handling and bring code coverage to 100% Signed-off-by: Matteo Collina <[email protected]> * fixup Signed-off-by: Matteo Collina <[email protected]> --------- Signed-off-by: Matteo Collina <[email protected]>
- Loading branch information
Showing
7 changed files
with
157 additions
and
43 deletions.
There are no files selected for viewing
File renamed without changes.
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
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,90 @@ | ||
'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 } = require('undici') | ||
const { createDecoder } = require('fast-jwt') | ||
const { createOAuthInterceptor } = require('../') | ||
const { createToken } = require('./helper') | ||
|
||
test('error when refreshing', async (t) => { | ||
const accessToken = createToken({ name: 'access' }, { expiresIn: '1ms' }) | ||
|
||
const mainServer = http.createServer((req, res) => { | ||
assert.fail('should not be called') | ||
}) | ||
mainServer.listen(0) | ||
|
||
const tokenServer = http.createServer((req, res) => { | ||
assert.strictEqual(req.method, 'POST') | ||
assert.strictEqual(req.url, '/token') | ||
|
||
res.writeHead(400) | ||
res.end(JSON.stringify({ message: 'kaboom' })) | ||
}) | ||
tokenServer.listen(0) | ||
|
||
t.after(() => { | ||
mainServer.close() | ||
tokenServer.close() | ||
}) | ||
|
||
const refreshToken = createToken( | ||
{ name: 'refresh' }, | ||
{ expiresIn: '1d', iss: `http://localhost:${tokenServer.address().port}`, sub: 'client-id' } | ||
) | ||
|
||
const dispatcher = new Agent({ | ||
interceptors: { | ||
Pool: [createOAuthInterceptor({ | ||
accessToken, | ||
refreshToken, | ||
retryOnStatusCodes: [401] | ||
})] | ||
} | ||
}) | ||
|
||
await assert.rejects(request(`http://localhost:${mainServer.address().port}`, { dispatcher })) | ||
}) | ||
|
||
test('after service rejects the token, token service reject token, error request', async (t) => { | ||
const accessToken = createToken({ name: 'access' }, { expiresIn: '1d' }) | ||
const mainServer = http.createServer((req, res) => { | ||
res.writeHead(401) | ||
return res.end() | ||
}) | ||
mainServer.listen(0) | ||
|
||
const tokenServer = http.createServer((req, res) => { | ||
assert.strictEqual(req.method, 'POST') | ||
assert.strictEqual(req.url, '/token') | ||
|
||
res.writeHead(403) | ||
res.end(JSON.stringify({ message: 'kaboom' })) | ||
}) | ||
tokenServer.listen(0) | ||
|
||
t.after(() => { | ||
mainServer.close() | ||
tokenServer.close() | ||
}) | ||
|
||
const refreshToken = createToken( | ||
{ name: 'refresh' }, | ||
{ expiresIn: '1d', iss: `http://localhost:${tokenServer.address().port}`, sub: 'client-id' } | ||
) | ||
|
||
const dispatcher = new Agent({ | ||
interceptors: { | ||
Pool: [createOAuthInterceptor({ | ||
accessToken, | ||
refreshToken, | ||
retryOnStatusCodes: [401] | ||
})] | ||
} | ||
}) | ||
|
||
assert.rejects(request(`http://localhost:${mainServer.address().port}`, { dispatcher })) | ||
}) |
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