Fix proxy redirect for base path without slash
Signed-off-by: Tim Hansen <timbonicus@gmail.com>
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-proxy-backend': patch
|
||||
---
|
||||
|
||||
Fixed proxy requests to the base URL of routes without a trailing slash redirecting to the `target` with the full path appended.
|
||||
@@ -74,7 +74,7 @@ describe('buildMiddleware', () => {
|
||||
expect(filter('', { method: 'PATCH', headers: {} })).toBe(true);
|
||||
expect(filter('', { method: 'DELETE', headers: {} })).toBe(true);
|
||||
|
||||
expect(fullConfig.pathRewrite).toEqual({ '^/proxy/test/': '/' });
|
||||
expect(fullConfig.pathRewrite).toEqual({ '^/proxy/test/?': '/' });
|
||||
expect(fullConfig.changeOrigin).toBe(true);
|
||||
expect(fullConfig.logProvider!(logger)).toBe(logger);
|
||||
});
|
||||
@@ -94,7 +94,7 @@ describe('buildMiddleware', () => {
|
||||
expect(filter('', { method: 'PATCH', headers: {} })).toBe(true);
|
||||
expect(filter('', { method: 'DELETE', headers: {} })).toBe(true);
|
||||
|
||||
expect(fullConfig.pathRewrite).toEqual({ '^/proxy/test/': '/' });
|
||||
expect(fullConfig.pathRewrite).toEqual({ '^/proxy/test/?': '/' });
|
||||
expect(fullConfig.changeOrigin).toBe(true);
|
||||
expect(fullConfig.logProvider!(logger)).toBe(logger);
|
||||
});
|
||||
@@ -114,7 +114,7 @@ describe('buildMiddleware', () => {
|
||||
expect(filter('', { method: 'PATCH', headers: {} })).toBe(true);
|
||||
expect(filter('', { method: 'DELETE', headers: {} })).toBe(true);
|
||||
|
||||
expect(fullConfig.pathRewrite).toEqual({ '^/proxy/test/': '/' });
|
||||
expect(fullConfig.pathRewrite).toEqual({ '^/proxy/test/?': '/' });
|
||||
expect(fullConfig.changeOrigin).toBe(true);
|
||||
expect(fullConfig.logProvider!(logger)).toBe(logger);
|
||||
});
|
||||
@@ -137,7 +137,7 @@ describe('buildMiddleware', () => {
|
||||
expect(filter('', { method: 'PATCH', headers: {} })).toBe(false);
|
||||
expect(filter('', { method: 'DELETE', headers: {} })).toBe(true);
|
||||
|
||||
expect(fullConfig.pathRewrite).toEqual({ '^/proxy/test/': '/' });
|
||||
expect(fullConfig.pathRewrite).toEqual({ '^/proxy/test/?': '/' });
|
||||
expect(fullConfig.changeOrigin).toBe(true);
|
||||
expect(fullConfig.logProvider!(logger)).toBe(logger);
|
||||
});
|
||||
|
||||
@@ -95,8 +95,12 @@ export function buildMiddleware(
|
||||
routeWithSlash = routeWithSlash.substring(1);
|
||||
}
|
||||
|
||||
// The ? makes the slash optional for the rewrite, so that a base path without an ending slash
|
||||
// will also be matched (e.g. '/sample' and then requesting just '/api/proxy/sample' without an
|
||||
// ending slash). Otherwise the target gets called with the full '/api/proxy/sample' path
|
||||
// appended.
|
||||
fullConfig.pathRewrite = {
|
||||
[`^${pathPrefix}${routeWithSlash}`]: '/',
|
||||
[`^${pathPrefix}${routeWithSlash}?`]: '/',
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user