Merge pull request #5933 from backstage/timbonicus/proxy-base-url

Fix proxy redirect for base path without slash
This commit is contained in:
Fredrik Adelöw
2021-06-05 07:24:22 +02:00
committed by GitHub
3 changed files with 14 additions and 5 deletions
+5
View File
@@ -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);
});
+5 -1
View File
@@ -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}?`]: '/',
};
}