@@ -40,18 +40,23 @@ describe('withApilytics()', () => {
4040 }
4141
4242 if ( req . url ?. includes ( 'empty' ) ) {
43+ res . status ( 200 ) . end ( ) ;
44+ return ;
45+ }
46+
47+ if ( req . url ?. includes ( 'no-url-or-method' ) ) {
4348 req . url = undefined ;
4449 req . method = undefined ;
45- res . end ( ) ;
50+ res . status ( 200 ) . end ( ) ;
4651 return ;
4752 }
4853
4954 if ( req . method === 'POST' ) {
50- res . status ( 201 ) . end ( ) ;
55+ res . status ( 201 ) . send ( 'created' ) ;
5156 return ;
5257 }
5358
54- res . status ( 200 ) . end ( ) ;
59+ res . status ( 200 ) . send ( 'ok' ) ;
5560 } ;
5661
5762 const createAgent = ( {
@@ -114,6 +119,7 @@ describe('withApilytics()', () => {
114119 path : '/' ,
115120 method : 'GET' ,
116121 statusCode : 200 ,
122+ responseSize : 2 ,
117123 timeMillis : expect . any ( Number ) ,
118124 } ) ;
119125 expect ( data [ 'timeMillis' ] ) . toEqual ( Math . trunc ( data [ 'timeMillis' ] ) ) ;
@@ -149,10 +155,34 @@ describe('withApilytics()', () => {
149155 query : '?param=foo¶m2=bar' ,
150156 method : 'POST' ,
151157 statusCode : 201 ,
158+ requestSize : 0 ,
159+ responseSize : 7 ,
152160 timeMillis : expect . any ( Number ) ,
153161 } ) ;
154162 } ) ;
155163
164+ it ( 'should handle zero request and response sizes' , async ( ) => {
165+ const agent = createAgent ( { apiKey } ) ;
166+ const response = await agent . post ( '/empty' ) ;
167+ expect ( response . status ) . toEqual ( 200 ) ;
168+
169+ expect ( requestSpy ) . toHaveBeenCalledTimes ( 1 ) ;
170+ const data = JSON . parse ( clientRequestMock . write . mock . calls [ 0 ] ) ;
171+ expect ( data . requestSize ) . toEqual ( 0 ) ;
172+ expect ( data . responseSize ) . toEqual ( 0 ) ;
173+ } ) ;
174+
175+ it ( 'should handle non zero request and response sizes' , async ( ) => {
176+ const agent = createAgent ( { apiKey } ) ;
177+ const response = await agent . post ( '/dummy' ) . send ( { hello : 'world' } ) ;
178+ expect ( response . status ) . toEqual ( 201 ) ;
179+
180+ expect ( requestSpy ) . toHaveBeenCalledTimes ( 1 ) ;
181+ const data = JSON . parse ( clientRequestMock . write . mock . calls [ 0 ] ) ;
182+ expect ( data . requestSize ) . toEqual ( 17 ) ;
183+ expect ( data . responseSize ) . toEqual ( 7 ) ;
184+ } ) ;
185+
156186 it ( 'should be disabled if API key is unset' , async ( ) => {
157187 const agent = createAgent ( { apiKey : undefined } ) ;
158188 const response = await agent . get ( '/' ) ;
@@ -174,21 +204,34 @@ describe('withApilytics()', () => {
174204 expect ( data ) . toStrictEqual ( {
175205 path : '/error' ,
176206 method : 'GET' ,
207+ responseSize : 0 ,
177208 timeMillis : expect . any ( Number ) ,
178209 } ) ;
179210 } ) ;
180211
181212 it ( 'should use correct default values' , async ( ) => {
182213 const agent = createAgent ( { apiKey } ) ;
183- const response = await agent . get ( '/empty ' ) ;
214+ const response = await agent . get ( '/no-url-or-method ' ) ;
184215 expect ( response . status ) . toEqual ( 200 ) ;
185216
186217 const data = JSON . parse ( clientRequestMock . write . mock . calls [ 0 ] ) ;
187218 expect ( data ) . toStrictEqual ( {
188219 path : '' ,
189220 method : '' ,
190221 statusCode : 200 ,
222+ responseSize : 0 ,
191223 timeMillis : expect . any ( Number ) ,
192224 } ) ;
193225 } ) ;
226+
227+ it ( 'should handle undefined content lengths' , async ( ) => {
228+ const agent = createAgent ( { apiKey } ) ;
229+ const numberSpy = jest
230+ . spyOn ( global , 'Number' )
231+ . mockImplementation ( ( ) => NaN ) ;
232+ const response = await agent . get ( '/empty' ) ;
233+ numberSpy . mockRestore ( ) ;
234+ expect ( response . status ) . toEqual ( 200 ) ;
235+ expect ( requestSpy ) . toHaveBeenCalledTimes ( 1 ) ;
236+ } ) ;
194237} ) ;
0 commit comments