|
1 | 1 | import { describe, it } from 'node:test' |
2 | | -import assert from 'node:assert' |
3 | | -import { createJsonRpcClient } from './index.js' |
| 2 | +import assert, { AssertionError } from 'node:assert' |
| 3 | +import { assertOkResponse } from './index.js' |
4 | 4 |
|
5 | | -describe('assert-ok-response', () => { |
6 | | - it('should create a JSON-RPC client', () => { |
7 | | - const client = createJsonRpcClient('http://example.com') |
8 | | - assert.ok(client) |
| 5 | +describe('assertOkResponse', () => { |
| 6 | + it('accepts ok (200)', async () => { |
| 7 | + const response = createFakeResponse({ |
| 8 | + ok: true, |
| 9 | + }) |
| 10 | + |
| 11 | + await assertOkResponse(response) |
| 12 | + }) |
| 13 | + |
| 14 | + it('rejects !ok (404)', async () => { |
| 15 | + const response = createFakeResponse({ |
| 16 | + ok: false, |
| 17 | + status: 404, |
| 18 | + async text() { |
| 19 | + return 'endpoint not found' |
| 20 | + }, |
| 21 | + }) |
| 22 | + |
| 23 | + const error = await assertRejects(() => assertOkResponse(response)) |
| 24 | + assert.equal(error.message, 'Cannot fetch / (404): endpoint not found') |
| 25 | + assert.equal(error.statusCode, 404) |
| 26 | + assert.equal(error.serverMessage, 'endpoint not found') |
| 27 | + }) |
| 28 | + |
| 29 | + it('uses custom message when provided', async () => { |
| 30 | + const response = createFakeResponse({ ok: false }) |
| 31 | + const error = await assertRejects(() => |
| 32 | + assertOkResponse(response, 'Cannot post the measurement'), |
| 33 | + ) |
| 34 | + assert.match(error.message, /^Cannot post the measurement/) |
| 35 | + }) |
| 36 | + |
| 37 | + it('handles error while reading error response body', async () => { |
| 38 | + const response = createFakeResponse({ |
| 39 | + ok: false, |
| 40 | + status: 500, |
| 41 | + async text() { |
| 42 | + throw new Error('chunked encoding error') |
| 43 | + }, |
| 44 | + }) |
| 45 | + |
| 46 | + const error = await assertRejects(() => assertOkResponse(response)) |
| 47 | + assert.equal( |
| 48 | + error.message, |
| 49 | + 'Cannot fetch / (500): (Cannot read response body: chunked encoding error)', |
| 50 | + ) |
| 51 | + assert.equal(error.statusCode, 500) |
| 52 | + assert.equal(error.serverMessage, '(Cannot read response body: chunked encoding error)') |
9 | 53 | }) |
10 | 54 | }) |
| 55 | + |
| 56 | +// Note: the built-in assert.rejects() does not return the rejection (the error) |
| 57 | +async function assertRejects(fn) { |
| 58 | + try { |
| 59 | + await fn() |
| 60 | + } catch (err) { |
| 61 | + return err |
| 62 | + } |
| 63 | + throw new AssertionError('Expected promise to reject') |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * @param {Partial<Response>} props |
| 68 | + * @returns {Response} |
| 69 | + */ |
| 70 | +function createFakeResponse(props = {}) { |
| 71 | + const status = props.status ?? (props.ok ? 200 : 500) |
| 72 | + const ok = props.ok ?? props.status < 400 |
| 73 | + return { |
| 74 | + headers: new Headers(), |
| 75 | + url: '/', |
| 76 | + async text() { |
| 77 | + return '(empty response body)' |
| 78 | + }, |
| 79 | + |
| 80 | + ...props, |
| 81 | + |
| 82 | + ok, |
| 83 | + status, |
| 84 | + } |
| 85 | +} |
0 commit comments