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