11import { describe , expect , it } from "vitest" ;
22import { readResponseBodySnippet } from "./http-error-body.js" ;
33
4- function bodyLessResponse ( text : string ) : Response {
5- return {
6- body : { } as ReadableStream ,
7- text : async ( ) => text ,
8- arrayBuffer : async ( ) => new TextEncoder ( ) . encode ( text ) . buffer ,
9- } as unknown as Response ;
4+ function streamResponseFromText ( text : string ) : Response {
5+ return new Response ( new Blob ( [ text ] ) . stream ( ) ) ;
106}
117
128describe ( "readResponseBodySnippet" , ( ) => {
13- it ( "returns full text when under both limits (body-less path) " , async ( ) => {
9+ it ( "returns full text when under both limits" , async ( ) => {
1410 const text = "short text" ;
15- const result = await readResponseBodySnippet ( bodyLessResponse ( text ) , {
11+ const result = await readResponseBodySnippet ( streamResponseFromText ( text ) , {
1612 maxBytes : 1024 ,
1713 maxChars : 50 ,
1814 } ) ;
1915 expect ( result ) . toBe ( text ) ;
2016 } ) ;
2117
22- it ( "truncates by maxChars when under maxBytes (body-less path) " , async ( ) => {
18+ it ( "truncates by maxChars when under maxBytes" , async ( ) => {
2319 const text = "abcdefghij" ;
24- const result = await readResponseBodySnippet ( bodyLessResponse ( text ) , {
20+ const result = await readResponseBodySnippet ( streamResponseFromText ( text ) , {
2521 maxBytes : 1024 ,
2622 maxChars : 5 ,
2723 } ) ;
2824 expect ( result ) . toBe ( "abcde" ) ;
2925 } ) ;
3026
31- it ( "truncates by maxBytes in the body-less path " , async ( ) => {
27+ it ( "truncates by maxBytes before maxChars " , async ( ) => {
3228 const text = "a" . repeat ( 200 ) ;
33- const result = await readResponseBodySnippet ( bodyLessResponse ( text ) , {
29+ const result = await readResponseBodySnippet ( streamResponseFromText ( text ) , {
3430 maxBytes : 50 ,
3531 maxChars : 500 ,
3632 } ) ;
@@ -39,9 +35,9 @@ describe("readResponseBodySnippet", () => {
3935 expect ( result . length ) . toBeLessThan ( text . length ) ;
4036 } ) ;
4137
42- it ( "enforces maxBytes before maxChars in the body-less path " , async ( ) => {
38+ it ( "enforces maxBytes before maxChars" , async ( ) => {
4339 const text = "a" . repeat ( 500 ) ;
44- const result = await readResponseBodySnippet ( bodyLessResponse ( text ) , {
40+ const result = await readResponseBodySnippet ( streamResponseFromText ( text ) , {
4541 maxBytes : 30 ,
4642 maxChars : 500 ,
4743 } ) ;
@@ -52,16 +48,16 @@ describe("readResponseBodySnippet", () => {
5248 it ( "does not split multi-byte UTF-8 characters at the byte boundary" , async ( ) => {
5349 // U+1F600 (😀) is 4 bytes in UTF-8: F0 9F 98 80
5450 const text = "ab😀cd" ;
55- // 2 ASCII bytes (ab) + cut before the 4-byte emoji
56- // Without stream flag, incomplete multi-byte produces U+FFFD
57- const result = await readResponseBodySnippet ( bodyLessResponse ( text ) , {
51+ // 2 ASCII bytes (ab) + cut before the 4-byte emoji.
52+ // Without stream:true flag, incomplete multi-byte produces U+FFFD.
53+ const result = await readResponseBodySnippet ( streamResponseFromText ( text ) , {
5854 maxBytes : 3 ,
5955 maxChars : 100 ,
6056 } ) ;
6157 expect ( result ) . toBe ( "ab�" ) ;
6258 } ) ;
6359
64- it ( "stream path still enforces maxBytes" , async ( ) => {
60+ it ( "stream path enforces maxBytes" , async ( ) => {
6561 const data = new Uint8Array ( 500 ) . fill ( 97 ) ; // 500 'a' bytes
6662 const response = new Response ( new Blob ( [ data ] ) . stream ( ) ) ;
6763 const result = await readResponseBodySnippet ( response , {
@@ -72,7 +68,7 @@ describe("readResponseBodySnippet", () => {
7268 expect ( byteLen ) . toBeLessThanOrEqual ( 100 ) ;
7369 } ) ;
7470
75- it ( "stream path still enforces maxChars" , async ( ) => {
71+ it ( "stream path enforces maxChars" , async ( ) => {
7672 const data = new Uint8Array ( 500 ) . fill ( 97 ) ;
7773 const response = new Response ( new Blob ( [ data ] ) . stream ( ) ) ;
7874 const result = await readResponseBodySnippet ( response , {
@@ -82,34 +78,33 @@ describe("readResponseBodySnippet", () => {
8278 expect ( result . length ) . toBeLessThanOrEqual ( 10 ) ;
8379 } ) ;
8480
85- it ( "returns empty string when maxBytes is 0 (body-less path) " , async ( ) => {
86- const result = await readResponseBodySnippet ( bodyLessResponse ( "some text" ) , {
81+ it ( "returns empty string when maxBytes is 0" , async ( ) => {
82+ const result = await readResponseBodySnippet ( streamResponseFromText ( "some text" ) , {
8783 maxBytes : 0 ,
8884 maxChars : 100 ,
8985 } ) ;
9086 expect ( result ) . toBe ( "" ) ;
9187 } ) ;
9288
9389 it ( "returns empty string for empty response body" , async ( ) => {
94- const result = await readResponseBodySnippet ( bodyLessResponse ( "" ) , {
90+ const result = await readResponseBodySnippet ( streamResponseFromText ( "" ) , {
9591 maxBytes : 1024 ,
9692 maxChars : 50 ,
9793 } ) ;
9894 expect ( result ) . toBe ( "" ) ;
9995 } ) ;
10096
101- it ( "avoids arrayBuffer() when Content-Length exceeds maxBytes (body-less path )" , async ( ) => {
97+ it ( "fails closed when body has no getReader (avoids arrayBuffer )" , async ( ) => {
10298 let arrayBufferCalled = false ;
103- const bigResponse = {
99+ const noReaderResponse = {
104100 body : { } as ReadableStream ,
105- headers : { get : ( ) => "1000" } ,
106101 arrayBuffer : async ( ) => {
107102 arrayBufferCalled = true ;
108- return new ArrayBuffer ( 0 ) ;
103+ return new TextEncoder ( ) . encode ( "large body" ) . buffer ;
109104 } ,
110105 } as unknown as Response ;
111- const result = await readResponseBodySnippet ( bigResponse , {
112- maxBytes : 50 ,
106+ const result = await readResponseBodySnippet ( noReaderResponse , {
107+ maxBytes : 1024 ,
113108 maxChars : 100 ,
114109 } ) ;
115110 expect ( arrayBufferCalled ) . toBe ( false ) ;
0 commit comments