backend-common: make HostDiscovery strip trailing slashes in backend.baseUrl

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-06-15 13:49:35 +02:00
parent 77d5920485
commit eeb3f801fd
3 changed files with 26 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-common': patch
---
`HostDiscovery` now strips trailing slashes in the `backend.baseUrl` config.
@@ -36,6 +36,24 @@ describe('HostDiscovery', () => {
);
});
it('strips trailing slashes in config', async () => {
const discovery = HostDiscovery.fromConfig(
new ConfigReader({
backend: {
baseUrl: 'http://localhost:40//',
listen: { port: 80, host: 'localhost' },
},
}),
);
await expect(discovery.getBaseUrl('catalog')).resolves.toBe(
'http://localhost:80/api/catalog',
);
await expect(discovery.getExternalBaseUrl('catalog')).resolves.toBe(
'http://localhost:40/api/catalog',
);
});
it('can configure the base path', async () => {
const discovery = HostDiscovery.fromConfig(
new ConfigReader({
@@ -57,7 +57,9 @@ export class HostDiscovery implements PluginEndpointDiscovery {
*/
static fromConfig(config: Config, options?: { basePath?: string }) {
const basePath = options?.basePath ?? '/api';
const externalBaseUrl = config.getString('backend.baseUrl');
const externalBaseUrl = config
.getString('backend.baseUrl')
.replace(/\/+$/, '');
const {
listen: { host: listenHost = '::', port: listenPort },