Write a test for pathRewrite
Signed-off-by: Karan Shah <karan.shah@simplybusiness.co.uk>
This commit is contained in:
@@ -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',
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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<express.Router> {
|
||||
@@ -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),
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user