66 * Side Public License, v 1.
77 */
88
9- import { createGunzip } from 'zlib' ;
109import type { Request } from '@hapi/hapi' ;
1110import Boom from '@hapi/boom' ;
1211
1312import mockFs from 'mock-fs' ;
1413import { createReadStream } from 'fs' ;
14+ import { PassThrough } from 'stream' ;
15+ import { createGunzip , createGzip } from 'zlib' ;
1516
1617import { loggerMock , MockedLogger } from '../../logging/logger.mock' ;
1718
@@ -55,59 +56,107 @@ describe('getPayloadSize', () => {
5556 } ) ;
5657 } ) ;
5758
58- describe ( 'handles fs streams' , ( ) => {
59+ describe ( 'handles streams' , ( ) => {
5960 afterEach ( ( ) => mockFs . restore ( ) ) ;
6061
61- test ( 'with ascii characters' , async ( ) => {
62- mockFs ( { 'test.txt' : 'heya' } ) ;
63- const source = createReadStream ( 'test.txt' ) ;
64-
65- let data = '' ;
66- for await ( const chunk of source ) {
67- data += chunk ;
68- }
69-
62+ test ( 'ignores streams that are not fs or zlib streams' , async ( ) => {
7063 const result = getResponsePayloadBytes (
7164 {
7265 variety : 'stream' ,
73- source,
66+ source : new PassThrough ( ) ,
7467 } as Response ,
7568 logger
7669 ) ;
7770
78- expect ( result ) . toBe ( Buffer . byteLength ( data ) ) ;
71+ expect ( result ) . toBe ( undefined ) ;
7972 } ) ;
8073
81- test ( 'with special characters' , async ( ) => {
82- mockFs ( { 'test.txt' : '¡hola!' } ) ;
83- const source = createReadStream ( 'test.txt' ) ;
74+ describe ( 'fs streams' , ( ) => {
75+ test ( 'with ascii characters' , async ( ) => {
76+ mockFs ( { 'test.txt' : 'heya' } ) ;
77+ const source = createReadStream ( 'test.txt' ) ;
8478
85- let data = '' ;
86- for await ( const chunk of source ) {
87- data += chunk ;
88- }
79+ let data = '' ;
80+ for await ( const chunk of source ) {
81+ data += chunk ;
82+ }
8983
90- const result = getResponsePayloadBytes (
91- {
92- variety : 'stream' ,
93- source,
94- } as Response ,
95- logger
96- ) ;
84+ const result = getResponsePayloadBytes (
85+ {
86+ variety : 'stream' ,
87+ source,
88+ } as Response ,
89+ logger
90+ ) ;
91+
92+ expect ( result ) . toBe ( Buffer . byteLength ( data ) ) ;
93+ } ) ;
94+
95+ test ( 'with special characters' , async ( ) => {
96+ mockFs ( { 'test.txt' : '¡hola!' } ) ;
97+ const source = createReadStream ( 'test.txt' ) ;
98+
99+ let data = '' ;
100+ for await ( const chunk of source ) {
101+ data += chunk ;
102+ }
103+
104+ const result = getResponsePayloadBytes (
105+ {
106+ variety : 'stream' ,
107+ source,
108+ } as Response ,
109+ logger
110+ ) ;
97111
98- expect ( result ) . toBe ( Buffer . byteLength ( data ) ) ;
112+ expect ( result ) . toBe ( Buffer . byteLength ( data ) ) ;
113+ } ) ;
99114 } ) ;
100115
101- test ( 'ignores streams that are not instances of ReadStream' , async ( ) => {
102- const result = getResponsePayloadBytes (
103- {
104- variety : 'stream' ,
105- source : createGunzip ( ) ,
106- } as Response ,
107- logger
108- ) ;
116+ describe ( 'zlib streams' , ( ) => {
117+ test ( 'with ascii characters' , async ( ) => {
118+ mockFs ( { 'test.txt' : 'heya' } ) ;
119+ const readStream = createReadStream ( 'test.txt' ) ;
120+ const source = readStream . pipe ( createGzip ( ) ) . pipe ( createGunzip ( ) ) ;
109121
110- expect ( result ) . toBe ( undefined ) ;
122+ let data = '' ;
123+ for await ( const chunk of source ) {
124+ data += chunk ;
125+ }
126+
127+ const result = getResponsePayloadBytes (
128+ {
129+ variety : 'stream' ,
130+ source,
131+ } as Response ,
132+ logger
133+ ) ;
134+
135+ expect ( data ) . toBe ( 'heya' ) ;
136+ expect ( result ) . toBe ( source . bytesWritten ) ;
137+ } ) ;
138+
139+ test ( 'with special characters' , async ( ) => {
140+ mockFs ( { 'test.txt' : '¡hola!' } ) ;
141+ const readStream = createReadStream ( 'test.txt' ) ;
142+ const source = readStream . pipe ( createGzip ( ) ) . pipe ( createGunzip ( ) ) ;
143+
144+ let data = '' ;
145+ for await ( const chunk of source ) {
146+ data += chunk ;
147+ }
148+
149+ const result = getResponsePayloadBytes (
150+ {
151+ variety : 'stream' ,
152+ source,
153+ } as Response ,
154+ logger
155+ ) ;
156+
157+ expect ( data ) . toBe ( '¡hola!' ) ;
158+ expect ( result ) . toBe ( source . bytesWritten ) ;
159+ } ) ;
111160 } ) ;
112161 } ) ;
113162
@@ -134,7 +183,7 @@ describe('getPayloadSize', () => {
134183 expect ( result ) . toBe ( 7 ) ;
135184 } ) ;
136185
137- test ( 'when source is object' , ( ) => {
186+ test ( 'when source is plain object' , ( ) => {
138187 const payload = { message : 'heya' } ;
139188 const result = getResponsePayloadBytes (
140189 {
@@ -146,11 +195,26 @@ describe('getPayloadSize', () => {
146195 expect ( result ) . toBe ( JSON . stringify ( payload ) . length ) ;
147196 } ) ;
148197
198+ test ( 'when source is array object' , ( ) => {
199+ const payload = [ { message : 'hey' } , { message : 'ya' } ] ;
200+ const result = getResponsePayloadBytes (
201+ {
202+ variety : 'plain' ,
203+ source : payload ,
204+ } as Response ,
205+ logger
206+ ) ;
207+ expect ( result ) . toBe ( JSON . stringify ( payload ) . length ) ;
208+ } ) ;
209+
149210 test ( 'returns undefined when source is not a plain object' , ( ) => {
211+ class TestClass {
212+ constructor ( ) { }
213+ }
150214 const result = getResponsePayloadBytes (
151215 {
152216 variety : 'plain' ,
153- source : [ 1 , 2 , 3 ] ,
217+ source : new TestClass ( ) ,
154218 } as Response ,
155219 logger
156220 ) ;
0 commit comments