From 9723a4a96a3a6465a8125cb70feee343ea8ac1e5 Mon Sep 17 00:00:00 2001 From: Karan Shah Date: Fri, 4 Feb 2022 10:27:01 +0000 Subject: [PATCH] Write a test for pathRewrite Signed-off-by: Karan Shah --- .../src/service/router.test.ts | 35 ++++++++++++++----- .../airbrake-backend/src/service/router.ts | 29 +++++++++------ 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/plugins/airbrake-backend/src/service/router.test.ts b/plugins/airbrake-backend/src/service/router.test.ts index ec21a340a3..f36d36d6f1 100644 --- a/plugins/airbrake-backend/src/service/router.test.ts +++ b/plugins/airbrake-backend/src/service/router.test.ts @@ -18,13 +18,20 @@ import { getVoidLogger } from '@backstage/backend-common'; import express from 'express'; import request from 'supertest'; import { ConfigReader } from '@backstage/config'; -import { createRouter } from './router'; -import { extractAirbrakeConfig } from '../config'; +import { + createRouter, + generateAirbrakePathRewrite, + RouterOptions, +} from './router'; +import { AirbrakeConfig, extractAirbrakeConfig } from '../config'; describe('createRouter', () => { let app: express.Express; + let airbrakeConfig: AirbrakeConfig; + + beforeEach(async () => { + jest.resetAllMocks(); - beforeAll(async () => { const config = new ConfigReader({ integrations: { airbrake: { @@ -32,7 +39,7 @@ describe('createRouter', () => { }, }, }); - const airbrakeConfig = extractAirbrakeConfig(config); + airbrakeConfig = extractAirbrakeConfig(config); const router = await createRouter({ logger: getVoidLogger(), @@ -41,10 +48,6 @@ describe('createRouter', () => { app = express().use(router); }); - beforeEach(() => { - jest.resetAllMocks(); - }); - describe('GET /health', () => { it('returns ok', async () => { const response = await request(app).get('/health'); @@ -53,4 +56,20 @@ describe('createRouter', () => { expect(response.body).toEqual({ status: 'ok' }); }); }); + + describe('GET /api', () => { + it('appends the API Key properly', () => { + const options: RouterOptions = { + logger: getVoidLogger(), + airbrakeConfig, + }; + const pathRewrite = generateAirbrakePathRewrite(options) as ( + path: string, + ) => string; + + expect(pathRewrite('/airbrake-backend/api/random/endpoint')).toBe( + '/random/endpoint?key=fakeApiKey', + ); + }); + }); }); diff --git a/plugins/airbrake-backend/src/service/router.ts b/plugins/airbrake-backend/src/service/router.ts index 77b115f088..670a98eb78 100644 --- a/plugins/airbrake-backend/src/service/router.ts +++ b/plugins/airbrake-backend/src/service/router.ts @@ -20,12 +20,29 @@ import Router from 'express-promise-router'; import { Logger } from 'winston'; import { createProxyMiddleware } from 'http-proxy-middleware'; import { AirbrakeConfig } from '../config'; +import { Options } from 'http-proxy-middleware/dist/types'; export interface RouterOptions { logger: Logger; airbrakeConfig: AirbrakeConfig; } +export const generateAirbrakePathRewrite = ( + options: RouterOptions, +): Options['pathRewrite'] => { + const apiKey = options.airbrakeConfig.apiKey; + + return path => { + let newPath = path.replace(/.+?(\/api)/, ''); + if (newPath.includes('?')) { + newPath += `&key=${apiKey}`; + } else { + newPath += `?key=${apiKey}`; + } + return newPath; + }; +}; + export async function createRouter( options: RouterOptions, ): Promise { @@ -44,17 +61,7 @@ export async function createRouter( createProxyMiddleware({ target: 'https://api.airbrake.io/api', changeOrigin: true, - pathRewrite: path => { - const apiKey = options.airbrakeConfig.apiKey; - - let newPath = path.replace(/.+?(\/api)/, ''); - if (newPath.includes('?')) { - newPath += `&key=${apiKey}`; - } else { - newPath += `?key=${apiKey}`; - } - return newPath; - }, + pathRewrite: generateAirbrakePathRewrite(options), }), );