|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { protectedResourceHandler } from "../src/index"; |
| 3 | + |
| 4 | +describe("auth", () => { |
| 5 | + describe("resource metadata URL to resource identifier mapping", () => { |
| 6 | + const handler = protectedResourceHandler({ |
| 7 | + authServerUrls: ["https://auth-server.com"], |
| 8 | + }); |
| 9 | + |
| 10 | + const testCases = [ |
| 11 | + // Default well-known URI suffix (oauth-protected-resource) |
| 12 | + { |
| 13 | + resourceMetadata: 'https://resource-server.com/.well-known/oauth-protected-resource', |
| 14 | + resource: 'https://resource-server.com', |
| 15 | + }, |
| 16 | + { |
| 17 | + resourceMetadata: 'https://resource-server.com/.well-known/oauth-protected-resource/my-resource', |
| 18 | + resource: 'https://resource-server.com/my-resource', |
| 19 | + }, |
| 20 | + { |
| 21 | + resourceMetadata: 'https://resource-server.com/.well-known/oauth-protected-resource/foo/bar', |
| 22 | + resource: 'https://resource-server.com/foo/bar', |
| 23 | + }, |
| 24 | + // Ensure ports work |
| 25 | + { |
| 26 | + resourceMetadata: 'https://resource-server.com:8443/.well-known/oauth-protected-resource', |
| 27 | + resource: 'https://resource-server.com:8443', |
| 28 | + }, |
| 29 | + // Example well-known URI suffix from RFC 9728 (example-protected-resource) |
| 30 | + { |
| 31 | + resourceMetadata: 'https://resource-server.com/.well-known/example-protected-resource', |
| 32 | + resource: 'https://resource-server.com', |
| 33 | + }, |
| 34 | + { |
| 35 | + resourceMetadata: 'https://resource-server.com/.well-known/example-protected-resource/my-resource', |
| 36 | + resource: 'https://resource-server.com/my-resource', |
| 37 | + }, |
| 38 | + ] as const; |
| 39 | + |
| 40 | + testCases.forEach(testCase => { |
| 41 | + it(`${testCase.resourceMetadata} → ${testCase.resource}`, async () => { |
| 42 | + const req = new Request(testCase.resourceMetadata); |
| 43 | + const res = handler(req); |
| 44 | + const json = await res.json(); |
| 45 | + expect(json.resource).toBe(testCase.resource); |
| 46 | + }); |
| 47 | + }); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
0 commit comments