diff --git a/sse/decoder.go b/sse/decoder.go index 12cd9346..1b3f0893 100644 --- a/sse/decoder.go +++ b/sse/decoder.go @@ -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 } diff --git a/sse/decoder_test.go b/sse/decoder_test.go index 35e75192..ff7a337d 100644 --- a/sse/decoder_test.go +++ b/sse/decoder_test.go @@ -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", @@ -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) + }) + } +}