/
envelope.test.ts
120 lines (100 loc) · 4.96 KB
/
envelope.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { EventEnvelope } from '@sentry/types';
import { TextEncoder } from 'util';
import { addItemToEnvelope, createEnvelope, forEachEnvelopeItem, serializeEnvelope } from '../src/envelope';
import { parseEnvelope } from './testutils';
describe('envelope', () => {
describe('createEnvelope()', () => {
const testTable: Array<[string, Parameters<typeof createEnvelope>[0], Parameters<typeof createEnvelope>[1]]> = [
['creates an empty envelope', {}, []],
['creates an envelope with a header but no items', { dsn: 'https://public@example.com/1', sdk: {} }, []],
];
it.each(testTable)('%s', (_: string, headers, items) => {
const env = createEnvelope(headers, items);
expect(env).toHaveLength(2);
expect(env[0]).toStrictEqual(headers);
expect(env[1]).toStrictEqual(items);
});
});
describe('serializeEnvelope()', () => {
it('serializes an envelope', () => {
const env = createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }, []);
const serializedEnvelope = serializeEnvelope(env, new TextEncoder());
expect(typeof serializedEnvelope).toBe('string');
const [headers] = parseEnvelope(serializedEnvelope);
expect(headers).toEqual({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' });
});
it('serializes an envelope with attachments', () => {
const items: EventEnvelope[1] = [
[{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }],
[{ type: 'attachment', filename: 'bar.txt', length: 6 }, Uint8Array.from([1, 2, 3, 4, 5, 6])],
[{ type: 'attachment', filename: 'foo.txt', length: 6 }, Uint8Array.from([7, 8, 9, 10, 11, 12])],
];
const env = createEnvelope<EventEnvelope>(
{ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' },
items,
);
expect.assertions(6);
const serializedEnvelope = serializeEnvelope(env, new TextEncoder());
expect(serializedEnvelope).toBeInstanceOf(Uint8Array);
const [parsedHeaders, parsedItems] = parseEnvelope(serializedEnvelope);
expect(parsedHeaders).toEqual({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' });
expect(parsedItems).toHaveLength(3);
expect(items[0]).toEqual([{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }]);
expect(items[1]).toEqual([
{ type: 'attachment', filename: 'bar.txt', length: 6 },
Uint8Array.from([1, 2, 3, 4, 5, 6]),
]);
expect(items[2]).toEqual([
{ type: 'attachment', filename: 'foo.txt', length: 6 },
Uint8Array.from([7, 8, 9, 10, 11, 12]),
]);
});
it("doesn't throw when being passed a an envelope that contains a circular item payload", () => {
const chicken: { egg?: unknown } = {};
const egg = { chicken };
chicken.egg = chicken;
const env = createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }, [
[{ type: 'event' }, egg],
]);
const serializedEnvelope = serializeEnvelope(env, new TextEncoder());
const [, , serializedBody] = serializedEnvelope.toString().split('\n');
expect(serializedBody).toBe('{"chicken":{"egg":"[Circular ~]"}}');
});
});
describe('addItemToEnvelope()', () => {
it('adds an item to an envelope', () => {
const env = createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }, []);
let [envHeaders, items] = parseEnvelope(serializeEnvelope(env, new TextEncoder()));
expect(items).toHaveLength(0);
expect(envHeaders).toEqual({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' });
const newEnv = addItemToEnvelope<EventEnvelope>(env, [
{ type: 'event' },
{ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' },
]);
[envHeaders, items] = parseEnvelope(serializeEnvelope(newEnv, new TextEncoder()));
expect(envHeaders).toEqual({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' });
expect(items).toHaveLength(1);
expect(items[0]).toEqual([{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }]);
});
});
describe('forEachEnvelopeItem', () => {
it('loops through an envelope', () => {
const items: EventEnvelope[1] = [
[{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }],
[{ type: 'attachment', filename: 'bar.txt', length: 6 }, Uint8Array.from([1, 2, 3, 4, 5, 6])],
[{ type: 'attachment', filename: 'foo.txt', length: 6 }, Uint8Array.from([7, 8, 9, 10, 11, 12])],
];
const env = createEnvelope<EventEnvelope>(
{ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' },
items,
);
expect.assertions(6);
let iteration = 0;
forEachEnvelopeItem(env, (item, type) => {
expect(item).toBe(items[iteration]);
expect(type).toBe(items[iteration][0].type);
iteration = iteration + 1;
});
});
});
});