|
1 | 1 | import { EventEnvelope } from '@sentry/types';
|
2 | 2 |
|
3 |
| -import { addItemToEnvelope, createEnvelope, forEachEnvelopeItem, serializeStringEnvelope } from '../src/envelope'; |
| 3 | +import { |
| 4 | + addItemToEnvelope, |
| 5 | + createEnvelope, |
| 6 | + forEachEnvelopeItem, |
| 7 | + serializeStringEnvelope, |
| 8 | + serializeEnvelope, |
| 9 | +} from '../src/envelope'; |
4 | 10 | import { parseEnvelope } from './testutils';
|
5 | 11 |
|
6 | 12 | describe('envelope', () => {
|
@@ -29,43 +35,56 @@ describe('envelope', () => {
|
29 | 35 | describe('addItemToEnvelope()', () => {
|
30 | 36 | it('adds an item to an envelope', () => {
|
31 | 37 | const env = createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }, []);
|
32 |
| - const parsedEnvelope = parseEnvelope(serializeStringEnvelope(env)); |
33 |
| - expect(parsedEnvelope).toHaveLength(1); |
34 |
| - expect(parsedEnvelope[0]).toEqual({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }); |
| 38 | + let [envHeaders, items] = parseEnvelope(serializeEnvelope(env)); |
| 39 | + expect(items).toHaveLength(0); |
| 40 | + expect(envHeaders).toEqual({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }); |
35 | 41 |
|
36 | 42 | const newEnv = addItemToEnvelope<EventEnvelope>(env, [
|
37 | 43 | { type: 'event' },
|
38 | 44 | { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' },
|
39 | 45 | ]);
|
40 |
| - const parsedNewEnvelope = parseEnvelope(serializeStringEnvelope(newEnv)); |
41 |
| - expect(parsedNewEnvelope).toHaveLength(3); |
42 |
| - expect(parsedNewEnvelope[0]).toEqual({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }); |
43 |
| - expect(parsedNewEnvelope[1]).toEqual({ type: 'event' }); |
44 |
| - expect(parsedNewEnvelope[2]).toEqual({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }); |
| 46 | + |
| 47 | + [envHeaders, items] = parseEnvelope(serializeEnvelope(newEnv)); |
| 48 | + expect(envHeaders).toEqual({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }); |
| 49 | + expect(items).toHaveLength(1); |
| 50 | + expect(items[0]).toEqual([{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }]); |
45 | 51 | });
|
46 | 52 | });
|
47 | 53 |
|
48 | 54 | describe('forEachEnvelopeItem', () => {
|
49 | 55 | it('loops through an envelope', () => {
|
50 | 56 | const items: EventEnvelope[1] = [
|
51 | 57 | [{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }],
|
52 |
| - [{ type: 'attachment', filename: 'bar.txt', length: 6 }, new Uint8Array(6)], |
53 |
| - [{ type: 'attachment', filename: 'foo.txt', length: 6 }, new Uint8Array(6)], |
| 58 | + [{ type: 'attachment', filename: 'bar.txt', length: 6 }, Uint8Array.from([1, 2, 3, 4, 5, 6])], |
| 59 | + [{ type: 'attachment', filename: 'foo.txt', length: 6 }, Uint8Array.from([7, 8, 9, 10, 11, 12])], |
54 | 60 | ];
|
55 | 61 |
|
56 | 62 | const env = createEnvelope<EventEnvelope>(
|
57 | 63 | { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' },
|
58 | 64 | items,
|
59 | 65 | );
|
60 | 66 |
|
61 |
| - expect.assertions(6); |
| 67 | + expect.assertions(11); |
62 | 68 |
|
63 | 69 | let iteration = 0;
|
64 | 70 | forEachEnvelopeItem(env, (item, type) => {
|
65 | 71 | expect(item).toBe(items[iteration]);
|
66 | 72 | expect(type).toBe(items[iteration][0].type);
|
67 | 73 | iteration = iteration + 1;
|
68 | 74 | });
|
| 75 | + |
| 76 | + const [parsedHeaders, parsedItems] = parseEnvelope(serializeEnvelope(env)); |
| 77 | + expect(parsedHeaders).toEqual({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }); |
| 78 | + expect(parsedItems).toHaveLength(3); |
| 79 | + expect(items[0]).toEqual([{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }]); |
| 80 | + expect(items[1]).toEqual([ |
| 81 | + { type: 'attachment', filename: 'bar.txt', length: 6 }, |
| 82 | + Uint8Array.from([1, 2, 3, 4, 5, 6]), |
| 83 | + ]); |
| 84 | + expect(items[2]).toEqual([ |
| 85 | + { type: 'attachment', filename: 'foo.txt', length: 6 }, |
| 86 | + Uint8Array.from([7, 8, 9, 10, 11, 12]), |
| 87 | + ]); |
69 | 88 | });
|
70 | 89 | });
|
71 | 90 | });
|
0 commit comments