-
Notifications
You must be signed in to change notification settings - Fork 13
/
sse_test.go
285 lines (223 loc) · 6.25 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2015 Julien Schmidt. All rights reserved.
// Use of this source code is governed by MIT license,
// a copy can be found in the LICENSE file.
package sse
import (
"context"
"math"
"net/http"
"strconv"
"testing"
"time"
)
type mockResponseWriter struct {
header http.Header
written string
status int
}
func (m *mockResponseWriter) Header() (h http.Header) {
return m.header
}
func (m *mockResponseWriter) Write(p []byte) (n int, err error) {
m.written += string(p)
return len(p), nil
}
func (m *mockResponseWriter) WriteString(s string) (n int, err error) {
m.written += string(s)
return len(s), nil
}
func (m *mockResponseWriter) WriteHeader(code int) {
m.status = code
}
func NewMockResponseWriter() *mockResponseWriter {
m := new(mockResponseWriter)
m.status = 200
m.header = http.Header{}
return m
}
type mockResponseWriteFlusher struct {
*mockResponseWriter
}
func (m mockResponseWriteFlusher) Flush() {}
func NewMockResponseWriteFlusher() mockResponseWriteFlusher {
return mockResponseWriteFlusher{NewMockResponseWriter()}
}
func NewMockRequest() (*http.Request, context.CancelFunc) {
request, err := http.NewRequest("GET", "MOCK", nil)
if err != nil {
panic(err)
}
context, cancel := context.WithCancel(context.Background())
return request.WithContext(context), cancel
}
func NewMockRequestNeverClose() *http.Request {
request, err := http.NewRequest("GET", "MOCK", nil)
if err != nil {
panic(err)
}
return request
}
func NewMockRequestWithTimeout(d time.Duration) *http.Request {
request, err := http.NewRequest("GET", "MOCK", nil)
if err != nil {
panic(err)
}
context, _ := context.WithTimeout(context.Background(), d)
return request.WithContext(context)
}
// all those good old Java times...
type mockResponseWriteFlushCloser struct {
mockResponseWriteFlusher
}
func NewMockResponseWriteFlushCloser() *mockResponseWriteFlushCloser {
return &mockResponseWriteFlushCloser{
NewMockResponseWriteFlusher(),
}
}
func TestNoFlush(t *testing.T) {
streamer := New()
w := NewMockResponseWriter()
time.Sleep(500 * time.Millisecond)
streamer.ServeHTTP(w, NewMockRequestWithTimeout(500*time.Millisecond))
if w.status != http.StatusNotImplemented {
t.Fatal("wrong status code:", w.status)
}
if w.written != "Flushing not supported\n" {
t.Fatal("wrong error, got:", w.written)
}
}
func TestClose(t *testing.T) {
streamer := New()
w := NewMockResponseWriteFlusher()
time.Sleep(500 * time.Millisecond)
streamer.ServeHTTP(w, NewMockRequestWithTimeout(time.Millisecond))
if w.status != http.StatusOK {
t.Fatal("wrong status code:", w.status)
}
if w.written != "" {
t.Fatal("wrong error, got:", w.written)
}
}
func TestClientConnection(t *testing.T) {
streamer := New()
w := NewMockResponseWriteFlushCloser()
r, cancel := NewMockRequest()
time.Sleep(500 * time.Millisecond)
go func() {
time.Sleep(500 * time.Millisecond)
if len(streamer.clients) != 1 {
t.Fatal("expected 1 client, has:", len(streamer.clients))
}
cancel()
}()
if len(streamer.clients) != 0 {
t.Fatal("expected 0 clients, has:", len(streamer.clients))
}
streamer.ServeHTTP(w, r)
time.Sleep(500 * time.Millisecond)
if len(streamer.clients) != 0 {
t.Fatal("expected 0 clients, has:", len(streamer.clients))
}
if w.status != http.StatusOK {
t.Fatal("wrong status code:", w.status)
}
}
func TestHeader(t *testing.T) {
streamer := New()
w := NewMockResponseWriteFlushCloser()
r, cancel := NewMockRequest()
time.Sleep(500 * time.Millisecond)
go func() {
time.Sleep(500 * time.Millisecond)
cancel()
}()
streamer.ServeHTTP(w, r)
if w.status != http.StatusOK {
t.Fatal("wrong status code:", w.status)
}
var expected = []struct {
header string
value string
}{
{"Cache-Control", "no-cache"},
{"Connection", "keep-alive"},
{"Content-Type", "text/event-stream"},
}
h := w.Header()
for _, header := range expected {
if h.Get(header.header) != header.value {
t.Errorf(
"wrong header value for '%s', expected: '%s', got: '%s'",
header.header,
header.value,
h.Get(header.header),
)
}
}
}
func TestSendEvent(t *testing.T) {
streamer := New()
w := NewMockResponseWriteFlushCloser()
r, cancel := NewMockRequest()
var expected string
time.Sleep(500 * time.Millisecond)
go func() {
time.Sleep(500 * time.Millisecond)
streamer.SendString("", "", "")
expected += "data\n\n"
streamer.SendString("", "", "Test")
expected += "data:Test\n\n"
streamer.SendString("", "msg", "Hi!")
expected += "event:msg\ndata:Hi!\n\n"
streamer.SendString("", "string", "multi\nline\n\nyay")
expected += "event:string\ndata:multi\ndata:line\ndata:\ndata:yay\n\n"
streamer.SendBytes("", "empty", nil)
expected += "event:empty\ndata\n\n"
streamer.SendBytes("", "error", []byte("gnah"))
expected += "event:error\ndata:gnah\n\n"
streamer.SendBytes("", "", []byte("\nline\nbreak\n\n"))
expected += "data:\ndata:line\ndata:break\ndata:\ndata:\n\n"
streamer.SendInt("", "number", math.MaxInt64)
expected += "event:number\ndata:" + strconv.FormatInt(math.MaxInt64, 10) + "\n\n"
streamer.SendUint("", "number", math.MaxUint64)
expected += "event:number\ndata:" + strconv.FormatUint(math.MaxUint64, 10) + "\n\n"
streamer.SendJSON("", "json", nil)
expected += "event:json\ndata:null\n\n"
streamer.SendJSON("", "json", map[string]string{"test": "successful"})
expected += "event:json\ndata:{\"test\":\"successful\"}\n\n"
time.Sleep(500 * time.Millisecond)
cancel()
}()
streamer.ServeHTTP(w, r)
if w.status != http.StatusOK {
t.Fatal("wrong status code:", w.status)
}
if w.written != expected {
t.Fatal("wrong body, got:\n", w.written, "\nexpected:\n", expected)
}
}
func TestJSONErr(t *testing.T) {
streamer := New()
w := NewMockResponseWriteFlushCloser()
r, cancel := NewMockRequest()
var expected string
var err error
time.Sleep(500 * time.Millisecond)
go func() {
time.Sleep(500 * time.Millisecond)
// Inf can not be marshalled
err = streamer.SendJSON("", "json", math.Inf(0))
time.Sleep(500 * time.Millisecond)
cancel()
}()
streamer.ServeHTTP(w, r)
if err == nil {
t.Fatal("expected an error!")
}
if w.status != http.StatusOK {
t.Fatal("wrong status code:", w.status)
}
if w.written != expected {
t.Fatal("wrong body, got:\n", w.written, "\nexpected:\n", expected)
}
}