1
1
import { createProxyMiddleware , createApp , createAppWithPath } from './_utils' ;
2
2
import * as request from 'supertest' ;
3
- import { getLocal , Mockttp } from 'mockttp' ;
3
+ import { getLocal , generateCACertificate , Mockttp } from 'mockttp' ;
4
+
5
+ const untrustedCACert = generateCACertificate ( { bits : 1024 } ) ;
6
+
7
+ process . env . NODE_TLS_REJECT_UNAUTHORIZED = '0' ;
4
8
5
9
describe ( 'E2E router' , ( ) => {
6
10
let targetServerA : Mockttp ;
7
11
let targetServerB : Mockttp ;
8
12
let targetServerC : Mockttp ;
9
13
10
14
beforeEach ( async ( ) => {
11
- targetServerA = getLocal ( ) ;
12
- targetServerB = getLocal ( ) ;
13
- targetServerC = getLocal ( ) ;
15
+ targetServerA = getLocal ( { https : await untrustedCACert } ) ;
16
+ targetServerB = getLocal ( { https : await untrustedCACert } ) ;
17
+ targetServerC = getLocal ( { https : await untrustedCACert } ) ;
18
+
19
+ await targetServerA
20
+ . anyRequest ( )
21
+ . thenPassThrough ( { ignoreHostCertificateErrors : [ 'localhost' ] } ) ;
22
+ await targetServerB
23
+ . anyRequest ( )
24
+ . thenPassThrough ( { ignoreHostCertificateErrors : [ 'localhost' ] } ) ;
25
+ await targetServerC
26
+ . anyRequest ( )
27
+ . thenPassThrough ( { ignoreHostCertificateErrors : [ 'localhost' ] } ) ;
28
+
29
+ await targetServerA
30
+ . anyRequest ( )
31
+ . thenCallback ( ( { protocol } ) => ( { body : protocol === 'https' ? 'A' : 'NOT HTTPS A' } ) ) ;
32
+ await targetServerB
33
+ . anyRequest ( )
34
+ . thenCallback ( ( { protocol } ) => ( { body : protocol === 'https' ? 'B' : 'NOT HTTPS B' } ) ) ;
35
+ await targetServerC
36
+ . anyRequest ( )
37
+ . thenCallback ( ( { protocol } ) => ( { body : protocol === 'https' ? 'C' : 'NOT HTTPS C' } ) ) ;
14
38
15
39
await targetServerA . start ( 6001 ) ;
16
40
await targetServerB . start ( 6002 ) ;
17
41
await targetServerC . start ( 6003 ) ;
18
-
19
- targetServerA . get ( ) . thenReply ( 200 , 'A' ) ;
20
- targetServerB . get ( ) . thenReply ( 200 , 'B' ) ;
21
- targetServerC . get ( ) . thenReply ( 200 , 'C' ) ;
22
42
} ) ;
23
43
24
44
afterEach ( async ( ) => {
@@ -27,13 +47,50 @@ describe('E2E router', () => {
27
47
await targetServerC . stop ( ) ;
28
48
} ) ;
29
49
30
- describe ( 'router with proxyTable' , ( ) => {
31
- it ( 'should proxy to: "localhost:6003/api"' , async ( ) => {
50
+ describe ( 'router with req' , ( ) => {
51
+ it ( 'should work with a string' , async ( ) => {
52
+ const app = createApp (
53
+ createProxyMiddleware ( {
54
+ target : 'https://localhost:6001' ,
55
+ secure : false ,
56
+ changeOrigin : true ,
57
+ router ( req ) {
58
+ return 'https://localhost:6003' ;
59
+ }
60
+ } )
61
+ ) ;
62
+
63
+ const agent = request ( app ) ;
64
+ const response = await agent . get ( '/api' ) . expect ( 200 ) ;
65
+ expect ( response . text ) . toBe ( 'C' ) ;
66
+ } ) ;
67
+
68
+ it ( 'should work with an object' , async ( ) => {
32
69
const app = createApp (
33
70
createProxyMiddleware ( {
34
- target : `http://localhost:6001` ,
71
+ target : 'https://localhost:6001' ,
72
+ secure : false ,
73
+ changeOrigin : true ,
35
74
router ( req ) {
36
- return 'http://localhost:6003' ;
75
+ return { host : 'localhost' , port : 6003 , protocol : 'https:' } ;
76
+ }
77
+ } )
78
+ ) ;
79
+ const agent = request ( app ) ;
80
+ const response = await agent . get ( '/api' ) . expect ( 200 ) ;
81
+ expect ( response . text ) . toBe ( 'C' ) ;
82
+ } ) ;
83
+
84
+ it ( 'should work with an async callback' , async ( ) => {
85
+ const app = createApp (
86
+ createProxyMiddleware ( {
87
+ target : 'https://localhost:6001' ,
88
+ secure : false ,
89
+ changeOrigin : true ,
90
+ router : async req => {
91
+ return new Promise ( resolve =>
92
+ resolve ( { host : 'localhost' , port : 6003 , protocol : 'https:' } )
93
+ ) ;
37
94
}
38
95
} )
39
96
) ;
@@ -42,6 +99,25 @@ describe('E2E router', () => {
42
99
const response = await agent . get ( '/api' ) . expect ( 200 ) ;
43
100
expect ( response . text ) . toBe ( 'C' ) ;
44
101
} ) ;
102
+
103
+ it ( 'missing a : will cause it to use http' , async ( ) => {
104
+ const app = createApp (
105
+ createProxyMiddleware ( {
106
+ target : 'https://localhost:6001' ,
107
+ secure : false ,
108
+ changeOrigin : true ,
109
+ router : async req => {
110
+ return new Promise ( resolve =>
111
+ resolve ( { host : 'localhost' , port : 6003 , protocol : 'https' } )
112
+ ) ;
113
+ }
114
+ } )
115
+ ) ;
116
+
117
+ const agent = request ( app ) ;
118
+ const response = await agent . get ( '/api' ) . expect ( 200 ) ;
119
+ expect ( response . text ) . toBe ( 'NOT HTTPS C' ) ;
120
+ } ) ;
45
121
} ) ;
46
122
47
123
describe ( 'router with proxyTable' , ( ) => {
@@ -51,11 +127,13 @@ describe('E2E router', () => {
51
127
const app = createAppWithPath (
52
128
'/' ,
53
129
createProxyMiddleware ( {
54
- target : 'http://localhost:6001' ,
130
+ target : 'https://localhost:6001' ,
131
+ secure : false ,
132
+ changeOrigin : true ,
55
133
router : {
56
- 'alpha.localhost:6000' : 'http ://localhost:6001' ,
57
- 'beta.localhost:6000' : 'http ://localhost:6002' ,
58
- 'localhost:6000/api' : 'http ://localhost:6003'
134
+ 'alpha.localhost:6000' : 'https ://localhost:6001' ,
135
+ 'beta.localhost:6000' : 'https ://localhost:6002' ,
136
+ 'localhost:6000/api' : 'https ://localhost:6003'
59
137
}
60
138
} )
61
139
) ;
0 commit comments