6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import * as http from 'http' ;
9
+ import { createServer } from 'node:http' ;
10
+ import { JasmineBuilderHarness } from '../../../../testing/jasmine-helpers' ;
10
11
import { executeDevServer } from '../../index' ;
11
12
import { executeOnceAndFetch } from '../execute-fetch' ;
12
13
import { describeServeBuilder } from '../jasmine-helpers' ;
13
14
import { BASE_OPTIONS , DEV_SERVER_BUILDER_INFO } from '../setup' ;
14
15
15
- describeServeBuilder ( executeDevServer , DEV_SERVER_BUILDER_INFO , ( harness , setupTarget ) => {
16
+ describeServeBuilder ( executeDevServer , DEV_SERVER_BUILDER_INFO , ( harness , setupTarget , isVite ) => {
16
17
describe ( 'option: "proxyConfig"' , ( ) => {
17
18
beforeEach ( async ( ) => {
18
19
setupTarget ( harness ) ;
@@ -27,21 +28,18 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
27
28
proxyConfig : 'proxy.config.json' ,
28
29
} ) ;
29
30
30
- const proxyServer = createProxyServer ( ) ;
31
+ const proxyServer = await createProxyServer ( ) ;
31
32
try {
32
- await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
33
- const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
34
-
35
33
await harness . writeFiles ( {
36
- 'proxy.config.json' : `{ "/api/*": { "target": "http://127.0.0.1:${ proxyAddress . port } " } }` ,
34
+ 'proxy.config.json' : `{ "/api/*": { "target": "http://127.0.0.1:${ proxyServer . address . port } " } }` ,
37
35
} ) ;
38
36
39
37
const { result, response } = await executeOnceAndFetch ( harness , '/api/test' ) ;
40
38
41
39
expect ( result ?. success ) . toBeTrue ( ) ;
42
40
expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
43
41
} finally {
44
- await new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ;
42
+ await proxyServer . close ( ) ;
45
43
}
46
44
} ) ;
47
45
@@ -51,15 +49,12 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
51
49
proxyConfig : 'proxy.config.json' ,
52
50
} ) ;
53
51
54
- const proxyServer = createProxyServer ( ) ;
52
+ const proxyServer = await createProxyServer ( ) ;
55
53
try {
56
- await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
57
- const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
58
-
59
54
await harness . writeFiles ( {
60
55
'proxy.config.json' : `
61
56
// JSON file with comments
62
- { "/api/*": { "target": "http://127.0.0.1:${ proxyAddress . port } " } }
57
+ { "/api/*": { "target": "http://127.0.0.1:${ proxyServer . address . port } " } }
63
58
` ,
64
59
} ) ;
65
60
@@ -68,7 +63,7 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
68
63
expect ( result ?. success ) . toBeTrue ( ) ;
69
64
expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
70
65
} finally {
71
- await new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ;
66
+ await proxyServer . close ( ) ;
72
67
}
73
68
} ) ;
74
69
@@ -77,22 +72,18 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
77
72
...BASE_OPTIONS ,
78
73
proxyConfig : 'proxy.config.js' ,
79
74
} ) ;
80
-
81
- const proxyServer = createProxyServer ( ) ;
75
+ const proxyServer = await createProxyServer ( ) ;
82
76
try {
83
- await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
84
- const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
85
-
86
77
await harness . writeFiles ( {
87
- 'proxy.config.js' : `module.exports = { "/api/*": { "target": "http://127.0.0.1:${ proxyAddress . port } " } }` ,
78
+ 'proxy.config.js' : `module.exports = { "/api/*": { "target": "http://127.0.0.1:${ proxyServer . address . port } " } }` ,
88
79
} ) ;
89
80
90
81
const { result, response } = await executeOnceAndFetch ( harness , '/api/test' ) ;
91
82
92
83
expect ( result ?. success ) . toBeTrue ( ) ;
93
84
expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
94
85
} finally {
95
- await new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ;
86
+ await proxyServer . close ( ) ;
96
87
}
97
88
} ) ;
98
89
@@ -102,13 +93,10 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
102
93
proxyConfig : 'proxy.config.js' ,
103
94
} ) ;
104
95
105
- const proxyServer = createProxyServer ( ) ;
96
+ const proxyServer = await createProxyServer ( ) ;
106
97
try {
107
- await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
108
- const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
109
-
110
98
await harness . writeFiles ( {
111
- 'proxy.config.js' : `export default { "/api/*": { "target": "http://127.0.0.1:${ proxyAddress . port } " } }` ,
99
+ 'proxy.config.js' : `export default { "/api/*": { "target": "http://127.0.0.1:${ proxyServer . address . port } " } }` ,
112
100
'package.json' : '{ "type": "module" }' ,
113
101
} ) ;
114
102
@@ -117,7 +105,7 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
117
105
expect ( result ?. success ) . toBeTrue ( ) ;
118
106
expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
119
107
} finally {
120
- await new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ;
108
+ await proxyServer . close ( ) ;
121
109
}
122
110
} ) ;
123
111
@@ -127,10 +115,9 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
127
115
proxyConfig : 'proxy.config.cjs' ,
128
116
} ) ;
129
117
130
- const proxyServer = createProxyServer ( ) ;
118
+ const proxyServer = await createProxyServer ( ) ;
131
119
try {
132
- await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
133
- const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
120
+ const proxyAddress = proxyServer . address ;
134
121
135
122
await harness . writeFiles ( {
136
123
'proxy.config.cjs' : `module.exports = { "/api/*": { "target": "http://127.0.0.1:${ proxyAddress . port } " } }` ,
@@ -141,7 +128,7 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
141
128
expect ( result ?. success ) . toBeTrue ( ) ;
142
129
expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
143
130
} finally {
144
- await new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ;
131
+ await proxyServer . close ( ) ;
145
132
}
146
133
} ) ;
147
134
@@ -151,21 +138,18 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
151
138
proxyConfig : 'proxy.config.mjs' ,
152
139
} ) ;
153
140
154
- const proxyServer = createProxyServer ( ) ;
141
+ const proxyServer = await createProxyServer ( ) ;
155
142
try {
156
- await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
157
- const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
158
-
159
143
await harness . writeFiles ( {
160
- 'proxy.config.mjs' : `export default { "/api/*": { "target": "http://127.0.0.1:${ proxyAddress . port } " } }` ,
144
+ 'proxy.config.mjs' : `export default { "/api/*": { "target": "http://127.0.0.1:${ proxyServer . address . port } " } }` ,
161
145
} ) ;
162
146
163
147
const { result, response } = await executeOnceAndFetch ( harness , '/api/test' ) ;
164
148
165
149
expect ( result ?. success ) . toBeTrue ( ) ;
166
150
expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
167
151
} finally {
168
- await new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ;
152
+ await proxyServer . close ( ) ;
169
153
}
170
154
} ) ;
171
155
@@ -175,21 +159,18 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
175
159
proxyConfig : 'proxy.config.json' ,
176
160
} ) ;
177
161
178
- const proxyServer = createProxyServer ( ) ;
162
+ const proxyServer = await createProxyServer ( ) ;
179
163
try {
180
- await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
181
- const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
182
-
183
164
await harness . writeFiles ( {
184
- 'proxy.config.json' : `[ { "context": ["/api", "/abc"], "target": "http://127.0.0.1:${ proxyAddress . port } " } ]` ,
165
+ 'proxy.config.json' : `[ { "context": ["/api", "/abc"], "target": "http://127.0.0.1:${ proxyServer . address . port } " } ]` ,
185
166
} ) ;
186
167
187
168
const { result, response } = await executeOnceAndFetch ( harness , '/api/test' ) ;
188
169
189
170
expect ( result ?. success ) . toBeTrue ( ) ;
190
171
expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
191
172
} finally {
192
- await new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ;
173
+ await proxyServer . close ( ) ;
193
174
}
194
175
} ) ;
195
176
@@ -232,18 +213,39 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
232
213
} ) ,
233
214
) ;
234
215
} ) ;
216
+
217
+ it ( 'supports negatation of globs' , async ( ) => {
218
+ harness . useTarget ( 'serve' , {
219
+ ...BASE_OPTIONS ,
220
+ proxyConfig : 'proxy.config.json' ,
221
+ } ) ;
222
+
223
+ const proxyServer = await createProxyServer ( ) ;
224
+ try {
225
+ await harness . writeFiles ( {
226
+ 'proxy.config.json' : `
227
+ { "!something/**/*": { "target": "http://127.0.0.1:${ proxyServer . address . port } " } }
228
+ ` ,
229
+ } ) ;
230
+
231
+ const { result, response } = await executeOnceAndFetch ( harness , '/api/test' ) ;
232
+
233
+ expect ( result ?. success ) . toBeTrue ( ) ;
234
+ expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
235
+ } finally {
236
+ await proxyServer . close ( ) ;
237
+ }
238
+ } ) ;
235
239
} ) ;
236
240
} ) ;
237
241
238
242
/**
239
243
* Creates an HTTP Server used for proxy testing that provides a `/test` endpoint
240
244
* that returns a 200 response with a body of `TEST_API_RETURN`. All other requests
241
245
* will return a 404 response.
242
- *
243
- * @returns An HTTP Server instance.
244
246
*/
245
- function createProxyServer ( ) {
246
- return http . createServer ( ( request , response ) => {
247
+ async function createProxyServer ( ) {
248
+ const proxyServer = createServer ( ( request , response ) => {
247
249
if ( request . url ?. endsWith ( '/test' ) ) {
248
250
response . writeHead ( 200 ) ;
249
251
response . end ( 'TEST_API_RETURN' ) ;
@@ -252,4 +254,11 @@ function createProxyServer() {
252
254
response . end ( ) ;
253
255
}
254
256
} ) ;
257
+
258
+ await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
259
+
260
+ return {
261
+ address : proxyServer . address ( ) as import ( 'net' ) . AddressInfo ,
262
+ close : ( ) => new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ,
263
+ } ;
255
264
}
0 commit comments