Skip to content

Commit

Permalink
test(core): added test for event body methods
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Feb 28, 2022
1 parent cabe88e commit bb5ea6e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/v2/core/event-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function getEventBodyAsBuffer(
body: string,
isBase64Encoded: boolean,
): [body: Buffer, contentLength: number] {
const encoding = isBase64Encoded ? 'base64' : 'utf8';
const encoding: BufferEncoding = isBase64Encoded ? 'base64' : 'utf8';

const buffer = Buffer.from(body, encoding);
const contentLength = Buffer.byteLength(buffer, encoding);
Expand Down
25 changes: 25 additions & 0 deletions test/core/event-body.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { getEventBodyAsBuffer } from '../../src/v2/core';

describe('getEventBodyAsBuffer', () => {
it('should return correctly the body in utf-8 as buffer', () => {
const body = '{}';

const [bodyAsBuffer, contentLength] = getEventBodyAsBuffer(body, false);

expect(bodyAsBuffer).toBeInstanceOf(Buffer);
expect(contentLength).toBe(2);

expect(bodyAsBuffer.toString('utf8')).toBe(body);
});

it('should return correctly the body in base64 as buffer', () => {
const body = Buffer.from('{}', 'utf8').toString('base64');

const [bodyAsBuffer, contentLength] = getEventBodyAsBuffer(body, true);

expect(bodyAsBuffer).toBeInstanceOf(Buffer);
expect(contentLength).toBe(2);

expect(bodyAsBuffer.toString('base64')).toBe(body);
});
});

0 comments on commit bb5ea6e

Please sign in to comment.