1
- 'use strict' ;
2
-
3
- const assert = require ( 'assert' ) ;
4
- const formstream = require ( 'formstream' ) ;
5
- const urllib = require ( 'urllib' ) ;
6
- const path = require ( 'path' ) ;
7
- const mock = require ( 'egg-mock' ) ;
8
- const fs = require ( 'fs' ) . promises ;
9
- const dayjs = require ( 'dayjs' ) ;
10
- const sleep = ms => new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
11
-
12
- describe ( 'test/file-mode.test.js' , ( ) => {
13
- let app ;
14
- let server ;
15
- let host ;
1
+ import assert from 'node:assert' ;
2
+ import path from 'node:path' ;
3
+ import fs from 'node:fs/promises' ;
4
+ import { scheduler } from 'node:timers/promises' ;
5
+ import { fileURLToPath } from 'node:url' ;
6
+ import { mm , mock , MockApplication } from '@eggjs/mock' ;
7
+ import dayjs from 'dayjs' ;
8
+ import formstream from 'formstream' ;
9
+ import urllib from 'urllib' ;
10
+
11
+ const __filename = fileURLToPath ( import . meta. url ) ;
12
+ const __dirname = path . dirname ( __filename ) ;
13
+
14
+ describe ( 'test/file-mode.test.ts' , ( ) => {
15
+ let app : MockApplication ;
16
+ let server : any ;
17
+ let host : string ;
16
18
before ( ( ) => {
17
- app = mock . app ( {
19
+ app = mm . app ( {
18
20
baseDir : 'apps/file-mode' ,
19
21
} ) ;
20
22
return app . ready ( ) ;
@@ -29,7 +31,7 @@ describe('test/file-mode.test.js', () => {
29
31
after ( ( ) => app . close ( ) ) ;
30
32
after ( ( ) => server . close ( ) ) ;
31
33
beforeEach ( ( ) => app . mockCsrf ( ) ) ;
32
- afterEach ( mock . restore ) ;
34
+ afterEach ( mm . restore ) ;
33
35
34
36
it ( 'should ignore non multipart request' , async ( ) => {
35
37
const res = await app . httpRequest ( )
@@ -62,32 +64,32 @@ describe('test/file-mode.test.js', () => {
62
64
const res = await urllib . request ( host + '/upload' , {
63
65
method : 'POST' ,
64
66
headers,
65
- stream : form ,
67
+ stream : form as any ,
66
68
} ) ;
67
69
68
70
assert ( res . status === 200 ) ;
69
71
const data = JSON . parse ( res . data ) ;
70
72
assert . deepStrictEqual ( data . body , { foo : 'fengmk2' , love : 'egg' , work : 'with Node.js' } ) ;
71
73
assert ( data . files . length === 3 ) ;
72
74
assert ( data . files [ 0 ] . field === 'file1' ) ;
73
- assert ( data . files [ 0 ] . filename === 'foooooooo.js' ) ;
75
+ assert . equal ( data . files [ 0 ] . filename , 'foooooooo.js' ) ;
74
76
assert ( data . files [ 0 ] . encoding === '7bit' ) ;
75
77
assert ( data . files [ 0 ] . mime === 'application/javascript' ) ;
76
78
assert ( data . files [ 0 ] . filepath . startsWith ( app . config . multipart . tmpdir ) ) ;
77
79
78
80
assert ( data . files [ 1 ] . field === 'file2' ) ;
79
81
assert ( data . files [ 1 ] . fieldname === 'file2' ) ;
80
- assert ( data . files [ 1 ] . filename === 'file-mode.test.js ' ) ;
82
+ assert . equal ( data . files [ 1 ] . filename , 'file-mode.test.ts ' ) ;
81
83
assert ( data . files [ 1 ] . encoding === '7bit' ) ;
82
84
assert ( data . files [ 1 ] . transferEncoding === '7bit' ) ;
83
- assert ( data . files [ 1 ] . mime === 'application/javascript ') ;
84
- assert ( data . files [ 1 ] . mimeType === 'application/javascript ') ;
85
+ assert . equal ( data . files [ 1 ] . mime , 'video/mp2t ') ;
86
+ assert . equal ( data . files [ 1 ] . mimeType , 'video/mp2t ') ;
85
87
assert ( data . files [ 1 ] . filepath . startsWith ( app . config . multipart . tmpdir ) ) ;
86
88
87
89
assert ( data . files [ 2 ] . field === 'bigfile' ) ;
88
- assert ( data . files [ 2 ] . filename === 'bigfile.js' ) ;
90
+ assert . equal ( data . files [ 2 ] . filename , 'bigfile.js' ) ;
89
91
assert ( data . files [ 2 ] . encoding === '7bit' ) ;
90
- assert ( data . files [ 2 ] . mime === 'application/javascript' ) ;
92
+ assert . equal ( data . files [ 2 ] . mime , 'application/javascript' ) ;
91
93
assert ( data . files [ 2 ] . filepath . startsWith ( app . config . multipart . tmpdir ) ) ;
92
94
} ) ;
93
95
@@ -98,18 +100,18 @@ describe('test/file-mode.test.js', () => {
98
100
const res = await urllib . request ( host + '/upload' , {
99
101
method : 'POST' ,
100
102
headers,
101
- stream : form ,
103
+ stream : form as any ,
102
104
} ) ;
103
105
assert ( res . status === 200 ) ;
104
106
const data = JSON . parse ( res . data ) ;
105
- assert ( data . files . length === 1 ) ;
106
- assert ( data . files [ 0 ] . field === 'file' ) ;
107
- assert ( data . files [ 0 ] . filename === '10mb.js' ) ;
108
- assert ( data . files [ 0 ] . encoding === '7bit' ) ;
109
- assert ( data . files [ 0 ] . mime === 'application/octet-stream' ) ;
107
+ assert . equal ( data . files . length , 1 ) ;
108
+ assert . equal ( data . files [ 0 ] . field , 'file' ) ;
109
+ assert . equal ( data . files [ 0 ] . filename , '10mb.js' ) ;
110
+ assert . equal ( data . files [ 0 ] . encoding , '7bit' ) ;
111
+ assert . equal ( data . files [ 0 ] . mime , 'application/octet-stream' ) ;
110
112
assert ( data . files [ 0 ] . filepath . startsWith ( app . config . multipart . tmpdir ) ) ;
111
113
const stat = await fs . stat ( data . files [ 0 ] . filepath ) ;
112
- assert ( stat . size === 10 * 1024 * 1024 - 1 ) ;
114
+ assert . equal ( stat . size , 10 * 1024 * 1024 - 1 ) ;
113
115
} ) ;
114
116
115
117
it ( 'should 200 when field size just 100kb' , async ( ) => {
@@ -120,7 +122,7 @@ describe('test/file-mode.test.js', () => {
120
122
const res = await urllib . request ( host + '/upload' , {
121
123
method : 'POST' ,
122
124
headers,
123
- stream : form ,
125
+ stream : form as any ,
124
126
} ) ;
125
127
126
128
assert ( res . status === 200 ) ;
@@ -138,7 +140,7 @@ describe('test/file-mode.test.js', () => {
138
140
const res = await urllib . request ( host + '/upload' , {
139
141
method : 'POST' ,
140
142
headers,
141
- stream : form ,
143
+ stream : form as any ,
142
144
} ) ;
143
145
144
146
assert ( res . status === 200 ) ;
@@ -156,7 +158,7 @@ describe('test/file-mode.test.js', () => {
156
158
const res = await urllib . request ( host + '/upload' , {
157
159
method : 'POST' ,
158
160
headers,
159
- stream : form ,
161
+ stream : form as any ,
160
162
} ) ;
161
163
162
164
assert ( res . status === 200 ) ;
@@ -183,7 +185,7 @@ describe('test/file-mode.test.js', () => {
183
185
const res = await urllib . request ( host + '/upload' , {
184
186
method : 'POST' ,
185
187
headers,
186
- stream : form ,
188
+ stream : form as any ,
187
189
} ) ;
188
190
189
191
assert ( res . status === 413 ) ;
@@ -201,7 +203,7 @@ describe('test/file-mode.test.js', () => {
201
203
const res = await urllib . request ( host + '/upload' , {
202
204
method : 'POST' ,
203
205
headers,
204
- stream : form ,
206
+ stream : form as any ,
205
207
} ) ;
206
208
207
209
assert ( res . status === 413 ) ;
@@ -216,7 +218,7 @@ describe('test/file-mode.test.js', () => {
216
218
const res = await urllib . request ( host + '/upload' , {
217
219
method : 'POST' ,
218
220
headers,
219
- stream : form ,
221
+ stream : form as any ,
220
222
} ) ;
221
223
222
224
assert ( res . status === 413 ) ;
@@ -233,7 +235,7 @@ describe('test/file-mode.test.js', () => {
233
235
const res = await urllib . request ( host + '/upload' , {
234
236
method : 'POST' ,
235
237
headers,
236
- stream : form ,
238
+ stream : form as any ,
237
239
} ) ;
238
240
239
241
assert ( res . status === 413 ) ;
@@ -252,7 +254,7 @@ describe('test/file-mode.test.js', () => {
252
254
const res = await urllib . request ( host + '/upload' , {
253
255
method : 'POST' ,
254
256
headers,
255
- stream : form ,
257
+ stream : form as any ,
256
258
} ) ;
257
259
258
260
assert ( res . status === 413 ) ;
@@ -271,7 +273,7 @@ describe('test/file-mode.test.js', () => {
271
273
const res = await urllib . request ( host + '/upload' , {
272
274
method : 'POST' ,
273
275
headers,
274
- stream : form ,
276
+ stream : form as any ,
275
277
} ) ;
276
278
277
279
assert ( res . status === 400 ) ;
@@ -289,10 +291,10 @@ describe('test/file-mode.test.js', () => {
289
291
const res = await urllib . request ( host + '/upload?call_multipart_twice=1' , {
290
292
method : 'POST' ,
291
293
headers,
292
- stream : form ,
294
+ stream : form as any ,
293
295
} ) ;
294
296
295
- assert ( res . status === 500 ) ;
297
+ assert . equal ( res . status , 500 ) ;
296
298
assert ( res . data . toString ( ) . includes ( 'the multipart request can\'t be consumed twice' ) ) ;
297
299
} ) ;
298
300
@@ -307,7 +309,7 @@ describe('test/file-mode.test.js', () => {
307
309
const res = await urllib . request ( host + '/upload?cleanup=true' , {
308
310
method : 'POST' ,
309
311
headers,
310
- stream : form ,
312
+ stream : form as any ,
311
313
} ) ;
312
314
313
315
assert ( res . status === 200 ) ;
@@ -326,7 +328,7 @@ describe('test/file-mode.test.js', () => {
326
328
const res = await urllib . request ( host + '/upload?async_cleanup=true' , {
327
329
method : 'POST' ,
328
330
headers,
329
- stream : form ,
331
+ stream : form as any ,
330
332
} ) ;
331
333
332
334
assert ( res . status === 200 ) ;
@@ -339,15 +341,15 @@ describe('test/file-mode.test.js', () => {
339
341
// [egg-schedule]: register schedule /hello/egg-multipart/app/schedule/clean_tmpdir.js
340
342
const logger = app . loggers . scheduleLogger ;
341
343
const content = await fs . readFile ( logger . options . file , 'utf8' ) ;
342
- assert ( / \[ e g g - s c h e d u l e \] : r e g i s t e r s c h e d u l e .+ c l e a n _ t m p d i r \. j s / . test ( content ) ) ;
344
+ assert . match ( content , / \[ @ e g g j s \/ s c h e d u l e \] : r e g i s t e r s c h e d u l e .+ c l e a n _ t m p d i r \. t s / ) ;
343
345
} ) ;
344
346
345
347
it ( 'should remove nothing' , async ( ) => {
346
348
app . mockLog ( ) ;
347
- await app . runSchedule ( path . join ( __dirname , '../app/schedule/clean_tmpdir' ) ) ;
348
- await sleep ( 1000 ) ;
349
- app . expectLog ( '[egg- multipart:CleanTmpdir] start clean tmpdir: "' , 'coreLogger' ) ;
350
- app . expectLog ( '[egg- multipart:CleanTmpdir] end' , 'coreLogger' ) ;
349
+ await app . runSchedule ( path . join ( __dirname , '../src/ app/schedule/clean_tmpdir' ) ) ;
350
+ await scheduler . wait ( 1000 ) ;
351
+ app . expectLog ( '[@eggjs/ multipart:CleanTmpdir] start clean tmpdir: "' , 'coreLogger' ) ;
352
+ app . expectLog ( '[@eggjs/ multipart:CleanTmpdir] end' , 'coreLogger' ) ;
351
353
} ) ;
352
354
353
355
it ( 'should remove old dirs' , async ( ) => {
@@ -366,7 +368,7 @@ describe('test/file-mode.test.js', () => {
366
368
const currentMonth = new Date ( ) . getMonth ( ) ;
367
369
const fourMonthBefore = path . join ( app . config . multipart . tmpdir , dayjs ( ) . subtract ( 4 , 'months' ) . format ( 'YYYY/MM/DD/HH' ) ) ;
368
370
if ( currentMonth < 4 ) {
369
- // if current month is less than April, four months before shoule be last year.
371
+ // if current month is less than April, four months before should be last year.
370
372
oldDirs . push ( fourMonthBefore ) ;
371
373
} else {
372
374
shouldKeepDirs . push ( fourMonthBefore ) ;
@@ -380,7 +382,7 @@ describe('test/file-mode.test.js', () => {
380
382
} ) ) ;
381
383
382
384
app . mockLog ( ) ;
383
- await app . runSchedule ( path . join ( __dirname , '../app/schedule/clean_tmpdir' ) ) ;
385
+ await app . runSchedule ( path . join ( __dirname , '../src/ app/schedule/clean_tmpdir' ) ) ;
384
386
for ( const dir of oldDirs ) {
385
387
const exists = await fs . access ( dir ) . then ( ( ) => true ) . catch ( ( ) => false ) ;
386
388
assert ( ! exists , dir ) ;
@@ -389,8 +391,8 @@ describe('test/file-mode.test.js', () => {
389
391
const exists = await fs . access ( dir ) . then ( ( ) => true ) . catch ( ( ) => false ) ;
390
392
assert ( exists , dir ) ;
391
393
}
392
- app . expectLog ( '[egg- multipart:CleanTmpdir] removing tmpdir: "' , 'coreLogger' ) ;
393
- app . expectLog ( '[egg- multipart:CleanTmpdir:success] tmpdir: "' , 'coreLogger' ) ;
394
+ app . expectLog ( '[@eggjs/ multipart:CleanTmpdir] removing tmpdir: "' , 'coreLogger' ) ;
395
+ app . expectLog ( '[@eggjs/ multipart:CleanTmpdir:success] tmpdir: "' , 'coreLogger' ) ;
394
396
} ) ;
395
397
} ) ;
396
398
@@ -405,7 +407,7 @@ describe('test/file-mode.test.js', () => {
405
407
const res = await urllib . request ( host + '/upload' , {
406
408
method : 'POST' ,
407
409
headers,
408
- stream : form ,
410
+ stream : form as any ,
409
411
dataType : 'json' ,
410
412
} ) ;
411
413
assert . deepStrictEqual ( res . data . body , { foo : 'egg' } ) ;
@@ -423,7 +425,7 @@ describe('test/file-mode.test.js', () => {
423
425
const res = await urllib . request ( host + '/upload' , {
424
426
method : 'POST' ,
425
427
headers,
426
- stream : form ,
428
+ stream : form as any ,
427
429
dataType : 'json' ,
428
430
} ) ;
429
431
assert . deepStrictEqual ( res . data . body , { foo : [ 'fengmk2' , 'like' , 'egg' ] } ) ;
0 commit comments