forked from SierraSoftworks/sentry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacket_test.go
More file actions
163 lines (129 loc) · 3.86 KB
/
packet_test.go
File metadata and controls
163 lines (129 loc) · 3.86 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package sentry
import (
"encoding/json"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func ExamplePacket() {
// Create a new packet object which can be sent to
// Sentry by one of the transports or send queues.
p := NewPacket().SetOptions(
DSN(""),
Message("Custom packet creation"),
)
// Create a clone of this packet if you want to use
// it as a template
p.Clone().SetOptions(
Message("Overridden message which doesn't affect the original"),
)
}
func TestPacket(t *testing.T) {
Convey("Packet", t, func() {
Convey("NewPacket()", func() {
p := NewPacket()
So(p, ShouldNotBeNil)
So(p, ShouldImplement, (*Packet)(nil))
})
Convey("Clone()", func() {
p := NewPacket()
p2 := p.Clone()
So(p, ShouldNotEqual, p2)
So(p, ShouldResemble, p2)
})
Convey("SetOptions()", func() {
p := NewPacket()
So(p.SetOptions(), ShouldResemble, p)
pp, ok := p.(*packet)
So(ok, ShouldBeTrue)
So(pp, ShouldNotBeNil)
pi := *pp
Convey("Should ignore nil options", func() {
Convey("When only nil options are provided", func() {
p2 := p.Clone()
So(p.SetOptions(nil), ShouldResemble, p2)
})
Convey("When both nil and non-nil options are provided", func() {
p2 := p.Clone()
opt := &testOption{}
So(p.SetOptions(nil, opt), ShouldResemble, p2.SetOptions(opt))
})
})
Convey("Should set normal option fields", func() {
opt := &testOption{}
p.SetOptions(opt)
So(pi, ShouldContainKey, "test")
So(pi["test"], ShouldEqual, opt)
})
Convey("Should obey the Omit() function", func() {
Convey("If it returns false", func() {
opt := &testOmitableOption{
omit: false,
}
p.SetOptions(opt)
So(pi, ShouldContainKey, "test")
So(pi["test"], ShouldEqual, opt)
})
Convey("If it returns true", func() {
opt := &testOmitableOption{
omit: true,
}
p.SetOptions(opt)
So(pi, ShouldNotContainKey, "test")
})
})
Convey("Should use the Finalize() function", func() {
opt := &testFinalizableOption{}
So(opt.finalized, ShouldBeFalse)
p.SetOptions(opt)
So(opt.finalized, ShouldBeTrue)
})
Convey("Should handle existing keys", func() {
Convey("Should replace by default", func() {
opt1 := &testOption{}
opt2 := &testOption{}
p.SetOptions(opt1)
So(pi, ShouldContainKey, "test")
So(pi["test"], ShouldEqual, opt1)
p.SetOptions(opt2)
So(pi, ShouldContainKey, "test")
So(pi["test"], ShouldEqual, opt2)
})
Convey("Should merge when Merge() is present", func() {
opt1 := &testMergeableOption{data: 1}
opt2 := &testMergeableOption{data: 2}
p.SetOptions(opt1)
So(pi, ShouldContainKey, "test")
So(pi["test"], ShouldEqual, opt1)
p.SetOptions(opt2)
So(pi, ShouldContainKey, "test")
So(opt1.data, ShouldEqual, 1)
So(opt2.data, ShouldEqual, 2)
So(pi["test"], ShouldResemble, &testMergeableOption{data: 3})
})
})
})
Convey("MarshalJSON", func() {
p := NewPacket()
Convey("With basic options", func() {
opt := &testOption{}
p.SetOptions(opt)
b, err := json.Marshal(p)
So(err, ShouldBeNil)
var data map[string]interface{}
So(json.Unmarshal(b, &data), ShouldBeNil)
So(data, ShouldContainKey, "test")
So(data["test"], ShouldResemble, map[string]interface{}{})
})
Convey("With custom MarshalJSON implementations", func() {
opt := &testSerializableOption{data: "testing"}
p.SetOptions(opt)
b, err := json.Marshal(p)
So(err, ShouldBeNil)
var data map[string]interface{}
So(json.Unmarshal(b, &data), ShouldBeNil)
So(data, ShouldContainKey, "test")
So(data["test"], ShouldResemble, "testing")
})
})
})
}