@@ -2,6 +2,7 @@ import { createProxyMiddleware, createApp, createAppWithPath } from './test-kit'
2
2
import { ErrorRequestHandler } from 'express' ;
3
3
import * as request from 'supertest' ;
4
4
import { getLocal , generateCACertificate , Mockttp } from 'mockttp' ;
5
+ import * as getPort from 'get-port' ;
5
6
6
7
const untrustedCACert = generateCACertificate ( { bits : 1024 } ) ;
7
8
@@ -12,11 +13,19 @@ describe('E2E router', () => {
12
13
let targetServerB : Mockttp ;
13
14
let targetServerC : Mockttp ;
14
15
16
+ let targetPortA : number ;
17
+ let targetPortB : number ;
18
+ let targetPortC : number ;
19
+
15
20
beforeEach ( async ( ) => {
16
21
targetServerA = getLocal ( { https : await untrustedCACert } ) ;
17
22
targetServerB = getLocal ( { https : await untrustedCACert } ) ;
18
23
targetServerC = getLocal ( { https : await untrustedCACert } ) ;
19
24
25
+ targetPortA = await getPort ( ) ;
26
+ targetPortB = await getPort ( ) ;
27
+ targetPortC = await getPort ( ) ;
28
+
20
29
await targetServerA
21
30
. anyRequest ( )
22
31
. thenPassThrough ( { ignoreHostCertificateErrors : [ 'localhost' ] } ) ;
@@ -37,9 +46,9 @@ describe('E2E router', () => {
37
46
. anyRequest ( )
38
47
. thenCallback ( ( { protocol } ) => ( { body : protocol === 'https' ? 'C' : 'NOT HTTPS C' } ) ) ;
39
48
40
- await targetServerA . start ( 6001 ) ;
41
- await targetServerB . start ( 6002 ) ;
42
- await targetServerC . start ( 6003 ) ;
49
+ await targetServerA . start ( targetPortA ) ;
50
+ await targetServerB . start ( targetPortB ) ;
51
+ await targetServerC . start ( targetPortC ) ;
43
52
} ) ;
44
53
45
54
afterEach ( async ( ) => {
@@ -52,11 +61,11 @@ describe('E2E router', () => {
52
61
it ( 'should work with a string' , async ( ) => {
53
62
const app = createApp (
54
63
createProxyMiddleware ( {
55
- target : ' https://localhost:6001' ,
64
+ target : ` https://localhost:${ targetPortA } ` ,
56
65
secure : false ,
57
66
changeOrigin : true ,
58
67
router ( req ) {
59
- return ' https://localhost:6003' ;
68
+ return ` https://localhost:${ targetPortC } ` ;
60
69
} ,
61
70
} )
62
71
) ;
@@ -69,11 +78,11 @@ describe('E2E router', () => {
69
78
it ( 'should work with an object' , async ( ) => {
70
79
const app = createApp (
71
80
createProxyMiddleware ( {
72
- target : ' https://localhost:6001' ,
81
+ target : ` https://localhost:${ targetPortA } ` ,
73
82
secure : false ,
74
83
changeOrigin : true ,
75
84
router ( req ) {
76
- return { host : 'localhost' , port : 6003 , protocol : 'https:' } ;
85
+ return { host : 'localhost' , port : targetPortC , protocol : 'https:' } ;
77
86
} ,
78
87
} )
79
88
) ;
@@ -85,12 +94,12 @@ describe('E2E router', () => {
85
94
it ( 'should work with an async callback' , async ( ) => {
86
95
const app = createApp (
87
96
createProxyMiddleware ( {
88
- target : ' https://localhost:6001' ,
97
+ target : ` https://localhost:${ targetPortA } ` ,
89
98
secure : false ,
90
99
changeOrigin : true ,
91
100
router : async ( req ) => {
92
101
return new Promise ( ( resolve ) =>
93
- resolve ( { host : 'localhost' , port : 6003 , protocol : 'https:' } )
102
+ resolve ( { host : 'localhost' , port : targetPortC , protocol : 'https:' } )
94
103
) ;
95
104
} ,
96
105
} )
@@ -104,7 +113,7 @@ describe('E2E router', () => {
104
113
it ( 'should handle promise rejection in router' , async ( ) => {
105
114
const app = createApp (
106
115
createProxyMiddleware ( {
107
- target : ' https://localhost:6001' ,
116
+ target : ` https://localhost:${ targetPortA } ` ,
108
117
secure : false ,
109
118
changeOrigin : true ,
110
119
router : async ( req ) => {
@@ -125,12 +134,12 @@ describe('E2E router', () => {
125
134
it ( 'missing a : will cause it to use http' , async ( ) => {
126
135
const app = createApp (
127
136
createProxyMiddleware ( {
128
- target : ' https://localhost:6001' ,
137
+ target : ` https://localhost:${ targetPortA } ` ,
129
138
secure : false ,
130
139
changeOrigin : true ,
131
140
router : async ( req ) => {
132
141
return new Promise ( ( resolve ) =>
133
- resolve ( { host : 'localhost' , port : 6003 , protocol : 'https' } )
142
+ resolve ( { host : 'localhost' , port : targetPortC , protocol : 'https' } )
134
143
) ;
135
144
} ,
136
145
} )
@@ -149,13 +158,13 @@ describe('E2E router', () => {
149
158
const app = createAppWithPath (
150
159
'/' ,
151
160
createProxyMiddleware ( {
152
- target : ' https://localhost:6001' ,
161
+ target : ` https://localhost:${ targetPortA } ` ,
153
162
secure : false ,
154
163
changeOrigin : true ,
155
164
router : {
156
- 'alpha.localhost:6000' : ' https://localhost:6001' ,
157
- 'beta.localhost:6000' : ' https://localhost:6002' ,
158
- 'localhost:6000/api' : ' https://localhost:6003' ,
165
+ 'alpha.localhost:6000' : ` https://localhost:${ targetPortA } ` ,
166
+ 'beta.localhost:6000' : ` https://localhost:${ targetPortB } ` ,
167
+ 'localhost:6000/api' : ` https://localhost:${ targetPortC } ` ,
159
168
} ,
160
169
} )
161
170
) ;
0 commit comments