-
Notifications
You must be signed in to change notification settings - Fork 52
/
sse_test.go
255 lines (223 loc) · 5.1 KB
/
sse_test.go
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// 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"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestEncodeOnlyData(t *testing.T) {
w := new(bytes.Buffer)
event := Event{
Data: "junk\n\njk\nid:fake",
}
err := Encode(w, event)
assert.NoError(t, err)
assert.Equal(t, w.String(),
`data:junk
data:
data:jk
data:id:fake
`)
decoded, _ := Decode(w)
assert.Equal(t, decoded, []Event{event})
}
func TestEncodeWithEvent(t *testing.T) {
w := new(bytes.Buffer)
event := Event{
Event: "t\n:<>\r\test",
Data: "junk\n\njk\nid:fake",
}
err := Encode(w, event)
assert.NoError(t, err)
assert.Equal(t, w.String(),
`event:t\n:<>\r est
data:junk
data:
data:jk
data:id:fake
`)
decoded, _ := Decode(w)
assert.Equal(t, decoded, []Event{event})
}
func TestEncodeWithId(t *testing.T) {
w := new(bytes.Buffer)
err := Encode(w, Event{
Id: "t\n:<>\r\test",
Data: "junk\n\njk\nid:fa\rke",
})
assert.NoError(t, err)
assert.Equal(t, w.String(),
`id:t\n:<>\r est
data:junk
data:
data:jk
data:id:fa\rke
`)
}
func TestEncodeWithRetry(t *testing.T) {
w := new(bytes.Buffer)
err := Encode(w, Event{
Retry: 11,
Data: "junk\n\njk\nid:fake\n",
})
assert.NoError(t, err)
assert.Equal(t, w.String(),
`retry:11
data:junk
data:
data:jk
data:id:fake
data:
`)
}
func TestEncodeWithEverything(t *testing.T) {
w := new(bytes.Buffer)
err := Encode(w, Event{
Event: "abc",
Id: "12345",
Retry: 10,
Data: "some data",
})
assert.NoError(t, err)
assert.Equal(t, w.String(), "id:12345\nevent:abc\nretry:10\ndata:some data\n\n")
}
func TestEncodeMap(t *testing.T) {
w := new(bytes.Buffer)
err := Encode(w, Event{
Event: "a map",
Data: map[string]interface{}{
"foo": "b\n\rar",
"bar": "id: 2",
},
})
assert.NoError(t, err)
assert.Equal(t, w.String(), "event:a map\ndata:{\"bar\":\"id: 2\",\"foo\":\"b\\n\\rar\"}\n\n")
}
func TestEncodeSlice(t *testing.T) {
w := new(bytes.Buffer)
err := Encode(w, Event{
Event: "a slice",
Data: []interface{}{1, "text", map[string]interface{}{"foo": "bar"}},
})
assert.NoError(t, err)
assert.Equal(t, w.String(), "event:a slice\ndata:[1,\"text\",{\"foo\":\"bar\"}]\n\n")
}
func TestEncodeStruct(t *testing.T) {
myStruct := struct {
A int
B string `json:"value"`
}{1, "number"}
w := new(bytes.Buffer)
err := Encode(w, Event{
Event: "a struct",
Data: myStruct,
})
assert.NoError(t, err)
assert.Equal(t, w.String(), "event:a struct\ndata:{\"A\":1,\"value\":\"number\"}\n\n")
w.Reset()
err = Encode(w, Event{
Event: "a struct",
Data: &myStruct,
})
assert.NoError(t, err)
assert.Equal(t, w.String(), "event:a struct\ndata:{\"A\":1,\"value\":\"number\"}\n\n")
}
func TestEncodeInteger(t *testing.T) {
w := new(bytes.Buffer)
err := Encode(w, Event{
Event: "an integer",
Data: 1,
})
assert.NoError(t, err)
assert.Equal(t, w.String(), "event:an integer\ndata:1\n\n")
}
func TestEncodeFloat(t *testing.T) {
w := new(bytes.Buffer)
err := Encode(w, Event{
Event: "Float",
Data: 1.5,
})
assert.NoError(t, err)
assert.Equal(t, w.String(), "event:Float\ndata:1.5\n\n")
}
func TestEncodeStream(t *testing.T) {
w := new(bytes.Buffer)
Encode(w, Event{
Event: "float",
Data: 1.5,
})
Encode(w, Event{
Id: "123",
Data: map[string]interface{}{"foo": "bar", "bar": "foo"},
})
Encode(w, Event{
Id: "124",
Event: "chat",
Data: "hi! dude",
})
assert.Equal(t, w.String(), "event:float\ndata:1.5\n\nid:123\ndata:{\"bar\":\"foo\",\"foo\":\"bar\"}\n\nid:124\nevent:chat\ndata:hi! dude\n\n")
}
func TestRenderSSE(t *testing.T) {
w := httptest.NewRecorder()
err := (Event{
Event: "msg",
Data: "hi! how are you?",
}).Render(w)
assert.NoError(t, err)
assert.Equal(t, w.Body.String(), "event:msg\ndata:hi! how are you?\n\n")
assert.Equal(t, w.Header().Get("Content-Type"), "text/event-stream")
assert.Equal(t, w.Header().Get("Cache-Control"), "no-cache")
}
func BenchmarkResponseWriter(b *testing.B) {
w := httptest.NewRecorder()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
(Event{
Event: "new_message",
Data: "hi! how are you? I am fine. this is a long stupid message!!!",
}).Render(w)
}
}
func BenchmarkFullSSE(b *testing.B) {
buf := new(bytes.Buffer)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
Encode(buf, Event{
Event: "new_message",
Id: "13435",
Retry: 10,
Data: "hi! how are you? I am fine. this is a long stupid message!!!",
})
buf.Reset()
}
}
func BenchmarkNoRetrySSE(b *testing.B) {
buf := new(bytes.Buffer)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
Encode(buf, Event{
Event: "new_message",
Id: "13435",
Data: "hi! how are you? I am fine. this is a long stupid message!!!",
})
buf.Reset()
}
}
func BenchmarkSimpleSSE(b *testing.B) {
buf := new(bytes.Buffer)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
Encode(buf, Event{
Event: "new_message",
Data: "hi! how are you? I am fine. this is a long stupid message!!!",
})
buf.Reset()
}
}