1
1
import type { Server } from 'http' ;
2
2
3
+ import { expect } from 'chai' ;
3
4
import type { Express } from 'express' ;
4
5
5
6
import { setupServer } from '.' ;
6
7
7
- const retryCount = {
8
- add : 0 ,
9
- update : 0 ,
10
- delete : 0 ,
11
- } ;
8
+ const retryCount : Record <
9
+ string ,
10
+ {
11
+ add : number ;
12
+ update : number ;
13
+ delete : number ;
14
+ }
15
+ > = { } ;
16
+
17
+ export function assertValidWaitForApiKey ( expectedCount : number ) : void {
18
+ expect ( Object . keys ( retryCount ) . length ) . to . be . equal ( expectedCount ) ;
19
+ for ( const retry of Object . values ( retryCount ) ) {
20
+ expect ( retry ) . to . deep . equal ( {
21
+ add : 0 ,
22
+ update : 0 ,
23
+ delete : 0 ,
24
+ } ) ;
25
+ }
26
+ }
12
27
13
28
function addRoutes ( app : Express ) : void {
14
29
app . get ( '/1/keys/:key' , ( req , res ) => {
15
- if ( req . params . key === 'api-key-add-operation-test' ) {
16
- if ( retryCount . add < 3 ) {
30
+ const lang = req . params . key . split ( '-' ) . at ( - 1 ) as string ;
31
+ if ( ! retryCount [ lang ] ) {
32
+ retryCount [ lang ] = {
33
+ add : 0 ,
34
+ update : 0 ,
35
+ delete : 0 ,
36
+ } ;
37
+ }
38
+ const retry = retryCount [ lang ] ;
39
+ if ( req . params . key === `api-key-add-operation-test-${ lang } ` ) {
40
+ if ( retry . add < 3 ) {
17
41
res . status ( 404 ) . json ( { message : `API key doesn't exist` } ) ;
18
- } else if ( retryCount . add === 3 ) {
42
+ } else if ( retry . add === 3 ) {
19
43
res . status ( 200 ) . json ( {
20
44
value : req . params . key ,
21
45
description : 'my new api key' ,
@@ -26,16 +50,15 @@ function addRoutes(app: Express): void {
26
50
createdAt : 1720094400 ,
27
51
} ) ;
28
52
29
- retryCount . add = - 1 ;
53
+ retry . add = - 1 ;
30
54
} else {
31
- // eslint-disable-next-line no-console
32
- console . error ( `Invalid retry count: ${ retryCount . add } ` ) ;
33
- res . status ( 500 ) . json ( { message : `Internal Server Error` } ) ;
55
+ expect ( retry . add ) . to . be . lessThan ( 3 ) ;
56
+ return ;
34
57
}
35
58
36
- retryCount . add += 1 ;
37
- } else if ( req . params . key === ' api-key-update-operation-test' ) {
38
- if ( retryCount . update < 3 ) {
59
+ retry . add += 1 ;
60
+ } else if ( req . params . key === ` api-key-update-operation-test- ${ lang } ` ) {
61
+ if ( retry . update < 3 ) {
39
62
res . status ( 200 ) . json ( {
40
63
value : req . params . key ,
41
64
description : 'my new api key' ,
@@ -45,7 +68,7 @@ function addRoutes(app: Express): void {
45
68
maxHitsPerQuery : 20 ,
46
69
createdAt : 1720094400 ,
47
70
} ) ;
48
- } else if ( retryCount . update === 3 ) {
71
+ } else if ( retry . update === 3 ) {
49
72
res . status ( 200 ) . json ( {
50
73
value : req . params . key ,
51
74
description : 'my updated api key' ,
@@ -56,16 +79,15 @@ function addRoutes(app: Express): void {
56
79
createdAt : 1720094400 ,
57
80
} ) ;
58
81
59
- retryCount . update = - 1 ;
82
+ retry . update = - 1 ;
60
83
} else {
61
- // eslint-disable-next-line no-console
62
- console . error ( `Invalid retry count: ${ retryCount . update } ` ) ;
63
- res . status ( 500 ) . json ( { message : `Internal Server Error` } ) ;
84
+ expect ( retry . update ) . to . be . lessThan ( 3 ) ;
85
+ return ;
64
86
}
65
87
66
- retryCount . update += 1 ;
67
- } else if ( req . params . key === ' api-key-delete-operation-test' ) {
68
- if ( retryCount . delete < 3 ) {
88
+ retry . update += 1 ;
89
+ } else if ( req . params . key === ` api-key-delete-operation-test- ${ lang } ` ) {
90
+ if ( retry . delete < 3 ) {
69
91
res . status ( 200 ) . json ( {
70
92
value : req . params . key ,
71
93
description : 'my updated api key' ,
@@ -75,36 +97,20 @@ function addRoutes(app: Express): void {
75
97
maxHitsPerQuery : 20 ,
76
98
createdAt : 1720094400 ,
77
99
} ) ;
78
- } else if ( retryCount . delete === 3 ) {
100
+ } else if ( retry . delete === 3 ) {
79
101
res . status ( 404 ) . json ( { message : `API key doesn't exist` } ) ;
80
102
81
- retryCount . delete = - 1 ;
103
+ retry . delete = - 1 ;
82
104
} else {
83
- // eslint-disable-next-line no-console
84
- console . error ( `Invalid retry count: ${ retryCount . delete } ` ) ;
85
- res . status ( 500 ) . json ( { message : `Internal Server Error` } ) ;
105
+ expect ( retry . delete ) . to . be . lessThan ( 3 ) ;
106
+ return ;
86
107
}
87
108
88
- retryCount . delete += 1 ;
109
+ retry . delete += 1 ;
89
110
} else {
90
- // eslint-disable-next-line no-console
91
- console . error ( `Invalid API key ${ req . params . key } ` ) ;
92
- res . status ( 500 ) . json ( { message : `Internal Server Error` } ) ;
111
+ throw new Error ( `Invalid API key ${ req . params . key } ` ) ;
93
112
}
94
113
} ) ;
95
-
96
- // fallback route
97
- app . use ( ( req , res ) => {
98
- // eslint-disable-next-line no-console
99
- console . log ( 'fallback route' , req . method , req . url ) ;
100
- res . status ( 500 ) . json ( { message : `Internal Server Error (fallback)` } ) ;
101
- } ) ;
102
-
103
- app . use ( ( err , req , res , _ ) => {
104
- // eslint-disable-next-line no-console
105
- console . error ( err . message ) ;
106
- res . status ( 500 ) . send ( { message : err . message } ) ;
107
- } ) ;
108
114
}
109
115
110
116
export function waitForApiKeyServer ( ) : Promise < Server > {
0 commit comments