|
| 1 | +// deno-lint-ignore-file no-unused-vars no-explicit-any |
| 2 | +import 'https://gist.githubusercontent.com/qwtel/b14f0f81e3a96189f7771f83ee113f64/raw/TestRequest.ts' |
| 3 | +import { |
| 4 | + assert, |
| 5 | + assertExists, |
| 6 | + assertEquals, |
| 7 | + assertStrictEquals, |
| 8 | + assertStringIncludes, |
| 9 | + assertThrows, |
| 10 | + assertRejects, |
| 11 | + assertArrayIncludes, |
| 12 | +} from 'https://deno.land/std@0.133.0/testing/asserts.ts' |
| 13 | +const { test } = Deno; |
| 14 | + |
| 15 | +import { JSONStreamResponse, JSONStreamRequest } from '../json-fetch.ts' |
| 16 | +import { JSONParseStream } from '../index.ts' |
| 17 | + |
| 18 | +test('exists', () =>{ |
| 19 | + assertExists(JSONStreamRequest) |
| 20 | + assertExists(JSONStreamResponse) |
| 21 | +}) |
| 22 | + |
| 23 | +test('simple response', async () => { |
| 24 | + const actual = await new JSONStreamResponse({ a: 3, b: { nested: 4 }, c: [1, 2, 3], __x: undefined }).json() |
| 25 | + assertEquals(actual, { a: 3, b: { nested: 4 }, c: [1, 2, 3] }) |
| 26 | +}) |
| 27 | + |
| 28 | +test('simple request', async () => { |
| 29 | + const actual = await new JSONStreamRequest('/', { method: 'PUT', body: { a: 3, b: { nested: 4 }, c: [1, 2, 3], __x: undefined } }).json() |
| 30 | + assertEquals(actual, { a: 3, b: { nested: 4 }, c: [1, 2, 3] }) |
| 31 | +}) |
| 32 | + |
| 33 | +test('with promise response', async () => { |
| 34 | + const actual = await new JSONStreamResponse(({ a: 3, b: Promise.resolve(4) })).json() |
| 35 | + assertEquals(actual, { a: 3, b: 4 }) |
| 36 | +}) |
| 37 | + |
| 38 | +test('with promise request', async () => { |
| 39 | + const actual = await new JSONStreamRequest('/', { method: 'PUT', body: { a: 3, b: Promise.resolve(4) } }).json() |
| 40 | + assertEquals(actual, { a: 3, b: 4 }) |
| 41 | +}) |
| 42 | + |
| 43 | +const timeout = (n?: number) => new Promise(r => setTimeout(r, n)) |
| 44 | +async function* asyncGen<T>(xs: T[]) { |
| 45 | + for (const x of xs) { await timeout(); yield x } |
| 46 | +} |
| 47 | + |
| 48 | +test('with generator response', async () => { |
| 49 | + const actual = await new JSONStreamResponse(({ a: 3, b: Promise.resolve(4), c: asyncGen([1, 2, 3]) })).text() |
| 50 | + assertEquals(actual, JSON.stringify({ a: 3, b: 4, c: [1, 2, 3] })) |
| 51 | +}) |
| 52 | + |
| 53 | +test('with generator request', async () => { |
| 54 | + const actual = await new JSONStreamRequest('/', { method: 'PUT', body: { a: 3, b: Promise.resolve(4), c: asyncGen([1, 2, 3]) } }).json() |
| 55 | + assertEquals(actual, { a: 3, b: 4, c: [1, 2, 3] }) |
| 56 | +}) |
| 57 | + |
| 58 | +test('circular throws', () => { |
| 59 | + const a: any = { a: 3, foo: { b: 4 } } |
| 60 | + a.foo.a = a; |
| 61 | + assertRejects(() => new JSONStreamResponse((a)).json(), TypeError) |
| 62 | +}) |
| 63 | + |
| 64 | +test('GET with body throws', () => { |
| 65 | + const a: any = { a: 3, foo: { b: 4 } } |
| 66 | + assertRejects(() => new JSONStreamRequest('/', { body: a }).json(), TypeError) |
| 67 | +}) |
| 68 | + |
| 69 | +test('stream', async () => { |
| 70 | + const actual = new JSONStreamResponse(({ a: 3, b: Promise.resolve(4), c: asyncGen([1, 2, 3]) })) |
| 71 | + const reader = actual.body!.pipeThrough(new JSONParseStream('$.c.*')).getReader() |
| 72 | + assertEquals((await reader.read()).value, 1) |
| 73 | + assertEquals((await reader.read()).value, 2) |
| 74 | + assertEquals((await reader.read()).value, 3) |
| 75 | + assertEquals((await reader.read()).done, true) |
| 76 | +}) |
0 commit comments