text.test.ts
This file
Path
src/helper/streaming/text.test.ts
34 lines 路 969 bytes 路 TypeScript
Last changed by
chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435import { streamText } from '.'import { Context } from '../../context'
describe('Text Streaming Helper', () => { const req = new Request('http://localhost/') let c: Context beforeEach(() => { c = new Context(req) })
it('Check streamText Response', async () => { const res = streamText(c, async (stream) => { for (let i = 0; i < 3; i++) { await stream.write(`${i}`) await stream.sleep(1) } })
expect(res.status).toBe(200) expect(res.headers.get('content-type')).toMatch(/^text\/plain/) expect(res.headers.get('x-content-type-options')).toBe('nosniff') expect(res.headers.get('transfer-encoding')).toBe('chunked')
if (!res.body) { throw new Error('Body is null') } const reader = res.body.getReader() const decoder = new TextDecoder() for (let i = 0; i < 3; i++) { const { value } = await reader.read() expect(decoder.decode(value)).toEqual(`${i}`) } })})