github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/sse/sse-decoder_test.go (about) 1 // Copyright 2014 Manu Martinez-Almeida. All rights reserved. 2 // Use of this source code is governed by a MIT style 3 // license that can be found in the LICENSE file. 4 5 package sse 6 7 import ( 8 "bytes" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestDecodeSingle1(t *testing.T) { 15 events, err := Decode(bytes.NewBufferString( 16 `data: this is a text 17 event: message 18 fake: 19 id: 123456789010 20 : we can append data 21 : and multiple comments should not break it 22 data: a very nice one`)) 23 24 assert.NoError(t, err) 25 assert.Len(t, events, 1) 26 assert.Equal(t, events[0].Event, "message") 27 assert.Equal(t, events[0].Id, "123456789010") 28 } 29 30 func TestDecodeSingle2(t *testing.T) { 31 events, err := Decode(bytes.NewBufferString( 32 `: starting with a comment 33 fake: 34 35 data:this is a \ntext 36 event:a message\n\n 37 fake 38 :and multiple comments\n should not break it\n\n 39 id:1234567890\n10 40 :we can append data 41 data:a very nice one\n! 42 43 44 `)) 45 assert.NoError(t, err) 46 assert.Len(t, events, 1) 47 assert.Equal(t, events[0].Event, "a message\\n\\n") 48 assert.Equal(t, events[0].Id, "1234567890\\n10") 49 } 50 51 func TestDecodeSingle3(t *testing.T) { 52 events, err := Decode(bytes.NewBufferString( 53 ` 54 id:123456ABCabc789010 55 event: message123 56 : we can append data 57 data:this is a text 58 data: a very nice one 59 data: 60 data 61 : ending with a comment`)) 62 63 assert.NoError(t, err) 64 assert.Len(t, events, 1) 65 assert.Equal(t, events[0].Event, "message123") 66 assert.Equal(t, events[0].Id, "123456ABCabc789010") 67 } 68 69 func TestDecodeMulti1(t *testing.T) { 70 events, err := Decode(bytes.NewBufferString( 71 ` 72 id: 73 event: weird event 74 data:this is a text 75 :data: this should NOT APER 76 data: second line 77 78 : a comment 79 event: message 80 id:123 81 data:this is a text 82 :data: this should NOT APER 83 data: second line 84 85 86 : a comment 87 event: message 88 id:123 89 data:this is a text 90 data: second line 91 92 :hola 93 94 data 95 96 event: 97 98 id`)) 99 assert.NoError(t, err) 100 assert.Len(t, events, 3) 101 assert.Equal(t, events[0].Event, "weird event") 102 assert.Equal(t, events[0].Id, "") 103 } 104 105 func TestDecodeW3C(t *testing.T) { 106 events, err := Decode(bytes.NewBufferString( 107 `data 108 109 data 110 data 111 112 data: 113 `)) 114 assert.NoError(t, err) 115 assert.Len(t, events, 1) 116 }