-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevent_test.go
More file actions
218 lines (213 loc) · 4.69 KB
/
event_test.go
File metadata and controls
218 lines (213 loc) · 4.69 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package sse
import (
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEvent_MarshalText(t *testing.T) {
tests := []struct {
name string
event Event
expected string
expectedErr string
isValidationErr bool // Flag to check if the error should be a ValidationError
}{
// Valid cases
{
name: "simple message event",
event: Event{
ID: "1",
Data: "hello world",
},
expected: "id: 1\ndata: \"hello world\"\n\n",
},
{
name: "full event with all fields",
event: Event{
ID: "42",
Event: "update",
Data: map[string]string{"status": "ok"},
Retry: 5000 * time.Millisecond,
},
expected: "id: 42\nretry: 5000\nevent: update\ndata: {\"status\":\"ok\"}\n\n",
},
{
name: "multiline data as slice",
event: Event{
Data: []string{"line1", "line2", "line3"},
},
expected: "data: [\"line1\",\"line2\",\"line3\"]\n\n",
},
{
name: "raw data with split lines",
event: Event{
Data: Raw("line1\nline2\nline3"),
Split: true,
},
expected: "data: line1\ndata: line2\ndata: line3\n\n",
},
{
name: "raw data without split",
event: Event{
Data: Raw("line1\nline2\nline3"),
Split: false,
},
expectedErr: "raw data contains newlines but Split is false",
isValidationErr: true,
},
{
name: "raw data with newlines requires split",
event: Event{
Data: Raw("line1\nline2\nline3"),
Split: true,
},
expected: "data: line1\ndata: line2\ndata: line3\n\n",
},
{
name: "empty data field",
event: Event{
Data: "",
},
expected: "data: \"\"\n\n",
},
{
name: "zero values",
event: Event{},
expected: "\n", // Just the terminating newline
},
{
name: "custom event type only",
event: Event{
Event: "custom",
},
expected: "event: custom\n\n",
},
{
name: "retry only",
event: Event{
Retry: 1000 * time.Millisecond,
},
expected: "retry: 1000\n\n",
},
{
name: "complex nested data",
event: Event{
Data: map[string]interface{}{
"users": []map[string]interface{}{
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"},
},
},
},
expected: "data: {\"users\":[{\"id\":1,\"name\":\"Alice\"},{\"id\":2,\"name\":\"Bob\"}]}\n\n",
},
{
name: "data with special characters",
event: Event{
Data: "line1\nline2\rline3",
},
expected: "data: \"line1\\nline2\\rline3\"\n\n",
},
{
name: "message event type",
event: Event{
Event: "message",
Data: "hello",
},
expected: "data: \"hello\"\n\n", // "message" event type should be omitted
},
{
name: "raw data with special characters",
event: Event{
Data: Raw("hello: world"),
Split: false,
},
expected: "data: hello: world\n\n",
},
{
name: "data with trailing newline",
event: Event{
Data: Raw("line1\n"),
Split: true,
},
expected: "data: line1\n\n", // spec 9.2.6: remove trailing LF from data buffer
},
{
name: "multiple data lines with trailing newlines",
event: Event{
Data: Raw("line1\n\nline2\n"),
Split: true,
},
expected: "data: line1\ndata: \ndata: line2\n\n",
},
{
name: "retry with different duration units",
event: Event{
Retry: 5 * time.Second,
},
expected: "retry: 5000\n\n",
},
{
name: "retry with minutes",
event: Event{
Retry: 2 * time.Minute,
},
expected: "retry: 120000\n\n",
},
{
name: "retry with hours",
event: Event{
Retry: time.Hour,
},
expected: "retry: 3600000\n\n",
},
{
name: "retry with decimal milliseconds",
event: Event{
Retry: 2*time.Second + 500*time.Millisecond + 500*time.Microsecond,
},
expected: "retry: 2500\n\n", // Should round 2500.5ms to 2500ms, spec only allows integers
},
// Error cases
{
name: "invalid data type",
event: Event{
Data: complex(1, 2),
},
expectedErr: "json encoding failed",
isValidationErr: true,
},
{
name: "negative ID is valid per spec",
event: Event{
ID: "-1",
},
expected: "id: -1\n\n",
},
{
name: "invalid retry",
event: Event{
Retry: -1000 * time.Millisecond,
},
expectedErr: "retry must be >=0",
isValidationErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := tt.event.MarshalText()
if tt.expectedErr != "" {
assert.Error(t, err)
if tt.isValidationErr {
assert.True(t, errors.Is(err, ErrValidation), "Expected ValidationError but got: %v", err)
}
assert.Contains(t, err.Error(), tt.expectedErr)
} else {
require.NoError(t, err)
assert.Equal(t, tt.expected, string(result))
}
})
}
}