Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions sse/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,7 @@ func (d *Decoder) dispatchEvent(event *Event, dataBuf, eventType string, dataLin
}
event.Split = dataLines > 1

// Decide Data vs Comment and JSON decode if possible.
if !strings.HasPrefix(dataBuf, ":") {
d.parseEventData(event, dataBuf)
}
// else {
// // If we ever want to return comments as events, we can do it here.
// }
d.parseEventData(event, dataBuf)

return nil
}
Expand Down
62 changes: 62 additions & 0 deletions sse/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,48 @@ func TestDecoder(t *testing.T) {
input: ": test\n: another\n\n",
expected: nil,
},
{
name: "data beginning with colon",
input: "data: :hello\n\n",
expected: []Event{
{Data: Raw(":hello")},
},
},
{
name: "data beginning with colon without separator space",
input: "data::hello\n\n",
expected: []Event{
{Data: Raw(":hello")},
},
},
{
name: "data containing only a colon",
input: "data: :\n\n",
expected: []Event{
{Data: Raw(":")},
},
},
{
name: "multiline colon data with comments and metadata",
input: ": before\nid: 42\nevent: update\nretry: 5000\ndata: :first\n: between\ndata: second\n\n",
expected: []Event{
{
ID: "42",
Event: "update",
Retry: 5000 * time.Millisecond,
Data: Raw(":first\nsecond"),
Split: true,
},
},
},
{
name: "colon data followed by another event",
input: "data: :first\n\ndata: second\n\n",
expected: []Event{
{Data: Raw(":first")},
{Data: Raw("second")},
},
},
{
name: "multiple colons in data",
input: "data: key: value: test\n\n",
Expand Down Expand Up @@ -695,3 +737,23 @@ func TestEvent_UnmarshalText(t *testing.T) {
})
}
}

func TestEvent_TextRoundTripWithColonData(t *testing.T) {
for _, data := range []Raw{Raw(":"), Raw(":hello"), Raw(":first\nsecond")} {
t.Run(string(data), func(t *testing.T) {
original := Event{
ID: "42",
Event: "update",
Retry: 5000 * time.Millisecond,
Data: data,
Split: strings.Contains(string(data), "\n"),
}
text, err := original.MarshalText()
require.NoError(t, err)

var decoded Event
require.NoError(t, decoded.UnmarshalText(text))
assert.Equal(t, original, decoded)
})
}
}