1
- 'use strict' ;
1
+ import { strict as assert } from 'node:assert' ;
2
+ import { once } from 'node:events' ;
3
+ import urllib from 'urllib' ;
4
+ import { createApp , MockApplication , restore , mm } from '../../utils.js' ;
2
5
3
- const assert = require ( 'assert' ) ;
4
- const mm = require ( 'egg-mock' ) ;
5
- const urllib = require ( 'urllib' ) ;
6
- const utils = require ( '../../utils' ) ;
7
-
8
- describe ( 'test/app/extend/request.test.js' , ( ) => {
6
+ describe ( 'test/app/extend/request.test.ts' , ( ) => {
9
7
describe ( 'normal' , ( ) => {
10
- let app ;
8
+ let app : MockApplication ;
11
9
let ctx ;
12
- let req ;
10
+ let req : any ;
13
11
before ( ( ) => {
14
- app = utils . app ( 'apps/demo' ) ;
12
+ app = createApp ( 'apps/demo' ) ;
15
13
return app . ready ( ) ;
16
14
} ) ;
17
15
after ( ( ) => app . close ( ) ) ;
18
16
beforeEach ( ( ) => {
19
17
ctx = app . mockContext ( ) ;
20
18
req = ctx . request ;
21
19
} ) ;
22
- afterEach ( mm . restore ) ;
20
+ afterEach ( restore ) ;
23
21
24
22
describe ( 'req.host' , ( ) => {
25
23
it ( 'should return host with port' , ( ) => {
@@ -180,8 +178,8 @@ describe('test/app/extend/request.test.js', () => {
180
178
181
179
it ( 'should return value from socket.encrypted' , ( ) => {
182
180
const ctx = app . mockContext ( ) ;
183
- ctx . request . socket . encrypted = true ;
184
- assert ( ctx . request . protocol === 'https' ) ;
181
+ ( ctx . request as any ) . socket . encrypted = true ;
182
+ assert . equal ( ctx . request . protocol , 'https' ) ;
185
183
} ) ;
186
184
} ) ;
187
185
@@ -208,7 +206,7 @@ describe('test/app/extend/request.test.js', () => {
208
206
} ) ;
209
207
210
208
describe ( 'this.query[key] => String' , ( ) => {
211
- function expectQuery ( querystring , query ) {
209
+ function expectQuery ( querystring : any , query : any ) {
212
210
mm ( req , 'querystring' , querystring ) ;
213
211
assert . deepEqual ( req . query , query ) ;
214
212
mm . restore ( ) ;
@@ -240,7 +238,7 @@ describe('test/app/extend/request.test.js', () => {
240
238
} ) ;
241
239
242
240
describe ( 'this.queries[key] => Array' , ( ) => {
243
- function expectQueries ( querystring , query ) {
241
+ function expectQueries ( querystring : any , query : any ) {
244
242
mm ( req , 'querystring' , querystring ) ;
245
243
assert . deepEqual ( req . queries , query ) ;
246
244
mm . restore ( ) ;
@@ -370,41 +368,36 @@ describe('test/app/extend/request.test.js', () => {
370
368
} ) ;
371
369
372
370
describe ( 'work with egg app' , ( ) => {
373
- let app ;
374
- let host ;
371
+ let app : MockApplication ;
372
+ let host : string ;
375
373
before ( ( ) => {
376
- app = utils . app ( 'apps/querystring-extended' ) ;
374
+ app = createApp ( 'apps/querystring-extended' ) ;
377
375
return app . ready ( ) ;
378
376
} ) ;
379
- before ( done => {
380
- app . listen ( 0 , function ( ) {
381
- host = `http://127.0.0.1:${ this . address ( ) . port } ` ;
382
- done ( ) ;
383
- } ) ;
377
+ before ( async ( ) => {
378
+ const server = app . listen ( 0 ) ;
379
+ await once ( server , 'listening' ) ;
380
+ host = `http://127.0.0.1:${ server . address ( ) . port } ` ;
384
381
} ) ;
385
382
after ( ( ) => app . close ( ) ) ;
386
383
387
- it ( 'should return query and queries' , done => {
388
- urllib . request ( `${ host } /?p=a,b&p=b,c&a[foo]=bar` , {
384
+ it ( 'should return query and queries' , async ( ) => {
385
+ const res = await urllib . request ( `${ host } /?p=a,b&p=b,c&a[foo]=bar` , {
389
386
dataType : 'json' ,
390
- } , ( err , body ) => {
391
- assert . deepEqual ( body , {
392
- query : { p : 'a,b' , 'a[foo]' : 'bar' } ,
393
- queries : { p : [ 'a,b' , 'b,c' ] , 'a[foo]' : [ 'bar' ] } ,
394
- } ) ;
395
- done ( err ) ;
387
+ } ) ;
388
+ assert . deepEqual ( res . data , {
389
+ query : { p : 'a,b' , 'a[foo]' : 'bar' } ,
390
+ queries : { p : [ 'a,b' , 'b,c' ] , 'a[foo]' : [ 'bar' ] } ,
396
391
} ) ;
397
392
} ) ;
398
393
399
- it ( 'should work with encodeURIComponent' , done => {
400
- urllib . request ( `${ host } /?p=a,b&p=b,c&${ encodeURIComponent ( 'a[foo]' ) } =bar` , {
394
+ it ( 'should work with encodeURIComponent' , async ( ) => {
395
+ const res = await urllib . request ( `${ host } /?p=a,b&p=b,c&${ encodeURIComponent ( 'a[foo]' ) } =bar` , {
401
396
dataType : 'json' ,
402
- } , ( err , body ) => {
403
- assert . deepEqual ( body , {
404
- query : { p : 'a,b' , 'a[foo]' : 'bar' } ,
405
- queries : { p : [ 'a,b' , 'b,c' ] , 'a[foo]' : [ 'bar' ] } ,
406
- } ) ;
407
- done ( err ) ;
397
+ } ) ;
398
+ assert . deepEqual ( res . data , {
399
+ query : { p : 'a,b' , 'a[foo]' : 'bar' } ,
400
+ queries : { p : [ 'a,b' , 'b,c' ] , 'a[foo]' : [ 'bar' ] } ,
408
401
} ) ;
409
402
} ) ;
410
403
} ) ;
0 commit comments