File: sse-decoder_test.go

package info (click to toggle)
golang-github-gin-contrib-sse 0.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 96 kB
  • sloc: makefile: 2
file content (116 lines) | stat: -rw-r--r-- 2,144 bytes parent folder | download | duplicates (4)
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
// Copyright 2014 Manu Martinez-Almeida.  All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package sse

import (
	"bytes"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestDecodeSingle1(t *testing.T) {
	events, err := Decode(bytes.NewBufferString(
		`data: this is a text
event: message
fake:
id: 123456789010
: we can append data
: and multiple comments should not break it
data: a very nice one`))

	assert.NoError(t, err)
	assert.Len(t, events, 1)
	assert.Equal(t, events[0].Event, "message")
	assert.Equal(t, events[0].Id, "123456789010")
}

func TestDecodeSingle2(t *testing.T) {
	events, err := Decode(bytes.NewBufferString(
		`: starting with a comment
fake:

data:this is a \ntext
event:a message\n\n
fake
:and multiple comments\n should not break it\n\n
id:1234567890\n10
:we can append data
data:a very nice one\n!


`))
	assert.NoError(t, err)
	assert.Len(t, events, 1)
	assert.Equal(t, events[0].Event, "a message\\n\\n")
	assert.Equal(t, events[0].Id, "1234567890\\n10")
}

func TestDecodeSingle3(t *testing.T) {
	events, err := Decode(bytes.NewBufferString(
		`
id:123456ABCabc789010
event: message123
: we can append data
data:this is a text
data: a very nice one
data:
data
: ending with a comment`))

	assert.NoError(t, err)
	assert.Len(t, events, 1)
	assert.Equal(t, events[0].Event, "message123")
	assert.Equal(t, events[0].Id, "123456ABCabc789010")
}

func TestDecodeMulti1(t *testing.T) {
	events, err := Decode(bytes.NewBufferString(
		`
id:
event: weird event
data:this is a text
:data: this should NOT APER
data:  second line

: a comment
event: message
id:123
data:this is a text
:data: this should NOT APER
data:  second line


: a comment
event: message
id:123
data:this is a text
data:  second line

:hola

data

event:

id`))
	assert.NoError(t, err)
	assert.Len(t, events, 3)
	assert.Equal(t, events[0].Event, "weird event")
	assert.Equal(t, events[0].Id, "")
}

func TestDecodeW3C(t *testing.T) {
	events, err := Decode(bytes.NewBufferString(
		`data

data
data

data:
`))
	assert.NoError(t, err)
	assert.Len(t, events, 1)
}