-
-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathenvelope_test.exs
148 lines (115 loc) · 4.98 KB
/
envelope_test.exs
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
defmodule Sentry.EnvelopeTest do
use Sentry.Case, async: false
import Sentry.TestHelpers
alias Sentry.{Attachment, CheckIn, ClientReport, Envelope, Event}
describe "to_binary/1" do
test "encodes an envelope" do
put_test_config(environment_name: "test")
event = Event.create_event([])
envelope = Envelope.from_event(event)
assert {:ok, encoded} = Envelope.to_binary(envelope)
assert [id_line, header_line, event_line] = String.split(encoded, "\n", trim: true)
assert decode!(id_line) == %{"event_id" => event.event_id}
assert %{"type" => "event", "length" => _} = decode!(header_line)
decoded_event = decode!(event_line)
assert decoded_event["event_id"] == event.event_id
assert decoded_event["breadcrumbs"] == []
assert decoded_event["environment"] == "test"
assert decoded_event["exception"] == []
assert decoded_event["extra"] == %{}
assert decoded_event["user"] == %{}
assert decoded_event["request"] == %{}
end
test "works without an event ID" do
envelope = Envelope.from_event(Event.create_event([]))
envelope = %Envelope{envelope | event_id: nil}
assert {:ok, encoded} = Envelope.to_binary(envelope)
assert [id_line, _header_line, _event_line] = String.split(encoded, "\n", trim: true)
assert id_line == "{{}}"
end
test "works with attachments" do
attachments = [
%Attachment{data: <<1, 2, 3>>, filename: "example.dat"},
%Attachment{data: "Hello!", filename: "example.txt", content_type: "text/plain"},
%Attachment{data: "{}", filename: "example.json", content_type: "application/json"},
%Attachment{data: "...", filename: "dump", attachment_type: "event.minidump"}
]
event = %Event{Event.create_event([]) | attachments: attachments}
assert {:ok, encoded} = event |> Envelope.from_event() |> Envelope.to_binary()
assert [
id_line,
_event_header,
_event_data,
attachment1_header,
<<1, 2, 3>>,
attachment2_header,
"Hello!",
attachment3_header,
"{}",
attachment4_header,
"..."
] = String.split(encoded, "\n", trim: true)
assert %{"event_id" => _} = decode!(id_line)
assert decode!(attachment1_header) == %{
"type" => "attachment",
"length" => 3,
"filename" => "example.dat"
}
assert decode!(attachment2_header) == %{
"type" => "attachment",
"length" => 6,
"filename" => "example.txt",
"content_type" => "text/plain"
}
assert decode!(attachment3_header) == %{
"type" => "attachment",
"length" => 2,
"filename" => "example.json",
"content_type" => "application/json"
}
assert decode!(attachment4_header) == %{
"type" => "attachment",
"length" => 3,
"filename" => "dump",
"attachment_type" => "event.minidump"
}
end
test "works with check-ins" do
put_test_config(environment_name: "test")
check_in_id = Sentry.UUID.uuid4_hex()
check_in = %CheckIn{check_in_id: check_in_id, monitor_slug: "test", status: :ok}
envelope = Envelope.from_check_in(check_in)
assert {:ok, encoded} = Envelope.to_binary(envelope)
assert [id_line, header_line, event_line] = String.split(encoded, "\n", trim: true)
assert %{"event_id" => _} = decode!(id_line)
assert %{"type" => "check_in", "length" => _} = decode!(header_line)
decoded_check_in = decode!(event_line)
assert decoded_check_in["check_in_id"] == check_in_id
assert decoded_check_in["monitor_slug"] == "test"
assert decoded_check_in["status"] == "ok"
end
end
test "works with client reports" do
put_test_config(environment_name: "test")
client_report = %ClientReport{
timestamp: "2024-10-12T13:21:13",
discarded_events: [%{reason: :event_processor, category: "error", quantity: 1}]
}
envelope = Envelope.from_client_report(client_report)
assert {:ok, encoded} = Envelope.to_binary(envelope)
assert [id_line, header_line, event_line] = String.split(encoded, "\n", trim: true)
assert %{"event_id" => _} = decode!(id_line)
assert %{"type" => "client_report", "length" => _} = decode!(header_line)
decoded_client_report = decode!(event_line)
assert decoded_client_report["timestamp"] == client_report.timestamp
assert decoded_client_report["discarded_events"] == [
%{"category" => "error", "reason" => "event_processor", "quantity" => 1}
]
end
test "returns correct data_category" do
assert Envelope.get_data_category(%Sentry.Event{
event_id: Sentry.UUID.uuid4_hex(),
timestamp: "2024-10-12T13:21:13"
}) == "error"
end
end