|
| 1 | +package sse_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "github.com/replicate/replicate-go/internal/sse" |
| 13 | +) |
| 14 | + |
| 15 | +func TestDecodeOneEventNoSpace(t *testing.T) { |
| 16 | + input := `event:output |
| 17 | +id:123abc |
| 18 | +data:giraffe |
| 19 | +
|
| 20 | +` |
| 21 | + d := sse.NewDecoder(strings.NewReader(input)) |
| 22 | + |
| 23 | + e, err := d.Next() |
| 24 | + |
| 25 | + require.NoError(t, err) |
| 26 | + |
| 27 | + assert.Equal(t, "output", e.Type) |
| 28 | + assert.Equal(t, "123abc", e.ID) |
| 29 | + assert.Equal(t, "giraffe\n", e.Data) |
| 30 | +} |
| 31 | + |
| 32 | +func TestDecodeOneEventWithSpace(t *testing.T) { |
| 33 | + input := `event: output |
| 34 | +id: 123abc |
| 35 | +data: giraffe |
| 36 | +
|
| 37 | +` |
| 38 | + d := sse.NewDecoder(strings.NewReader(input)) |
| 39 | + |
| 40 | + e, err := d.Next() |
| 41 | + |
| 42 | + require.NoError(t, err) |
| 43 | + |
| 44 | + assert.Equal(t, "output", e.Type) |
| 45 | + assert.Equal(t, "123abc", e.ID) |
| 46 | + // only one space should be trimmed |
| 47 | + assert.Equal(t, " giraffe\n", e.Data) |
| 48 | +} |
| 49 | + |
| 50 | +func TestDecodeOneEventMultipleData(t *testing.T) { |
| 51 | + input := `event:output |
| 52 | +data:giraffe |
| 53 | +data:rhino |
| 54 | +data:wombat |
| 55 | +
|
| 56 | +` |
| 57 | + d := sse.NewDecoder(strings.NewReader(input)) |
| 58 | + |
| 59 | + e, err := d.Next() |
| 60 | + |
| 61 | + require.NoError(t, err) |
| 62 | + |
| 63 | + assert.Equal(t, "output", e.Type) |
| 64 | + assert.Equal(t, "giraffe\nrhino\nwombat\n", e.Data) |
| 65 | +} |
| 66 | + |
| 67 | +func TestDecodeOneEventHugeData(t *testing.T) { |
| 68 | + // this test is mainly to make sure we're not constrained by the |
| 69 | + // bufio.Reader buffer size |
| 70 | + input := fmt.Sprintf(`event:output |
| 71 | +data:%s |
| 72 | +
|
| 73 | +`, strings.Repeat("0123456789abcdef", 1_000_000)) |
| 74 | + d := sse.NewDecoder(strings.NewReader(input)) |
| 75 | + |
| 76 | + e, err := d.Next() |
| 77 | + |
| 78 | + require.NoError(t, err) |
| 79 | + |
| 80 | + assert.Equal(t, "output", e.Type) |
| 81 | + // 16_000_000 data bytes and the terminal LF character |
| 82 | + assert.Equal(t, 16_000_001, len(e.Data)) |
| 83 | +} |
| 84 | + |
| 85 | +func TestDecodeManyEvents(t *testing.T) { |
| 86 | + input := `event:output |
| 87 | +id:alpha1 |
| 88 | +data:giraffe |
| 89 | +
|
| 90 | +event:output |
| 91 | +id:bravo2 |
| 92 | +data:rhino |
| 93 | +
|
| 94 | +event:output |
| 95 | +id:gamma3 |
| 96 | +data:pine marten |
| 97 | +
|
| 98 | +` |
| 99 | + d := sse.NewDecoder(strings.NewReader(input)) |
| 100 | + |
| 101 | + e, err := d.Next() |
| 102 | + |
| 103 | + require.NoError(t, err) |
| 104 | + |
| 105 | + assert.Equal(t, "output", e.Type) |
| 106 | + assert.Equal(t, "alpha1", e.ID) |
| 107 | + assert.Equal(t, "giraffe\n", e.Data) |
| 108 | + |
| 109 | + e, err = d.Next() |
| 110 | + |
| 111 | + require.NoError(t, err) |
| 112 | + |
| 113 | + assert.Equal(t, "output", e.Type) |
| 114 | + assert.Equal(t, "bravo2", e.ID) |
| 115 | + assert.Equal(t, "rhino\n", e.Data) |
| 116 | + |
| 117 | + e, err = d.Next() |
| 118 | + |
| 119 | + require.NoError(t, err) |
| 120 | + |
| 121 | + assert.Equal(t, "output", e.Type) |
| 122 | + assert.Equal(t, "gamma3", e.ID) |
| 123 | + assert.Equal(t, "pine marten\n", e.Data) |
| 124 | +} |
| 125 | + |
| 126 | +func TestDecodeEarlyEOF(t *testing.T) { |
| 127 | + input := `` |
| 128 | + d := sse.NewDecoder(strings.NewReader(input)) |
| 129 | + |
| 130 | + _, err := d.Next() |
| 131 | + |
| 132 | + assert.ErrorIs(t, err, io.ErrUnexpectedEOF) |
| 133 | +} |
0 commit comments