Skip to content

ref(core): Actually ensure attachments are added to the envelope #5159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) }] });
});
});