|
1 | | -import test from 'node:test'; |
2 | 1 | import assert from 'node:assert'; |
| 2 | +import test from 'node:test'; |
| 3 | +import { createApiClient } from './src/apiClient.js'; |
| 4 | + |
| 5 | +test('attaches Authorization Bearer header', async () => { |
| 6 | + let capturedInit: RequestInit | undefined; |
| 7 | + const mockFetch = async ( |
| 8 | + _url: string | URL | Request, |
| 9 | + init?: RequestInit, |
| 10 | + ): Promise<Response> => { |
| 11 | + capturedInit = init; |
| 12 | + return new Response(JSON.stringify({}), { |
| 13 | + status: 200, |
| 14 | + headers: { 'Content-Type': 'application/json' }, |
| 15 | + }); |
| 16 | + }; |
| 17 | + |
| 18 | + const client = createApiClient( |
| 19 | + async () => 'test-token', |
| 20 | + 'http://localhost:8080', |
| 21 | + mockFetch as typeof fetch, |
| 22 | + ); |
| 23 | + await client.post('/ping'); |
| 24 | + |
| 25 | + const headers = capturedInit?.headers as Record<string, string>; |
| 26 | + assert.strictEqual(headers['Authorization'], 'Bearer test-token'); |
| 27 | +}); |
| 28 | + |
| 29 | +test('sends POST to the correct URL', async () => { |
| 30 | + let capturedUrl: string | URL | Request = ''; |
| 31 | + const mockFetch = async ( |
| 32 | + url: string | URL | Request, |
| 33 | + _init?: RequestInit, |
| 34 | + ): Promise<Response> => { |
| 35 | + capturedUrl = url; |
| 36 | + return new Response(JSON.stringify({}), { |
| 37 | + status: 200, |
| 38 | + headers: { 'Content-Type': 'application/json' }, |
| 39 | + }); |
| 40 | + }; |
| 41 | + |
| 42 | + const client = createApiClient( |
| 43 | + async () => 'token', |
| 44 | + 'http://localhost:8080', |
| 45 | + mockFetch as typeof fetch, |
| 46 | + ); |
| 47 | + await client.post('/ping'); |
| 48 | + assert.strictEqual(capturedUrl, 'http://localhost:8080/ping'); |
| 49 | +}); |
| 50 | + |
| 51 | +test('throws on non-ok response with status code', async () => { |
| 52 | + const mockFetch = async (): Promise<Response> => |
| 53 | + new Response('', { status: 401 }); |
| 54 | + const client = createApiClient( |
| 55 | + async () => 'token', |
| 56 | + 'http://localhost:8080', |
| 57 | + mockFetch as typeof fetch, |
| 58 | + ); |
| 59 | + await assert.rejects( |
| 60 | + () => client.post('/ping'), |
| 61 | + (err: Error) => { |
| 62 | + assert.match(err.message, /HTTP 401/); |
| 63 | + return true; |
| 64 | + }, |
| 65 | + ); |
| 66 | +}); |
| 67 | + |
| 68 | +test('includes server error body in thrown error', async () => { |
| 69 | + const mockFetch = async (): Promise<Response> => |
| 70 | + new Response('Unauthorized: token expired', { status: 401 }); |
| 71 | + const client = createApiClient( |
| 72 | + async () => 'token', |
| 73 | + 'http://localhost:8080', |
| 74 | + mockFetch as typeof fetch, |
| 75 | + ); |
| 76 | + await assert.rejects( |
| 77 | + () => client.post('/ping'), |
| 78 | + (err: Error) => { |
| 79 | + assert.match(err.message, /token expired/); |
| 80 | + return true; |
| 81 | + }, |
| 82 | + ); |
| 83 | +}); |
3 | 84 |
|
4 | | -test('Simple Test', () => { |
5 | | - assert.strictEqual(true, true); |
| 85 | +test('returns undefined for 204 No Content', async () => { |
| 86 | + const mockFetch = async (): Promise<Response> => |
| 87 | + new Response(null, { status: 204 }); |
| 88 | + const client = createApiClient( |
| 89 | + async () => 'token', |
| 90 | + 'http://localhost:8080', |
| 91 | + mockFetch as typeof fetch, |
| 92 | + ); |
| 93 | + const result = await client.post('/trigger'); |
| 94 | + assert.strictEqual(result, undefined); |
6 | 95 | }); |
0 commit comments