deno.land / x / oauth4webapi@v1.2.2 / test / discovery.test.ts

discovery.test.ts
View Documentation
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
import 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', }, )})
oauth4webapi

Version Info

Tagged at
2 years ago