-
Notifications
You must be signed in to change notification settings - Fork 69
/
payload_test.go
93 lines (83 loc) · 3.79 KB
/
payload_test.go
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
package bugsnag
import (
"context"
"fmt"
"runtime"
"strings"
"testing"
"github.com/bugsnag/bugsnag-go/errors"
"github.com/bugsnag/bugsnag-go/sessions"
)
const expSmall = `{"apiKey":"","events":[{"app":{"releaseStage":""},"device":{"osName":"%s","runtimeVersions":{"go":"%s"}},"exceptions":[{"errorClass":"","message":"","stacktrace":null}],"metaData":{},"payloadVersion":"4","severity":"","unhandled":false}],"notifier":{"name":"Bugsnag Go","url":"https://github.com/bugsnag/bugsnag-go","version":"1.9.1"}}`
// The large payload has a timestamp in it which makes it awkward to assert against.
// Instead, assert that the timestamp property exist, along with the rest of the expected payload
const expLargePre = `{"apiKey":"166f5ad3590596f9aa8d601ea89af845","events":[{"app":{"releaseStage":"mega-production","type":"gin","version":"1.5.3"},"context":"/api/v2/albums","device":{"hostname":"super.duper.site","osName":"%s","runtimeVersions":{"go":"%s"}},"exceptions":[{"errorClass":"error class","message":"error message goes here","stacktrace":[{"method":"doA","file":"a.go","lineNumber":65},{"method":"fetchB","file":"b.go","lineNumber":99,"inProject":true},{"method":"incrementI","file":"i.go","lineNumber":651}]}],"groupingHash":"custom grouping hash","metaData":{"custom tab":{"my key":"my value"}},"payloadVersion":"4","session":{"startedAt":"`
const expLargePost = `,"severity":"info","severityReason":{"type":"unhandledError","attributes":{"framework":"gin"}},"unhandled":true,"user":{"id":"1234baerg134","name":"Kool Kidz on da bus","email":"[email protected]"}}],"notifier":{"name":"Bugsnag Go","url":"https://github.com/bugsnag/bugsnag-go","version":"1.9.1"}}`
func TestMarshalEmptyPayload(t *testing.T) {
sessionTracker = sessions.NewSessionTracker(&sessionTrackingConfig)
p := payload{&Event{Ctx: context.Background()}, &Configuration{}}
bytes, _ := p.MarshalJSON()
exp := fmt.Sprintf(expSmall, runtime.GOOS, runtime.Version())
if got := string(bytes[:]); got != exp {
t.Errorf("Payload different to what was expected. \nGot: %s\nExp: %s", got, exp)
}
}
func TestMarshalLargePayload(t *testing.T) {
payload := makeLargePayload()
bytes, _ := payload.MarshalJSON()
got := string(bytes[:])
expPre := fmt.Sprintf(expLargePre, runtime.GOOS, runtime.Version())
if !strings.Contains(got, expPre) {
t.Errorf("Expected large payload to contain\n'%s'\n but was\n'%s'", expPre, got)
}
if !strings.Contains(got, expLargePost) {
t.Errorf("Expected large payload to contain\n'%s'\n but was\n'%s'", expLargePost, got)
}
}
func makeLargePayload() *payload {
stackframes := []StackFrame{
{Method: "doA", File: "a.go", LineNumber: 65, InProject: false},
{Method: "fetchB", File: "b.go", LineNumber: 99, InProject: true},
{Method: "incrementI", File: "i.go", LineNumber: 651, InProject: false},
}
user := User{
Id: "1234baerg134",
Name: "Kool Kidz on da bus",
Email: "[email protected]",
}
handledState := HandledState{
SeverityReason: SeverityReasonUnhandledError,
OriginalSeverity: severity{String: "error"},
Unhandled: true,
Framework: "gin",
}
ctx := context.Background()
ctx = StartSession(ctx)
event := Event{
Error: &errors.Error{},
RawData: nil,
ErrorClass: "error class",
Message: "error message goes here",
Stacktrace: stackframes,
Context: "/api/v2/albums",
Severity: SeverityInfo,
GroupingHash: "custom grouping hash",
User: &user,
Ctx: ctx,
MetaData: map[string]map[string]interface{}{
"custom tab": map[string]interface{}{
"my key": "my value",
},
},
Unhandled: true,
handledState: handledState,
}
config := Configuration{
APIKey: testAPIKey,
ReleaseStage: "mega-production",
AppType: "gin",
AppVersion: "1.5.3",
Hostname: "super.duper.site",
}
return &payload{&event, &config}
}