1
1
import { afterAll , beforeAll , describe , expect , test } from "vitest" ;
2
2
import path from "path" ;
3
3
import { existsSync , rmSync } from "fs" ;
4
- import { ChildProcessWithoutNullStreams , spawn } from "child_process" ;
4
+ import { ChildProcessWithoutNullStreams , spawn , spawnSync } from "child_process" ;
5
5
import axios , { AxiosInstance , AxiosResponse } from "axios" ;
6
6
7
7
import { sampleData } from "./__fixtures__/sample-data" ;
@@ -11,6 +11,46 @@ import { testCacheDirPath } from "../src/constants";
11
11
import { ReqResConfig , StartCommandOptions } from "./types" ;
12
12
13
13
describe ( "caching-proxy-cli" , ( ) => {
14
+ const originHost = "localhost" ;
15
+ const originPort = 3000 ;
16
+ const originHref = `http://${ originHost } :${ originPort } ` ;
17
+ const proxyServerHost = "localhost" ;
18
+ const proxyPort = "4000" ;
19
+ const proxyHref = `http://${ proxyServerHost } :${ proxyPort } ` ;
20
+
21
+ const reqResConfigs : ReqResConfig [ ] = [
22
+ {
23
+ requestPath : "/api/sample-json" ,
24
+ responseContentType : "application/json" ,
25
+ responseData : sampleData . json ,
26
+ } ,
27
+ {
28
+ requestPath : "/api/sample-xml" ,
29
+ responseContentType : "application/xml" ,
30
+ responseData : sampleData . xml ,
31
+ } ,
32
+ {
33
+ requestPath : "/api/sample-plain-txt" ,
34
+ responseContentType : "text/plain" ,
35
+ responseData : sampleData . plainTxt ,
36
+ } ,
37
+ {
38
+ requestPath : "/public/sample-html" ,
39
+ responseContentType : "text/html" ,
40
+ responseData : sampleData . html ,
41
+ } ,
42
+ {
43
+ requestPath : "/public/sample-styles.css" ,
44
+ responseContentType : "text/css" ,
45
+ responseData : sampleData . css ,
46
+ } ,
47
+ {
48
+ requestPath : "/public/sample-script" ,
49
+ responseContentType : "application/javascript" ,
50
+ responseData : sampleData . javascript ,
51
+ } ,
52
+ ] ;
53
+
14
54
describe ( "start command with invalid options" , ( ) => {
15
55
const invalidOptions : StartCommandOptions [ ] = [
16
56
{ origin : "" , port : "" } ,
@@ -67,13 +107,6 @@ describe("caching-proxy-cli", () => {
67
107
} ) ;
68
108
69
109
describe ( "start command with valid options" , ( ) => {
70
- const originHost = "localhost" ;
71
- const originPort = 3000 ;
72
- const originHref = `http://${ originHost } :${ originPort } ` ;
73
- const proxyServerHost = "localhost" ;
74
- const proxyPort = "4000" ;
75
- const proxyHref = `http://${ proxyServerHost } :${ proxyPort } ` ;
76
-
77
110
let proxyChildProcess : ChildProcessWithoutNullStreams ;
78
111
79
112
setupMockServer ( { host : originHost , port : originPort } ) ;
@@ -120,39 +153,6 @@ describe("caching-proxy-cli", () => {
120
153
} ) ;
121
154
} ) ;
122
155
123
- const reqResConfigs : ReqResConfig [ ] = [
124
- {
125
- requestPath : "/api/sample-json" ,
126
- responseContentType : "application/json" ,
127
- responseData : sampleData . json ,
128
- } ,
129
- {
130
- requestPath : "/api/sample-xml" ,
131
- responseContentType : "application/xml" ,
132
- responseData : sampleData . xml ,
133
- } ,
134
- {
135
- requestPath : "/api/sample-plain-txt" ,
136
- responseContentType : "text/plain" ,
137
- responseData : sampleData . plainTxt ,
138
- } ,
139
- {
140
- requestPath : "/public/sample-html" ,
141
- responseContentType : "text/html" ,
142
- responseData : sampleData . html ,
143
- } ,
144
- {
145
- requestPath : "/public/sample-styles.css" ,
146
- responseContentType : "text/css" ,
147
- responseData : sampleData . css ,
148
- } ,
149
- {
150
- requestPath : "/public/sample-script" ,
151
- responseContentType : "application/javascript" ,
152
- responseData : sampleData . javascript ,
153
- } ,
154
- ] ;
155
-
156
156
describe . each ( reqResConfigs ) (
157
157
"when proxy server is fetched for $responseContentType response content type API" ,
158
158
( config ) => {
@@ -220,4 +220,79 @@ describe("caching-proxy-cli", () => {
220
220
}
221
221
) ;
222
222
} ) ;
223
+
224
+ describe ( "'clear-cache' command" , ( ) => {
225
+ let proxyChildProcess : ChildProcessWithoutNullStreams ;
226
+
227
+ setupMockServer ( { host : originHost , port : originPort } ) ;
228
+
229
+ beforeAll ( async ( ) => {
230
+ proxyChildProcess = spawn ( "ts-node" , [
231
+ path . join ( __dirname , "../src/cli.ts" ) ,
232
+ "start" ,
233
+ originHref ,
234
+ "-p" ,
235
+ proxyPort ,
236
+ ] ) ;
237
+
238
+ // Wait for the proxy server to start
239
+ await new Promise < void > ( ( resolve , reject ) => {
240
+ const timeout = 5 * 1000 ;
241
+
242
+ proxyChildProcess . stdout . once ( "data" , ( chunk : any ) => {
243
+ const data = chunk . toString ( ) ;
244
+
245
+ if ( / c a c h i n g p r o x y r u n n i n g / gi. test ( data ) ) {
246
+ resolve ( ) ;
247
+ } else {
248
+ reject ( new Error ( "Unexpected stdout data" ) ) ;
249
+ }
250
+ } ) ;
251
+
252
+ setTimeout ( ( ) => {
253
+ const err = new Error (
254
+ `Failed to receive proxyChildProcess.stdout 'data' event within ${
255
+ timeout / 1000
256
+ } s`
257
+ ) ;
258
+ reject ( err ) ;
259
+ } , timeout ) ;
260
+ } ) ;
261
+
262
+ // Make a request to have something in cache directory to delete
263
+ const axiosInstance = axios . create ( {
264
+ baseURL : proxyHref ,
265
+ } ) ;
266
+
267
+ try {
268
+ await axiosInstance . get ( reqResConfigs [ 0 ] . requestPath ) ;
269
+ } catch ( e ) {
270
+ throw e ;
271
+ }
272
+ } ) ;
273
+
274
+ afterAll ( ( ) => {
275
+ const res = proxyChildProcess ?. kill ( "SIGTERM" ) ;
276
+ console . log (
277
+ `Kill ${ res ? "succeeded" : "failed" } for the spawned process`
278
+ ) ;
279
+ } ) ;
280
+
281
+ describe ( "when 'clear-cache' command is executed" , ( ) => {
282
+ let data : string | undefined = undefined
283
+
284
+ beforeAll ( ( ) => {
285
+ const buffer = spawnSync ( "ts-node" , [
286
+ path . join ( __dirname , "../src/cli.ts" ) ,
287
+ "clear-cache" ,
288
+ ] ) ;
289
+
290
+ data = buffer . stdout . toString ( )
291
+ } )
292
+
293
+ test ( "should clear the cache directory" , ( ) => {
294
+ expect ( data ) . toMatch ( / c l e a r e d t h e c a c h e s u c c e s s f u l l y / gi)
295
+ } ) ;
296
+ } ) ;
297
+ } ) ;
223
298
} ) ;
0 commit comments