Fix proxy redirect for base path without slash

Signed-off-by: Tim Hansen <timbonicus@gmail.com>
This commit is contained in:
Tim Hansen
2021-06-04 21:01:15 -06:00
parent 997be24a6e
commit 875809a59b
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}?`]: '/',
};
}