@@ -33,6 +33,10 @@ async function run () {
33
33
return 'this is /api2/a'
34
34
} )
35
35
36
+ origin . get ( '/variable-api/:id/endpoint' , async ( request , reply ) => {
37
+ return `this is "variable-api" endpoint with id ${ request . params . id } `
38
+ } )
39
+
36
40
origin . get ( '/timeout' , async ( request , reply ) => {
37
41
await new Promise ( ( resolve ) => setTimeout ( resolve , 600 ) )
38
42
return 'this is never received'
@@ -453,7 +457,7 @@ async function run () {
453
457
t . equal ( firstProxyPrefix . body , 'this is /api2/a' )
454
458
} )
455
459
456
- test ( 'rewritePrefix with variables' , async t => {
460
+ test ( 'prefix with variables' , async t => {
457
461
const proxyServer = Fastify ( )
458
462
459
463
proxyServer . register ( proxy , {
@@ -474,6 +478,69 @@ async function run () {
474
478
t . equal ( firstProxyPrefix . body , 'this is /api2/a' )
475
479
} )
476
480
481
+ test ( 'prefix and rewritePrefix with variables' , async t => {
482
+ const proxyServer = Fastify ( )
483
+
484
+ proxyServer . register ( proxy , {
485
+ upstream : `http://localhost:${ origin . server . address ( ) . port } ` ,
486
+ prefix : '/api/:id' ,
487
+ rewritePrefix : '/variable-api/:id'
488
+ } )
489
+
490
+ await proxyServer . listen ( { port : 0 } )
491
+
492
+ t . teardown ( ( ) => {
493
+ proxyServer . close ( )
494
+ } )
495
+
496
+ const firstProxyPrefix = await got (
497
+ `http://localhost:${ proxyServer . server . address ( ) . port } /api/123/endpoint`
498
+ )
499
+ t . equal ( firstProxyPrefix . body , 'this is "variable-api" endpoint with id 123' )
500
+ } )
501
+
502
+ test ( 'prefix (complete path) and rewritePrefix with variables and similar path' , async t => {
503
+ const proxyServer = Fastify ( )
504
+
505
+ proxyServer . register ( proxy , {
506
+ upstream : `http://localhost:${ origin . server . address ( ) . port } ` ,
507
+ prefix : '/api/:id/static' ,
508
+ rewritePrefix : '/variable-api/:id/endpoint'
509
+ } )
510
+
511
+ await proxyServer . listen ( { port : 0 } )
512
+
513
+ t . teardown ( ( ) => {
514
+ proxyServer . close ( )
515
+ } )
516
+
517
+ const firstProxyPrefix = await got (
518
+ `http://localhost:${ proxyServer . server . address ( ) . port } /api/123/static`
519
+ )
520
+ t . equal ( firstProxyPrefix . body , 'this is "variable-api" endpoint with id 123' )
521
+ } )
522
+
523
+ test ( 'prefix and rewritePrefix with variables with different paths' , async t => {
524
+ const proxyServer = Fastify ( )
525
+
526
+ proxyServer . register ( proxy , {
527
+ upstream : `http://localhost:${ origin . server . address ( ) . port } ` ,
528
+ prefix : '/:id' ,
529
+ rewritePrefix : '/variable-api/:id/endpoint'
530
+ } )
531
+
532
+ await proxyServer . listen ( { port : 0 } )
533
+
534
+ t . teardown ( ( ) => {
535
+ proxyServer . close ( )
536
+ } )
537
+
538
+ const firstProxyPrefix = await got (
539
+ `http://localhost:${ proxyServer . server . address ( ) . port } /123`
540
+ )
541
+ t . equal ( firstProxyPrefix . body , 'this is "variable-api" endpoint with id 123' )
542
+ } )
543
+
477
544
test ( 'rewrite location headers' , async t => {
478
545
const proxyServer = Fastify ( )
479
546
0 commit comments