1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-23 00:22:19 +01:00

Authorization header webhook (#2356)

Added unit test and made code prettier in 
webhook.ts
webhook.test.ts
This commit is contained in:
Sebastian Bury 2022-11-09 11:45:30 +01:00 committed by GitHub
parent 29524ab423
commit f316e99dca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 2 deletions

View File

@ -44,6 +44,16 @@ const webhookDefinition: IAddonDefinition = {
required: false, required: false,
sensitive: false, sensitive: false,
}, },
{
name: 'authorization',
displayName: 'Authorization',
placeholder: '',
description:
'(Optional) The Authorization header to use. Not used if left blank.',
type: 'text',
required: false,
sensitive: true,
},
{ {
name: 'bodyTemplate', name: 'bodyTemplate',
displayName: 'Body template', displayName: 'Body template',

View File

@ -84,3 +84,33 @@ test('Should format event with "bodyTemplate"', () => {
expect(call.options.headers['Content-Type']).toBe('text/plain'); expect(call.options.headers['Content-Type']).toBe('text/plain');
expect(call.options.body).toBe('feature-created on toggle some-toggle'); expect(call.options.body).toBe('feature-created on toggle some-toggle');
}); });
test('Should format event with "authorization"', () => {
const addon = new WebhookAddon({ getLogger: noLogger });
const event: IEvent = {
id: 1,
createdAt: new Date(),
type: FEATURE_CREATED,
createdBy: 'some@user.com',
featureName: 'some-toggle',
data: {
name: 'some-toggle',
enabled: false,
strategies: [{ name: 'default' }],
},
};
const parameters = {
url: 'http://test.webhook.com/plain',
bodyTemplate: '{{event.type}} on toggle {{event.data.name}}',
contentType: 'text/plain',
authorization: 'API KEY 123abc',
};
addon.handleEvent(event, parameters);
const call = fetchRetryCalls[0];
expect(fetchRetryCalls.length).toBe(1);
expect(call.url).toBe(parameters.url);
expect(call.options.headers.Authorization).toBe(parameters.authorization);
expect(call.options.body).toBe('feature-created on toggle some-toggle');
});

View File

@ -8,6 +8,7 @@ interface IParameters {
url: string; url: string;
bodyTemplate?: string; bodyTemplate?: string;
contentType?: string; contentType?: string;
authorization?: string;
} }
export default class Webhook extends Addon { export default class Webhook extends Addon {
@ -16,7 +17,7 @@ export default class Webhook extends Addon {
} }
async handleEvent(event: IEvent, parameters: IParameters): Promise<void> { async handleEvent(event: IEvent, parameters: IParameters): Promise<void> {
const { url, bodyTemplate, contentType } = parameters; const { url, bodyTemplate, contentType, authorization } = parameters;
const context = { const context = {
event, event,
}; };
@ -31,7 +32,10 @@ export default class Webhook extends Addon {
const requestOpts = { const requestOpts = {
method: 'POST', method: 'POST',
headers: { 'Content-Type': contentType || 'application/json' }, headers: {
'Content-Type': contentType || 'application/json',
Authorization: authorization || undefined,
},
body, body,
}; };
const res = await this.fetchRetry(url, requestOpts); const res = await this.fetchRetry(url, requestOpts);