@@ -33,6 +33,10 @@ async function run () {
3333 return 'this is /api2/a'
3434 } )
3535
36+ origin . get ( '/variable-api/:id/endpoint' , async ( request , reply ) => {
37+ return `this is "variable-api" endpoint with id ${ request . params . id } `
38+ } )
39+
3640 origin . get ( '/timeout' , async ( request , reply ) => {
3741 await new Promise ( ( resolve ) => setTimeout ( resolve , 600 ) )
3842 return 'this is never received'
@@ -453,7 +457,7 @@ async function run () {
453457 t . equal ( firstProxyPrefix . body , 'this is /api2/a' )
454458 } )
455459
456- test ( 'rewritePrefix with variables' , async t => {
460+ test ( 'prefix with variables' , async t => {
457461 const proxyServer = Fastify ( )
458462
459463 proxyServer . register ( proxy , {
@@ -474,6 +478,69 @@ async function run () {
474478 t . equal ( firstProxyPrefix . body , 'this is /api2/a' )
475479 } )
476480
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+
477544 test ( 'rewrite location headers' , async t => {
478545 const proxyServer = Fastify ( )
479546
0 commit comments