7
7
*/
8
8
9
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 ) ;
@@ -235,6 +236,15 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
235
236
await proxyServer . close ( ) ;
236
237
}
237
238
} ) ;
239
+
240
+ /**
241
+ * ****************************************************************************************************
242
+ * ********************************** Below only Vite specific tests **********************************
243
+ * ****************************************************************************************************
244
+ */
245
+ if ( isVite ) {
246
+ viteOnlyTests ( harness ) ;
247
+ }
238
248
} ) ;
239
249
} ) ;
240
250
@@ -261,3 +271,54 @@ async function createProxyServer() {
261
271
close : ( ) => new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ,
262
272
} ;
263
273
}
274
+
275
+ /**
276
+ * Vite specific tests
277
+ */
278
+ function viteOnlyTests ( harness : JasmineBuilderHarness < unknown > ) : void {
279
+ it ( 'proxies support regexp as context' , async ( ) => {
280
+ harness . useTarget ( 'serve' , {
281
+ ...BASE_OPTIONS ,
282
+ proxyConfig : 'proxy.config.json' ,
283
+ } ) ;
284
+
285
+ const proxyServer = await createProxyServer ( ) ;
286
+ try {
287
+ await harness . writeFiles ( {
288
+ 'proxy.config.json' : `
289
+ { "^/api/.*": { "target": "http://127.0.0.1:${ proxyServer . address . port } " } }
290
+ ` ,
291
+ } ) ;
292
+
293
+ const { result, response } = await executeOnceAndFetch ( harness , '/api/test' ) ;
294
+
295
+ expect ( result ?. success ) . toBeTrue ( ) ;
296
+ expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
297
+ } finally {
298
+ await proxyServer . close ( ) ;
299
+ }
300
+ } ) ;
301
+
302
+ it ( 'proxies support negated regexp as context' , async ( ) => {
303
+ harness . useTarget ( 'serve' , {
304
+ ...BASE_OPTIONS ,
305
+ proxyConfig : 'proxy.config.json' ,
306
+ } ) ;
307
+
308
+ const proxyServer = await createProxyServer ( ) ;
309
+ try {
310
+ await harness . writeFiles ( {
311
+ 'proxy.config.json' : `
312
+ { "^\\/(?!something).*": { "target": "http://127.0.0.1:${ proxyServer . address . port } " } }
313
+ ` ,
314
+ } ) ;
315
+
316
+ const { result, response } = await executeOnceAndFetch ( harness , '/api/test' ) ;
317
+
318
+ expect ( result ?. success ) . toBeTrue ( ) ;
319
+ expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
320
+ } finally {
321
+ await proxyServer . close ( ) ;
322
+ }
323
+ } ) ;
324
+ }
0 commit comments