Skip to content

Commit

Permalink
ref(core): Actually ensure attachments are added to the envelope (#5159)
Browse files Browse the repository at this point in the history
This patch fixes a bug where attachments were previously not added to the envelope that would be sent to Sentry. Additionally, it adds a test that ensures that the attachments actually make it into the raw envelope.
  • Loading branch information
timfish authored and AbhiPrasad committed May 30, 2022
1 parent 36599a3 commit 747f1df
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,13 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
*/
public sendEvent(event: Event, hint: EventHint = {}): void {
if (this._dsn) {
const env = createEventEnvelope(event, this._dsn, this._options._metadata, this._options.tunnel);
let env = createEventEnvelope(event, this._dsn, this._options._metadata, this._options.tunnel);

for (const attachment of hint.attachments || []) {
addItemToEnvelope(env, createAttachmentEnvelopeItem(attachment, this._options.transportOptions?.textEncoder));
env = addItemToEnvelope(
env,
createAttachmentEnvelopeItem(attachment, this._options.transportOptions?.textEncoder),
);
}

this._sendEnvelope(env);
Expand Down
38 changes: 38 additions & 0 deletions packages/core/test/lib/attachments.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { parseEnvelope } from '@sentry/utils/test/testutils';
import { TextEncoder } from 'util';

import { createTransport } from '../../src/transports/base';
import { getDefaultTestClientOptions, TestClient } from '../mocks/client';

describe('Attachments', () => {
beforeEach(() => {
TestClient.sendEventCalled = undefined;
TestClient.instance = undefined;
});

afterEach(() => {
jest.clearAllMocks();
});

test('actually end up in envelope', async () => {
expect.assertions(4);

const options = getDefaultTestClientOptions({
dsn: 'https://username@domain/123',
enableSend: true,
transport: () =>
createTransport({ recordDroppedEvent: () => undefined, textEncoder: new TextEncoder() }, async req => {
const [, items] = parseEnvelope(req.body);
expect(items.length).toEqual(2);
// Second envelope item should be the attachment
expect(items[1][0]).toEqual({ type: 'attachment', length: 50000, filename: 'empty.bin' });
expect(items[1][1]).toBeInstanceOf(Uint8Array);
expect((items[1][1] as Uint8Array).length).toEqual(50_000);
return {};
}),
});

const client = new TestClient(options);
client.captureEvent({}, { attachments: [{ filename: 'empty.bin', data: new Uint8Array(50_000) }] });
});
});

0 comments on commit 747f1df

Please sign in to comment.