-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_test.go
142 lines (122 loc) · 4.59 KB
/
json_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
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
package taskhawk
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDeserialize(t *testing.T) {
hub := NewHub(Config{}, nil)
taskRef := SendEmailTask{}
_, err := RegisterTask(hub, "task_test.SendEmailTask", taskRef.Run)
require.NoError(t, err)
jsonStr := `{"headers":{"request_id":"request-id"},"id":"message-id","kwargs":{"To":"mail@example.com",` +
`"From":"mail@spammer.com"},"metadata":{"timestamp":1521493587123,"version":"1.0"},` +
`"task":"task_test.SendEmailTask"}`
attributes := map[string]string{
headerTask: "task_test.SendEmailTask",
}
m, err := jsonifier{}.deserialize(hub, []byte(jsonStr), attributes)
assert.NoError(t, err)
input := &SendEmailTaskInput{
To: "mail@example.com",
From: "mail@spammer.com",
}
expected := getValidMessage(t, hub, input)
assert.Equal(t, expected, m)
}
func TestDeserialize_ErrorNoTaskAttribute(t *testing.T) {
hub := NewHub(Config{}, nil)
taskRef := SendEmailTask{}
_, err := RegisterTask(hub, "task_test.SendEmailTask", taskRef.Run)
require.NoError(t, err)
jsonStr := `{"headers":{"request_id":"request-id"},"id":"message-id","kwargs":{"To":"mail@example.com",` +
`"From":"mail@spammer.com"},"metadata":{"timestamp":1521493587123,"version":"1.0"},` +
`"task":"task_test.SendEmailTask"}`
attributes := map[string]string{}
_, err = jsonifier{}.deserialize(hub, []byte(jsonStr), attributes)
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid attributes")
}
func TestDeserialize_ErrorNoTask(t *testing.T) {
hub := NewHub(Config{}, nil)
jsonStr := `{"headers":{"request_id":"request-id"},"id":"message-id","kwargs":{"To":"mail@example.com",` +
`"From":"mail@spammer.com"},"metadata":{"timestamp":1521493587123,"version":"1.0"},` +
`"task":"task_test.SendEmailTask"}`
attributes := map[string]string{
headerTask: "task_test.SendEmailTask",
}
_, err := jsonifier{}.deserialize(hub, []byte(jsonStr), attributes)
assert.Error(t, err)
assert.ErrorIs(t, err, ErrTaskNotFound)
}
func TestDeserialize_ErrorInvalidInput(t *testing.T) {
hub := NewHub(Config{}, nil)
taskRef := SendEmailTask{}
_, err := RegisterTask(hub, "task_test.SendEmailTask", taskRef.Run)
require.NoError(t, err)
jsonStr := `{"headers":{"request_id":"request-id"},"id":"message-id","kwargs":{"To":"mail@example.com",` +
`"From":1},"metadata":{"timestamp":1521493587123,"version":"1.0"},` +
`"task":"task_test.SendEmailTask"}`
attributes := map[string]string{
headerTask: "task_test.SendEmailTask",
}
_, err = jsonifier{}.deserialize(hub, []byte(jsonStr), attributes)
assert.Error(t, err)
assert.Contains(t, err.Error(), "json: cannot unmarshal")
}
func TestSerialize(t *testing.T) {
hub := NewHub(Config{}, nil)
taskRef := SendEmailTask{}
_, err := RegisterTask(hub, "task_test.SendEmailTask", taskRef.Run)
require.NoError(t, err)
jsonStr := `{"headers":{"request_id":"request-id"},"id":"message-id","kwargs":{"to":"mail@example.com",` +
`"from":"mail@spammer.com","time":"2022-04-18T17:36:00Z"},"metadata":{"priority":"default",` +
`"timestamp":1521493587123,"version":"1.0"},"task":"task_test.SendEmailTask"}`
attributes := map[string]string{
headerTask: "task_test.SendEmailTask",
"request_id": "request-id",
}
input := &SendEmailTaskInput{
To: "mail@example.com",
From: "mail@spammer.com",
At: time.UnixMilli(1650303360000).In(time.UTC),
}
m := getValidMessage(t, hub, input)
m.Headers["request_id"] = "request-id"
payload, actualAttributes, err := jsonifier{}.serialize(m)
assert.NoError(t, err)
assert.Equal(t, jsonStr, string(payload))
assert.Equal(t, attributes, actualAttributes)
}
func TestSerialize_ErrorMarshaling(t *testing.T) {
hub := NewHub(Config{}, nil)
taskRef := SendEmailTask{}
_, err := RegisterTask(hub, "task_test.SendEmailTask", taskRef.Run)
require.NoError(t, err)
input := &struct {
// unmarshalable type
Ch chan int
}{make(chan int)}
m := getValidMessage(t, hub, input)
m.Headers["request_id"] = "request-id"
_, _, err = jsonifier{}.serialize(m)
assert.Error(t, err)
assert.Contains(t, err.Error(), "json: unsupported type")
}
func TestSerialize_InvalidHeaders(t *testing.T) {
hub := NewHub(Config{}, nil)
taskRef := SendEmailTask{}
_, err := RegisterTask(hub, "task_test.SendEmailTask", taskRef.Run)
require.NoError(t, err)
input := &SendEmailTaskInput{
To: "mail@example.com",
From: "mail@spammer.com",
At: time.UnixMilli(1650303360000),
}
m := getValidMessage(t, hub, input)
m.Headers["taskhawk_foobar"] = "something"
_, _, err = jsonifier{}.serialize(m)
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid header key")
}