diff --git a/packages/core/src/baseclient.ts b/packages/core/src/baseclient.ts index 260eb412561b..1c8de1472a49 100644 --- a/packages/core/src/baseclient.ts +++ b/packages/core/src/baseclient.ts @@ -287,10 +287,13 @@ export abstract class BaseClient implements Client { */ 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); diff --git a/packages/core/test/lib/attachments.test.ts b/packages/core/test/lib/attachments.test.ts new file mode 100644 index 000000000000..610518225793 --- /dev/null +++ b/packages/core/test/lib/attachments.test.ts @@ -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) }] }); + }); +});