deno.land / x / oauth4webapi@v1.2.2 / test / discovery.test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140import anyTest, { type TestFn } from 'ava'import setup, { type Context, teardown, issuer, getResponse, UA } from './_setup.js'import * as lib from '../src/index.js'
const test = anyTest as TestFn<Context>
test.before(setup)test.after(teardown)
test('discoveryRequest()', async (t) => { const data = { ...issuer } t.context .intercept({ path: '/.well-known/openid-configuration', method: 'GET', headers: { accept: 'application/json', 'user-agent': UA, }, }) .reply(200, data)
const response = await lib.discoveryRequest(new URL(issuer.issuer)) t.true(response instanceof Response)})
test('discoveryRequest() w/ Custom Headers', async (t) => { const data = { ...issuer } t.context .intercept({ path: '/.well-known/openid-configuration', method: 'GET', headers: { 'user-agent': 'foo', foo: 'bar', accept: 'application/json', }, }) .reply(200, data)
const response = await lib.discoveryRequest(new URL(issuer.issuer), { headers: new Headers([ ['accept', 'will be overwritten'], ['user-agent', 'foo'], ['foo', 'bar'], ]), }) t.true(response instanceof Response)})
test('discoveryRequest() - oidc with a pathname', async (t) => { const data = { issuer: 'https://op.example.com/path' } t.context .intercept({ path: '/path/.well-known/openid-configuration', method: 'GET', }) .reply(200, data)
const url = new URL(data.issuer) await lib.discoveryRequest(url, { algorithm: 'oidc' }) t.pass()})
test('discoveryRequest() - oauth2', async (t) => { const data = { ...issuer } t.context .intercept({ path: '/.well-known/oauth-authorization-server', method: 'GET', }) .reply(200, data)
const url = new URL(issuer.issuer) await lib.discoveryRequest(url, { algorithm: 'oauth2' }) t.pass()})
test('discoveryRequest() - oauth2 with a pathname', async (t) => { const data = { issuer: 'https://op.example.com/path' } t.context .intercept({ path: '/.well-known/oauth-authorization-server/path', method: 'GET', }) .reply(200, data)
const url = new URL(data.issuer) await lib.discoveryRequest(url, { algorithm: 'oauth2' }) t.pass()})
test('processDiscoveryResponse()', async (t) => { const expected = new URL('https://op.example.com')
await t.throwsAsync(lib.processDiscoveryResponse(expected, <any>null), { message: '"response" must be an instance of Response', }) await t.throwsAsync(lib.processDiscoveryResponse(<any>null, new Response()), { message: '"expectedIssuer" must be an instance of URL', }) await t.throwsAsync(lib.processDiscoveryResponse(expected, getResponse('', { status: 404 })), { message: '"response" is not a conform Authorization Server Metadata response', }) await t.throwsAsync(lib.processDiscoveryResponse(expected, getResponse('{"')), { message: 'failed to parse "response" body as JSON', }) await t.throwsAsync(lib.processDiscoveryResponse(expected, getResponse('null')), { message: '"response" body must be a top level object', }) await t.throwsAsync(lib.processDiscoveryResponse(expected, getResponse('[]')), { message: '"response" body must be a top level object', })
await t.throwsAsync( lib.processDiscoveryResponse(expected, getResponse(JSON.stringify({ issuer: null }))), { message: '"response" body "issuer" property must be a non-empty string', }, )
await t.throwsAsync( lib.processDiscoveryResponse( expected, getResponse(JSON.stringify({ issuer: 'https://another-op.example.com' })), ), { message: '"response" body "issuer" does not match "expectedIssuer"' }, )
t.deepEqual( await lib.processDiscoveryResponse( expected, getResponse(JSON.stringify({ issuer: 'https://op.example.com' })), ), { issuer: 'https://op.example.com', }, )})
Version Info