1+ import { Readable , Transform } from 'stream'
12import supertest , { SuperTest , Test } from 'supertest'
23import { describe , it , expect , beforeEach } from 'vitest'
34import { createApp , App } from '../src'
@@ -18,6 +19,48 @@ describe('app', () => {
1819 expect ( res . body ) . toEqual ( { url : '/' } )
1920 } )
2021
22+ it ( 'can return Buffer directly' , async ( ) => {
23+ app . use ( ( ) => Buffer . from ( '<h1>Hello world!</h1>' , 'utf8' ) )
24+ const res = await request . get ( '/' )
25+
26+ expect ( res . text ) . toBe ( '<h1>Hello world!</h1>' )
27+ } )
28+
29+ it ( 'can return Readable stream directly' , async ( ) => {
30+ app . use ( ( ) => {
31+ const readable = new Readable ( )
32+ readable . push ( Buffer . from ( '<h1>Hello world!</h1>' , 'utf8' ) )
33+ readable . push ( null )
34+ return readable
35+ } )
36+ const res = await request . get ( '/' )
37+
38+ expect ( res . text ) . toBe ( '<h1>Hello world!</h1>' )
39+ expect ( res . header [ 'transfer-encoding' ] ) . toBe ( 'chunked' )
40+ } )
41+
42+ it ( 'can return Readable stream that may throw' , async ( ) => {
43+ app . use ( ( ) => {
44+ const readable = new Readable ( )
45+ const willThrow = new Transform ( {
46+ transform (
47+ _chunk ,
48+ _encoding ,
49+ callback
50+ ) {
51+ setTimeout ( ( ) => callback ( new Error ( 'test' ) ) , 0 )
52+ }
53+ } )
54+ readable . push ( Buffer . from ( '<h1>Hello world!</h1>' , 'utf8' ) )
55+ readable . push ( null )
56+
57+ return readable . pipe ( willThrow )
58+ } )
59+ const res = await request . get ( '/' )
60+
61+ expect ( res . status ) . toBe ( 500 )
62+ } )
63+
2164 it ( 'can return HTML directly' , async ( ) => {
2265 app . use ( ( ) => '<h1>Hello world!</h1>' )
2366 const res = await request . get ( '/' )
0 commit comments