-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_formatter_test.go
53 lines (41 loc) · 1.18 KB
/
json_formatter_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
package hook
import (
"encoding/json"
"strings"
"testing"
"github.com/sirupsen/logrus"
)
func TestJSONFormatter_FormatQuoted(t *testing.T) {
formatter := DefaultJSONFormatter
b, err := formatter.Format(logrus.WithField("key", "wild walrus"))
if err != nil {
t.Fatalf("unable to format entry: %s", err)
}
bstr := string(b)
if strings.Index(bstr, "```json\n") != 0 || strings.Index(bstr, "\n```") != len(bstr)-4 {
t.Fatal("json tag not set")
}
bstr = strings.TrimLeft(bstr, "```json")
bstr = strings.TrimRight(bstr, "```")
b = []byte(bstr)
entry := make(map[string]interface{})
if err = json.Unmarshal(b, &entry); err != nil {
t.Fatalf("unable to unmarshal formatted entry: %s", err)
}
if entry["key"] != "wild walrus" {
t.Fatal("key field not set")
}
}
func TestJSONFormatter_FormatLongMessage(t *testing.T) {
formatter := &JSONFormatter{}
entry := &logrus.Entry{}
entry = entry.WithField("key", "value")
entry.Message = string(make([]byte, DiscordMaxMessageLen))
_, err := formatter.Format(entry)
if err == nil {
t.Fatal("expect error got nil")
}
if err != ErrMessageTooLong {
t.Fatalf("unexpected error: %s got error: %s", err, ErrMessageTooLong)
}
}