forked from donovanhide/eventsource
-
Notifications
You must be signed in to change notification settings - Fork 8
/
decoder_test.go
143 lines (116 loc) · 4.3 KB
/
decoder_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
143
package eventsource
import (
"io"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDecode(t *testing.T) {
tests := []struct {
rawInput string
wantedEvents []*publication
}{
{
rawInput: "event: eventName\ndata: {\"sample\":\"value\"}\n\n",
wantedEvents: []*publication{{event: "eventName", data: "{\"sample\":\"value\"}"}},
},
{
// the newlines should not be parsed as empty event
rawInput: "\n\n\nevent: event1\n\n\n\n\nevent: event2\n\n",
wantedEvents: []*publication{{event: "event1"}, {event: "event2"}},
},
{
rawInput: "id: abc\ndata: def\n\n",
wantedEvents: []*publication{{id: "abc", lastEventID: "abc", data: "def"}},
},
{
// id field should be ignored if it contains a null
rawInput: "id: a\x00bc\ndata: def\n\n",
wantedEvents: []*publication{{data: "def"}},
},
}
for _, test := range tests {
decoder := NewDecoder(strings.NewReader(test.rawInput))
i := 0
for {
event, err := decoder.Decode()
if err == io.EOF {
break
}
require.NoError(t, err, "for input: %q", test.rawInput)
assert.Equal(t, test.wantedEvents[i], event, "for input: %q", test.rawInput)
i++
}
assert.Equal(t, len(test.wantedEvents), i, "Wrong number of decoded events")
}
}
func requireLastEventID(t *testing.T, event Event) string {
// necessary because we can't yet add LastEventID to the basic Event interface; see EventWithLastID
eventWithID, ok := event.(EventWithLastID)
require.True(t, ok, "event should have implemented EventWithLastID")
return eventWithID.LastEventID()
}
func TestDecoderTracksLastEventID(t *testing.T) {
t.Run("uses last ID that is passed in options", func(t *testing.T) {
inputData := "data: abc\n\n"
decoder := NewDecoderWithOptions(strings.NewReader(inputData), DecoderOptionLastEventID("my-id"))
event, err := decoder.Decode()
require.NoError(t, err)
assert.Equal(t, "abc", event.Data())
assert.Equal(t, "", event.Id())
assert.Equal(t, "my-id", requireLastEventID(t, event))
})
t.Run("last ID persists if not overridden", func(t *testing.T) {
inputData := "id: abc\ndata: first\n\ndata: second\n\nid: def\ndata:third\n\n"
decoder := NewDecoderWithOptions(strings.NewReader(inputData), DecoderOptionLastEventID("my-id"))
event1, err := decoder.Decode()
require.NoError(t, err)
assert.Equal(t, "first", event1.Data())
assert.Equal(t, "abc", event1.Id())
assert.Equal(t, "abc", requireLastEventID(t, event1))
event2, err := decoder.Decode()
require.NoError(t, err)
assert.Equal(t, "second", event2.Data())
assert.Equal(t, "", event2.Id())
assert.Equal(t, "abc", requireLastEventID(t, event2))
event3, err := decoder.Decode()
require.NoError(t, err)
assert.Equal(t, "third", event3.Data())
assert.Equal(t, "def", event3.Id())
assert.Equal(t, "def", requireLastEventID(t, event3))
})
t.Run("last ID persists if not overridden", func(t *testing.T) {
inputData := "id: abc\ndata: first\n\ndata: second\n\nid: def\ndata:third\n\n"
decoder := NewDecoderWithOptions(strings.NewReader(inputData), DecoderOptionLastEventID("my-id"))
event1, err := decoder.Decode()
require.NoError(t, err)
assert.Equal(t, "first", event1.Data())
assert.Equal(t, "abc", event1.Id())
assert.Equal(t, "abc", requireLastEventID(t, event1))
event2, err := decoder.Decode()
require.NoError(t, err)
assert.Equal(t, "second", event2.Data())
assert.Equal(t, "", event2.Id())
assert.Equal(t, "abc", requireLastEventID(t, event2))
event3, err := decoder.Decode()
require.NoError(t, err)
assert.Equal(t, "third", event3.Data())
assert.Equal(t, "def", event3.Id())
assert.Equal(t, "def", requireLastEventID(t, event3))
})
t.Run("last ID can be overridden with empty string", func(t *testing.T) {
inputData := "id: abc\ndata: first\n\nid: \ndata: second\n\n"
decoder := NewDecoderWithOptions(strings.NewReader(inputData), DecoderOptionLastEventID("my-id"))
event1, err := decoder.Decode()
require.NoError(t, err)
assert.Equal(t, "first", event1.Data())
assert.Equal(t, "abc", event1.Id())
assert.Equal(t, "abc", requireLastEventID(t, event1))
event2, err := decoder.Decode()
require.NoError(t, err)
assert.Equal(t, "second", event2.Data())
assert.Equal(t, "", event2.Id())
assert.Equal(t, "", requireLastEventID(t, event2))
})
}