|
| 1 | +import test from 'ava'; |
| 2 | +import getStream from '../source/index.js'; |
| 3 | +import {fixtureString, fixtureMultiString} from './fixtures/index.js'; |
| 4 | +import {readableStreamFrom, onFinishedStream} from './helpers/index.js'; |
| 5 | + |
| 6 | +test('Can use ReadableStream', async t => { |
| 7 | + const stream = readableStreamFrom(fixtureMultiString); |
| 8 | + t.is(await getStream(stream), fixtureString); |
| 9 | + await onFinishedStream(stream); |
| 10 | +}); |
| 11 | + |
| 12 | +test('Can use already ended ReadableStream', async t => { |
| 13 | + const stream = readableStreamFrom(fixtureMultiString); |
| 14 | + t.is(await getStream(stream), fixtureString); |
| 15 | + t.is(await getStream(stream), ''); |
| 16 | + await onFinishedStream(stream); |
| 17 | +}); |
| 18 | + |
| 19 | +test('Can use already canceled ReadableStream', async t => { |
| 20 | + let canceledValue; |
| 21 | + const stream = new ReadableStream({ |
| 22 | + cancel(canceledError) { |
| 23 | + canceledValue = canceledError; |
| 24 | + }, |
| 25 | + }); |
| 26 | + const error = new Error('test'); |
| 27 | + await stream.cancel(error); |
| 28 | + t.is(canceledValue, error); |
| 29 | + t.is(await getStream(stream), ''); |
| 30 | + await onFinishedStream(stream); |
| 31 | +}); |
| 32 | + |
| 33 | +test('Can use already errored ReadableStream', async t => { |
| 34 | + const error = new Error('test'); |
| 35 | + const stream = new ReadableStream({ |
| 36 | + start(controller) { |
| 37 | + controller.error(error); |
| 38 | + }, |
| 39 | + }); |
| 40 | + t.is(await t.throwsAsync(getStream(stream)), error); |
| 41 | + t.is(await t.throwsAsync(onFinishedStream(stream)), error); |
| 42 | +}); |
| 43 | + |
| 44 | +test('Cancel ReadableStream when maxBuffer is hit', async t => { |
| 45 | + let canceled = false; |
| 46 | + const stream = new ReadableStream({ |
| 47 | + start(controller) { |
| 48 | + controller.enqueue(fixtureString); |
| 49 | + controller.enqueue(fixtureString); |
| 50 | + controller.close(); |
| 51 | + }, |
| 52 | + cancel() { |
| 53 | + canceled = true; |
| 54 | + }, |
| 55 | + }); |
| 56 | + const error = await t.throwsAsync( |
| 57 | + getStream(stream, {maxBuffer: 1}), |
| 58 | + {message: /maxBuffer exceeded/}, |
| 59 | + ); |
| 60 | + t.deepEqual(error.bufferedData, fixtureString[0]); |
| 61 | + await onFinishedStream(stream); |
| 62 | + t.true(canceled); |
| 63 | +}); |
0 commit comments