From 24450b8a40e0d68805ccc1aca86064f6c80befdb Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 2 Aug 2023 16:47:29 +0200 Subject: [PATCH] proxy-backend: update tests to use proxy.endpoints Signed-off-by: Patrik Oldsberg --- plugins/proxy-backend/package.json | 1 + .../src/service/router.config.test.ts | 75 +++++++++++-------- .../proxy-backend/src/service/router.test.ts | 26 ++++--- yarn.lock | 1 + 4 files changed, 61 insertions(+), 42 deletions(-) diff --git a/plugins/proxy-backend/package.json b/plugins/proxy-backend/package.json index 56439cd0f3..d7eb3ad435 100644 --- a/plugins/proxy-backend/package.json +++ b/plugins/proxy-backend/package.json @@ -49,6 +49,7 @@ }, "devDependencies": { "@backstage/cli": "workspace:^", + "@backstage/config-loader": "workspace:^", "@types/http-proxy-middleware": "^0.19.3", "@types/supertest": "^2.0.8", "@types/uuid": "^8.0.0", diff --git a/plugins/proxy-backend/src/service/router.config.test.ts b/plugins/proxy-backend/src/service/router.config.test.ts index 5f90f847e4..fffdcb5142 100644 --- a/plugins/proxy-backend/src/service/router.config.test.ts +++ b/plugins/proxy-backend/src/service/router.config.test.ts @@ -15,7 +15,11 @@ */ import { getVoidLogger, SingleHostDiscovery } from '@backstage/backend-common'; -import { ConfigReader } from '@backstage/config'; +import { + ConfigSources, + MutableConfigSource, + StaticConfigSource, +} from '@backstage/config-loader'; import express from 'express'; import { rest } from 'msw'; import { setupServer } from 'msw/node'; @@ -56,34 +60,36 @@ describe('createRouter reloadable configuration', () => { const logger = getVoidLogger(); // Grab the subscriber function and use mutable config data to mock a config file change - let subscriber: () => void; - const mutableConfigData: any = { - backend: { - baseUrl: 'http://localhost:7007', - listen: { - port: 7007, - }, - }, - proxy: { - '/test': { - target: 'https://non-existing-example.com', - pathRewrite: { - '.*': '/', + const mutableConfigSource = MutableConfigSource.create({ data: {} }); + const config = await ConfigSources.toConfig( + ConfigSources.merge([ + StaticConfigSource.create({ + data: { + backend: { + baseUrl: 'http://localhost:7007', + listen: { + port: 7007, + }, + }, + proxy: { + endpoints: { + '/test': { + target: 'https://non-existing-example.com', + pathRewrite: { + '.*': '/', + }, + }, + }, + }, }, - }, - }, - }; + }), + mutableConfigSource, + ]), + ); - const mockConfig = Object.assign(new ConfigReader(mutableConfigData), { - subscribe: (s: () => void) => { - subscriber = s; - return { unsubscribe: () => {} }; - }, - }); - - const discovery = SingleHostDiscovery.fromConfig(mockConfig); + const discovery = SingleHostDiscovery.fromConfig(config); const router = await createRouter({ - config: mockConfig, + config, logger, discovery, }); @@ -100,13 +106,18 @@ describe('createRouter reloadable configuration', () => { expect(response1.status).toEqual(200); - mutableConfigData.proxy['/test2'] = { - target: 'https://non-existing-example.com', - pathRewrite: { - '.*': '/', + mutableConfigSource.setData({ + proxy: { + endpoints: { + '/test2': { + target: 'https://non-existing-example.com', + pathRewrite: { + '.*': '/', + }, + }, + }, }, - }; - subscriber!(); + }); const response2 = await agent.get('/test2'); diff --git a/plugins/proxy-backend/src/service/router.test.ts b/plugins/proxy-backend/src/service/router.test.ts index 9e8c909d25..f69b0d38b7 100644 --- a/plugins/proxy-backend/src/service/router.test.ts +++ b/plugins/proxy-backend/src/service/router.test.ts @@ -45,10 +45,12 @@ describe('createRouter', () => { }, }, proxy: { - '/test': { - target: 'https://example.com', - headers: { - Authorization: 'Bearer supersecret', + endpoints: { + '/test': { + target: 'https://example.com', + headers: { + Authorization: 'Bearer supersecret', + }, }, }, }, @@ -112,9 +114,11 @@ describe('createRouter', () => { }, // no target would cause the buildMiddleware to fail proxy: { - '/test': { - headers: { - Authorization: 'Bearer supersecret', + endpoints: { + '/test': { + headers: { + Authorization: 'Bearer supersecret', + }, }, }, }, @@ -141,9 +145,11 @@ describe('createRouter', () => { }, // no target would cause the buildMiddleware to fail proxy: { - '/test': { - headers: { - Authorization: 'Bearer supersecret', + endpoints: { + '/test': { + headers: { + Authorization: 'Bearer supersecret', + }, }, }, }, diff --git a/yarn.lock b/yarn.lock index 9e829b5a58..33681796da 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8055,6 +8055,7 @@ __metadata: "@backstage/backend-plugin-api": "workspace:^" "@backstage/cli": "workspace:^" "@backstage/config": "workspace:^" + "@backstage/config-loader": "workspace:^" "@types/express": ^4.17.6 "@types/http-proxy-middleware": ^0.19.3 "@types/supertest": ^2.0.8