File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -134,4 +134,30 @@ describe('API Routes Tests', () => {
134134 expect ( res . headers ) . toHaveProperty ( 'x-content-type-options' ) ;
135135 } ) ;
136136 } ) ;
137+
138+ describe ( 'GET /api/stats' , ( ) => {
139+ it ( 'should return application statistics' , async ( ) => {
140+ const res = await request ( app ) . get ( '/api/stats' ) ;
141+
142+ expect ( res . statusCode ) . toBe ( 200 ) ;
143+ expect ( res . body ) . toHaveProperty ( 'totalRequests' ) ;
144+ expect ( res . body ) . toHaveProperty ( 'uptime' ) ;
145+ expect ( res . body ) . toHaveProperty ( 'version' ) ;
146+ expect ( res . body ) . toHaveProperty ( 'hostname' ) ;
147+ expect ( res . body ) . toHaveProperty ( 'memory' ) ;
148+ expect ( res . body ) . toHaveProperty ( 'nodeVersion' ) ;
149+ } ) ;
150+
151+ it ( 'should increment request count' , async ( ) => {
152+ const res1 = await request ( app ) . get ( '/api/stats' ) ;
153+ const count1 = res1 . body . totalRequests ;
154+
155+ await request ( app ) . get ( '/' ) ;
156+
157+ const res2 = await request ( app ) . get ( '/api/stats' ) ;
158+ const count2 = res2 . body . totalRequests ;
159+
160+ expect ( count2 ) . toBeGreaterThan ( count1 ) ;
161+ } ) ;
162+ } ) ;
137163} ) ;
Original file line number Diff line number Diff line change @@ -13,6 +13,31 @@ app.use(morgan('combined'));
1313
1414// Compteur de visites (en mémoire pour la démo)
1515let visitCount = 0 ;
16+ let requestCount = 0 ;
17+
18+ // Middleware pour compter les requêtes
19+ app . use ( ( req , res , next ) => {
20+ requestCount ++ ;
21+ next ( ) ;
22+ } ) ;
23+
24+ app . get ( '/api/stats' , ( req , res ) => {
25+ const memUsage = process . memoryUsage ( ) ;
26+
27+ res . json ( {
28+ totalRequests : requestCount ,
29+ uptime : process . uptime ( ) ,
30+ version : '1.1.0' ,
31+ hostname : process . env . HOSTNAME || 'unknown' ,
32+ memory : {
33+ rss : `${ Math . round ( memUsage . rss / 1024 / 1024 ) } MB` ,
34+ heapTotal : `${ Math . round ( memUsage . heapTotal / 1024 / 1024 ) } MB` ,
35+ heapUsed : `${ Math . round ( memUsage . heapUsed / 1024 / 1024 ) } MB` ,
36+ external : `${ Math . round ( memUsage . external / 1024 / 1024 ) } MB`
37+ } ,
38+ nodeVersion : process . version
39+ } ) ;
40+ } ) ;
1641
1742// Routes
1843app . get ( '/' , ( req , res ) => {
You can’t perform that action at this time.
0 commit comments