-
Notifications
You must be signed in to change notification settings - Fork 215
/
message_test.go
69 lines (63 loc) · 1.51 KB
/
message_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
/*
Copyright 2023 The CloudEvents Authors
SPDX-License-Identifier: Apache-2.0
*/
package mqtt_paho
import (
"context"
"testing"
"github.com/cloudevents/sdk-go/v2/binding"
"github.com/cloudevents/sdk-go/v2/event"
"github.com/eclipse/paho.golang/paho"
)
func TestReadStructured(t *testing.T) {
tests := []struct {
name string
msg *paho.Publish
wantErr error
}{
{
name: "nil format",
msg: &paho.Publish{
Payload: []byte(""),
},
wantErr: binding.ErrNotStructured,
},
{
name: "json format",
msg: &paho.Publish{
Payload: []byte(""),
Properties: &paho.PublishProperties{
User: []paho.UserProperty{{Key: contentType, Value: event.ApplicationCloudEventsJSON}},
},
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
msg := NewMessage(tc.msg)
err := msg.ReadStructured(context.Background(), (*pubMessageWriter)(tc.msg))
if err != tc.wantErr {
t.Errorf("Error unexpected. got: %v, want: %v", err, tc.wantErr)
}
})
}
}
func TestReadBinary(t *testing.T) {
msg := &paho.Publish{
Payload: []byte("{hello:world}"),
Properties: &paho.PublishProperties{
User: []paho.UserProperty{
{Key: "ce-specversion", Value: "1.0"},
{Key: "ce-type", Value: "binary.test"},
{Key: "ce-source", Value: "test-source"},
{Key: "ce-id", Value: "ABC-123"},
},
},
}
message := NewMessage(msg)
err := message.ReadBinary(context.Background(), (*pubMessageWriter)(msg))
if err != nil {
t.Errorf("Error unexpected. got: %v", err)
}
}