-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsse_test.go
121 lines (95 loc) · 2.58 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
// Copyright 2022 Flamego. 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"
"net/http/httptest"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/flamego/flamego"
)
type mockResponseWriter struct {
lock sync.Mutex
*httptest.ResponseRecorder
}
func (m *mockResponseWriter) Write(buf []byte) (int, error) {
m.lock.Lock()
defer m.lock.Unlock()
return m.ResponseRecorder.Write(buf)
}
func (m *mockResponseWriter) Body() string {
m.lock.Lock()
defer m.lock.Unlock()
return m.ResponseRecorder.Body.String()
}
func TestBind(t *testing.T) {
f := flamego.NewWithLogger(&bytes.Buffer{})
type object struct {
Message string
}
f.Get("/normal",
Bind(object{}),
func(msg chan<- *object) {
msg <- &object{Message: "Flamego"}
// Sleep for the response flushed.
time.Sleep(1 * time.Second)
},
)
f.Get("/ping",
Bind(
object{},
Options{
300 * time.Millisecond, // Sleep for 1 second, so we can get 3 pings.
},
),
func(msg chan<- *object) {
msg <- &object{Message: "Flamego"}
// Sleep for the response flushed.
time.Sleep(1 * time.Second)
},
)
t.Run("normal", func(t *testing.T) {
resp := &mockResponseWriter{
ResponseRecorder: httptest.NewRecorder(),
}
req, err := http.NewRequest(http.MethodGet, "/normal", nil)
require.NoError(t, err)
f.ServeHTTP(resp, req)
assert.Equal(t, resp.Code, http.StatusOK)
assert.Equal(t, "text/event-stream", resp.Header().Get("Content-Type"))
assert.Equal(t, "no-cache", resp.Header().Get("Cache-Control"))
assert.Equal(t, "keep-alive", resp.Header().Get("Connection"))
assert.Equal(t, "no", resp.Header().Get("X-Accel-Buffering"))
wantBody := `: ping
events: stream opened
data: {"Message":"Flamego"}
`
assert.Equal(t, wantBody, resp.Body())
})
t.Run("ping", func(t *testing.T) {
resp := &mockResponseWriter{
ResponseRecorder: httptest.NewRecorder(),
}
req, err := http.NewRequest(http.MethodGet, "/ping", nil)
require.NoError(t, err)
f.ServeHTTP(resp, req)
assert.Equal(t, resp.Code, http.StatusOK)
assert.Equal(t, "text/event-stream", resp.Header().Get("Content-Type"))
assert.Equal(t, "no-cache", resp.Header().Get("Cache-Control"))
assert.Equal(t, "keep-alive", resp.Header().Get("Connection"))
assert.Equal(t, "no", resp.Header().Get("X-Accel-Buffering"))
wantBody := `: ping
events: stream opened
data: {"Message":"Flamego"}
: ping
: ping
: ping
`
assert.Equal(t, wantBody, resp.Body())
})
}