11import * as https from 'https' ;
2- import { parse } from 'url' ;
32import { IoHelper } from '../../../lib/api-private' ;
43import { CliIoHost } from '../../../lib/cli/io-host' ;
54import { EndpointTelemetrySink } from '../../../lib/cli/telemetry/endpoint-sink' ;
@@ -88,16 +87,15 @@ describe('EndpointTelemetrySink', () => {
8887 test ( 'makes a POST request to the specified endpoint' , async ( ) => {
8988 // GIVEN
9089 const mockRequest = setupMockRequest ( ) ;
91- const endpoint = parse ( 'https://example.com/telemetry' ) ;
9290 const testEvent = createTestEvent ( 'test' , { foo : 'bar' } ) ;
93- const client = new EndpointTelemetrySink ( { endpoint, ioHost } ) ;
91+ const client = new EndpointTelemetrySink ( { endpoint : 'https://example.com/telemetry' , ioHost } ) ;
9492
9593 // WHEN
9694 await client . emit ( testEvent ) ;
9795 await client . flush ( ) ;
9896
9997 // THEN
100- const expectedPayload = JSON . stringify ( [ testEvent ] ) ;
98+ const expectedPayload = JSON . stringify ( { events : [ testEvent ] } ) ;
10199 expect ( https . request ) . toHaveBeenCalledWith ( {
102100 hostname : 'example.com' ,
103101 port : null ,
@@ -117,9 +115,8 @@ describe('EndpointTelemetrySink', () => {
117115 test ( 'silently catches request errors' , async ( ) => {
118116 // GIVEN
119117 const mockRequest = setupMockRequest ( ) ;
120- const endpoint = parse ( 'https://example.com/telemetry' ) ;
121118 const testEvent = createTestEvent ( 'test' ) ;
122- const client = new EndpointTelemetrySink ( { endpoint, ioHost } ) ;
119+ const client = new EndpointTelemetrySink ( { endpoint : 'https://example.com/telemetry' , ioHost } ) ;
123120
124121 mockRequest . on . mockImplementation ( ( event , callback ) => {
125122 if ( event === 'error' ) {
@@ -137,18 +134,17 @@ describe('EndpointTelemetrySink', () => {
137134 test ( 'multiple events sent as one' , async ( ) => {
138135 // GIVEN
139136 const mockRequest = setupMockRequest ( ) ;
140- const endpoint = parse ( 'https://example.com/telemetry' ) ;
141137 const testEvent1 = createTestEvent ( 'test1' , { foo : 'bar' } ) ;
142138 const testEvent2 = createTestEvent ( 'test2' , { foo : 'bazoo' } ) ;
143- const client = new EndpointTelemetrySink ( { endpoint, ioHost } ) ;
139+ const client = new EndpointTelemetrySink ( { endpoint : 'https://example.com/telemetry' , ioHost } ) ;
144140
145141 // WHEN
146142 await client . emit ( testEvent1 ) ;
147143 await client . emit ( testEvent2 ) ;
148144 await client . flush ( ) ;
149145
150146 // THEN
151- const expectedPayload = JSON . stringify ( [ testEvent1 , testEvent2 ] ) ;
147+ const expectedPayload = JSON . stringify ( { events : [ testEvent1 , testEvent2 ] } ) ;
152148 expect ( https . request ) . toHaveBeenCalledTimes ( 1 ) ;
153149 expect ( https . request ) . toHaveBeenCalledWith ( {
154150 hostname : 'example.com' ,
@@ -169,10 +165,9 @@ describe('EndpointTelemetrySink', () => {
169165 test ( 'successful flush clears events cache' , async ( ) => {
170166 // GIVEN
171167 setupMockRequest ( ) ;
172- const endpoint = parse ( 'https://example.com/telemetry' ) ;
173168 const testEvent1 = createTestEvent ( 'test1' , { foo : 'bar' } ) ;
174169 const testEvent2 = createTestEvent ( 'test2' , { foo : 'bazoo' } ) ;
175- const client = new EndpointTelemetrySink ( { endpoint, ioHost } ) ;
170+ const client = new EndpointTelemetrySink ( { endpoint : 'https://example.com/telemetry' , ioHost } ) ;
176171
177172 // WHEN
178173 await client . emit ( testEvent1 ) ;
@@ -181,7 +176,7 @@ describe('EndpointTelemetrySink', () => {
181176 await client . flush ( ) ;
182177
183178 // THEN
184- const expectedPayload1 = JSON . stringify ( [ testEvent1 ] ) ;
179+ const expectedPayload1 = JSON . stringify ( { events : [ testEvent1 ] } ) ;
185180 expect ( https . request ) . toHaveBeenCalledTimes ( 2 ) ;
186181 expect ( https . request ) . toHaveBeenCalledWith ( {
187182 hostname : 'example.com' ,
@@ -196,7 +191,7 @@ describe('EndpointTelemetrySink', () => {
196191 timeout : 500 ,
197192 } , expect . anything ( ) ) ;
198193
199- const expectedPayload2 = JSON . stringify ( [ testEvent2 ] ) ;
194+ const expectedPayload2 = JSON . stringify ( { events : [ testEvent2 ] } ) ;
200195 expect ( https . request ) . toHaveBeenCalledWith ( {
201196 hostname : 'example.com' ,
202197 port : null ,
@@ -238,10 +233,9 @@ describe('EndpointTelemetrySink', () => {
238233 return mockRequest ;
239234 } ) ;
240235
241- const endpoint = parse ( 'https://example.com/telemetry' ) ;
242236 const testEvent1 = createTestEvent ( 'test1' , { foo : 'bar' } ) ;
243237 const testEvent2 = createTestEvent ( 'test2' , { foo : 'bazoo' } ) ;
244- const client = new EndpointTelemetrySink ( { endpoint, ioHost } ) ;
238+ const client = new EndpointTelemetrySink ( { endpoint : 'https://example.com/telemetry' , ioHost } ) ;
245239
246240 // WHEN
247241 await client . emit ( testEvent1 ) ;
@@ -255,7 +249,7 @@ describe('EndpointTelemetrySink', () => {
255249 await client . flush ( ) ;
256250
257251 // THEN
258- const expectedPayload1 = JSON . stringify ( [ testEvent1 ] ) ;
252+ const expectedPayload1 = JSON . stringify ( { events : [ testEvent1 ] } ) ;
259253 expect ( https . request ) . toHaveBeenCalledTimes ( 2 ) ;
260254 expect ( https . request ) . toHaveBeenCalledWith ( {
261255 hostname : 'example.com' ,
@@ -270,15 +264,15 @@ describe('EndpointTelemetrySink', () => {
270264 timeout : 500 ,
271265 } , expect . anything ( ) ) ;
272266
273- const expectedPayload2 = JSON . stringify ( [ testEvent2 ] ) ;
267+ const expectedPayload2 = JSON . stringify ( { events : [ testEvent1 , testEvent2 ] } ) ;
274268 expect ( https . request ) . toHaveBeenCalledWith ( {
275269 hostname : 'example.com' ,
276270 port : null ,
277271 path : '/telemetry' ,
278272 method : 'POST' ,
279273 headers : {
280274 'content-type' : 'application/json' ,
281- 'content-length' : expectedPayload1 . length + expectedPayload2 . length - 1 ,
275+ 'content-length' : expectedPayload2 . length ,
282276 } ,
283277 agent : undefined ,
284278 timeout : 500 ,
@@ -289,13 +283,12 @@ describe('EndpointTelemetrySink', () => {
289283 // GIVEN
290284 jest . useFakeTimers ( ) ;
291285 setupMockRequest ( ) ; // Setup the mock request but we don't need the return value
292- const endpoint = parse ( 'https://example.com/telemetry' ) ;
293286
294287 // Create a spy on setInterval
295288 const setIntervalSpy = jest . spyOn ( global , 'setInterval' ) ;
296289
297290 // Create the client
298- const client = new EndpointTelemetrySink ( { endpoint, ioHost } ) ;
291+ const client = new EndpointTelemetrySink ( { endpoint : 'https://example.com/telemetry' , ioHost } ) ;
299292
300293 // Create a spy on the flush method
301294 const flushSpy = jest . spyOn ( client , 'flush' ) ;
@@ -337,8 +330,7 @@ describe('EndpointTelemetrySink', () => {
337330 // Mock IoHelper.fromActionAwareIoHost to return our mock
338331 jest . spyOn ( IoHelper , 'fromActionAwareIoHost' ) . mockReturnValue ( mockIoHelper as any ) ;
339332
340- const endpoint = parse ( 'https://example.com/telemetry' ) ;
341- const client = new EndpointTelemetrySink ( { endpoint, ioHost } ) ;
333+ const client = new EndpointTelemetrySink ( { endpoint : 'https://example.com/telemetry' , ioHost } ) ;
342334
343335 // Mock https.request to throw an error
344336 ( https . request as jest . Mock ) . mockImplementation ( ( ) => {
0 commit comments