@@ -6,40 +6,50 @@ import * as nock from 'nock';
6
6
describe ( 'test/express.test.ts' , function ( ) {
7
7
let app ;
8
8
beforeAll ( async ( ) => {
9
- nock ( 'https://gw.alicdn.com' ) . get ( '/tfs/TB1.1EzoBBh1e4jSZFhXXcC9VXa-48-48.png?version=123' ) . reply ( 200 , '123' , { 'content-type' : 'image/png' } ) ;
10
- nock ( 'https://www.baidu.com' ) . get ( '/' ) . reply ( 200 , '<html>123</html>' , { 'content-type' : 'text/html' } ) ;
11
- nock ( 'https://sm.bdimg.com' ) . get ( '/static/wiseindex/amd_modules/@searchfe/assert_3ed54c3.js' ) . reply ( 200 , '123' , { 'content-type' : 'application/x-javascript' } ) ;
9
+ nock ( 'https://gw.alicdn.com' ) . get ( '/tfs/TB1.1EzoBBh1e4jSZFhXXcC9VXa-48-48.png?version=123' ) . reply ( 200 , '123' , { 'content-type' : 'image/png' } ) ;
10
+ nock ( 'https://www.baidu.com' ) . get ( '/' ) . reply ( 200 , '<html>123</html>' , { 'content-type' : 'text/html' } ) ;
11
+ //奇怪, 同一个链接只能用一次, 再用nock会报404
12
+ nock ( 'https://www.baidu.com' ) . get ( '/1234' ) . reply ( 200 , '<html>1234</html>' , { 'content-type' : 'text/html' } ) ;
13
+ nock ( 'https://www.baidu.com' ) . get ( '/5678' ) . reply ( 200 , '<html>5678</html>' , { 'content-type' : 'text/html' } ) ;
14
+ nock ( 'https://sm.bdimg.com' ) . get ( '/static/wiseindex/amd_modules/@searchfe/assert_3ed54c3.js' ) . reply ( 200 , '123' , { 'content-type' : 'application/x-javascript' } ) ;
12
15
nock ( 'https://httpbin.org' )
13
16
. persist ( )
14
17
. get ( '/get?name=midway' ) . reply ( 200 , {
15
- "args" : {
16
- "name" : "midway"
17
- } ,
18
- "headers" : {
19
- "Host" : "httpbin.org" ,
20
- } ,
21
- "url" : "https://httpbin.org/get?name=midway"
22
- } , { 'content-type' : 'application/json' } )
18
+ "args" : {
19
+ "name" : "midway"
20
+ } ,
21
+ "headers" : {
22
+ "Host" : "httpbin.org" ,
23
+ } ,
24
+ "url" : "https://httpbin.org/get?name=midway"
25
+ } , { 'content-type' : 'application/json' } )
23
26
. post ( '/post' ) . reply ( 200 , function ( uri , requestBody ) {
24
- const body = {
25
- 'headers' : {
26
- 'Host' : 'httpbin.org' ,
27
- 'Content-Type' : this . req . headers [ 'content-type' ] ,
28
- 'url' : 'https://httpbin.org/post'
29
- } ,
27
+ const body = {
28
+ 'headers' : {
29
+ 'Host' : 'httpbin.org' ,
30
+ 'Content-Type' : this . req . headers [ 'content-type' ] ,
30
31
'url' : 'https://httpbin.org/post'
31
- } as any ;
32
- if ( this . req . headers [ 'content-type' ] === 'application/x-www-form-urlencoded' ) {
33
- body . form = {
34
- "name" : "midway"
35
- } ;
36
- }
37
- if ( this . req . headers [ 'content-type' ] === 'application/json' ) {
38
- body . data = JSON . stringify ( requestBody ) ;
39
- }
40
- return body ;
41
- } , { 'content-type' : 'application/json' } ) ;
42
-
32
+ } ,
33
+ 'url' : 'https://httpbin.org/post'
34
+ } as any ;
35
+ if ( this . req . headers [ 'content-type' ] === 'application/x-www-form-urlencoded' ) {
36
+ body . form = {
37
+ "name" : "midway"
38
+ } ;
39
+ }
40
+ if ( this . req . headers [ 'content-type' ] === 'application/json' ) {
41
+ body . data = JSON . stringify ( requestBody ) ;
42
+ }
43
+ return body ;
44
+ } , { 'content-type' : 'application/json' } ) ;
45
+ nock ( 'https://aliyun.com' ) . get ( '/1234' ) . reply ( 302 , '<html>123</html>' , {
46
+ 'content-type' : 'text/html' ,
47
+ Location : "https://www.baidu.com/1234"
48
+ } ) ;
49
+ nock ( 'https://aliyun.com' ) . get ( '/5678' ) . reply ( 302 , '<html>456</html>' , {
50
+ 'content-type' : 'text/html' ,
51
+ Location : "https://www.baidu.com/5678"
52
+ } ) ;
43
53
const appDir = join ( __dirname , 'fixtures/express' ) ;
44
54
app = await createApp ( appDir ) ;
45
55
} )
@@ -98,14 +108,14 @@ describe('test/express.test.ts', function () {
98
108
it ( 'post json to httpbin' , async ( ) => {
99
109
const request = await createHttpRequest ( app ) ;
100
110
await request . post ( '/httpbin/post' )
101
- . send ( { name : 'midway' } )
111
+ . send ( { name : 'midway' } )
102
112
. set ( 'Accept' , 'application/json' )
103
113
. expect ( 200 )
104
114
. then ( async response => {
105
115
assert ( response . status === 200 )
106
116
assert ( response . body . url === 'https://httpbin.org/post' ) ;
107
117
assert ( response . body . headers [ 'Content-Type' ] === 'application/json' ) ;
108
- assert ( response . body . data === JSON . stringify ( { name : 'midway' } ) ) ;
118
+ assert ( response . body . data === JSON . stringify ( { name : 'midway' } ) ) ;
109
119
} ) ;
110
120
} ) ;
111
121
@@ -122,4 +132,22 @@ describe('test/express.test.ts', function () {
122
132
assert ( response . body . form . name === 'midway' ) ;
123
133
} ) ;
124
134
} ) ;
135
+ it ( 'canredirects' , async ( ) => {
136
+ const request = await createHttpRequest ( app ) ;
137
+ await request . get ( '/canredirects/1234' )
138
+ . expect ( 200 ) . then ( async response => {
139
+
140
+ assert ( response . text === "<html>1234</html>" )
141
+ } ) ;
142
+ } ) ;
143
+
144
+ // express不懂为啥不支持
145
+ // it('noredirects', async () => {
146
+ // const request = await createHttpRequest(app);
147
+ // await request.get('/noredirects/5678')
148
+ // .expect(302).then(async response => {
149
+ // assert(response.text === "<html>456</html>")
150
+ // });
151
+ // });
152
+
125
153
} ) ;
0 commit comments