-
Notifications
You must be signed in to change notification settings - Fork 0
/
messageParser_test.go
58 lines (52 loc) · 1.27 KB
/
messageParser_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
package main
import (
"bytes"
"encoding/json"
"testing"
)
func TestReadMessages(t *testing.T) {
expectedMessages := generateTestMessages(2)
testData, _ := json.Marshal(expectedMessages)
messages, err := readMessages(bytes.NewReader(testData))
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if len(messages) != len(expectedMessages) {
t.Errorf("expected %d messages, got %d", len(expectedMessages), len(messages))
}
for i, msg := range messages {
if *expectedMessages[i].MessageID != *msg.MessageID {
t.Errorf("expected MessageId %v, got %v", expectedMessages[i].MessageID, msg.MessageID)
}
}
}
func TestInvalidMessages(t *testing.T) {
tests := []struct {
name string
messages string
errStr string
}{
{
name: "Test with empty message",
messages: "[]",
},
{
name: "Test with invalid JSON",
messages: `[{"body": "invalid json", "contentType": "application/json"}]`,
},
{
name: "Test with missing body",
messages: `[{"contentType": "application/json"}]`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testData, _ := json.Marshal(tt.messages)
_, err := readMessages(bytes.NewReader(testData))
if err == nil {
t.Errorf("expected error, got nil")
return
}
})
}
}