@@ -3,18 +3,22 @@ import type http from 'http';
33
44import { milliSecondTimer , sendApilyticsMetrics } from '../src' ;
55
6+ const APILYTICS_VERSION = require ( '../package.json' ) . version ;
7+
68describe ( 'sendApilyticsMetrics()' , ( ) => {
79 const OLD_ENV = process . env ;
10+ const apiKey = 'dummy-key' ;
811
912 const params = {
10- apiKey : 'dummy-key' ,
13+ apiKey,
1114 path : '/' ,
1215 method : 'GET' ,
1316 statusCode : 200 ,
1417 timeMillis : 10 ,
1518 } ;
1619
1720 let consoleErrorSpy : jest . SpyInstance ;
21+ let requestSpy : jest . SpyInstance ;
1822
1923 const clientRequestMock = {
2024 on : jest . fn ( ) . mockImplementation ( ( event , handler ) => {
@@ -28,7 +32,7 @@ describe('sendApilyticsMetrics()', () => {
2832 jest . resetModules ( ) ;
2933 process . env = { ...OLD_ENV } ;
3034
31- jest
35+ requestSpy = jest
3236 . spyOn ( https , 'request' )
3337 . mockImplementation (
3438 ( ) => clientRequestMock as unknown as http . ClientRequest ,
@@ -46,6 +50,64 @@ describe('sendApilyticsMetrics()', () => {
4650 process . env = OLD_ENV ;
4751 } ) ;
4852
53+ it ( 'should call apilytics API' , async ( ) => {
54+ sendApilyticsMetrics ( params ) ;
55+
56+ expect ( requestSpy ) . toHaveBeenCalledTimes ( 1 ) ;
57+
58+ expect ( APILYTICS_VERSION ) . toBeTruthy ( ) ;
59+ expect ( process . versions . node ) . toBeTruthy ( ) ;
60+
61+ expect ( requestSpy ) . toHaveBeenLastCalledWith ( {
62+ hostname : 'www.apilytics.io' ,
63+ port : 443 ,
64+ path : '/api/v1/middleware' ,
65+ method : 'POST' ,
66+ headers : {
67+ 'Content-Type' : 'application/json' ,
68+ 'Content-Length' : expect . any ( Number ) ,
69+ 'X-API-Key' : apiKey ,
70+ 'Apilytics-Version' : `apilytics-node-core/${ APILYTICS_VERSION } ;node/${ process . versions . node } ` ,
71+ } ,
72+ } ) ;
73+
74+ expect ( clientRequestMock . on ) . toHaveBeenCalledTimes ( 1 ) ;
75+ expect ( clientRequestMock . write ) . toHaveBeenCalledTimes ( 1 ) ;
76+ expect ( clientRequestMock . end ) . toHaveBeenCalledTimes ( 1 ) ;
77+
78+ const data = JSON . parse ( clientRequestMock . write . mock . calls [ 0 ] ) ;
79+ expect ( data ) . toStrictEqual ( {
80+ path : '/' ,
81+ method : 'GET' ,
82+ statusCode : 200 ,
83+ timeMillis : expect . any ( Number ) ,
84+ } ) ;
85+ expect ( data [ 'timeMillis' ] ) . toEqual ( Math . trunc ( data [ 'timeMillis' ] ) ) ;
86+ } ) ;
87+
88+ it ( 'should allow to pass optional `apilyticsIntegration` and `integratedLibrary` params' , async ( ) => {
89+ sendApilyticsMetrics ( {
90+ ...params ,
91+ apilyticsIntegration : 'dummy' ,
92+ integratedLibrary : 'lib/1.2.3' ,
93+ } ) ;
94+
95+ expect ( requestSpy ) . toHaveBeenCalledTimes ( 1 ) ;
96+
97+ expect ( requestSpy ) . toHaveBeenLastCalledWith ( {
98+ hostname : 'www.apilytics.io' ,
99+ port : 443 ,
100+ path : '/api/v1/middleware' ,
101+ method : 'POST' ,
102+ headers : {
103+ 'Content-Type' : 'application/json' ,
104+ 'Content-Length' : expect . any ( Number ) ,
105+ 'X-API-Key' : apiKey ,
106+ 'Apilytics-Version' : `dummy/${ APILYTICS_VERSION } ;node/${ process . versions . node } ;lib/1.2.3` ,
107+ } ,
108+ } ) ;
109+ } ) ;
110+
49111 it ( 'should hide HTTP errors in production' , async ( ) => {
50112 // @ts -ignore: Assigning to a read-only property.
51113 process . env . NODE_ENV = 'production' ;
0 commit comments