-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsse_test.go
More file actions
164 lines (133 loc) · 3.57 KB
/
Copy pathsse_test.go
File metadata and controls
164 lines (133 loc) · 3.57 KB
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
// 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"
"context"
"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)
},
)
f.Get("/ticker",
Bind(
object{},
Options{
100 * time.Millisecond,
},
),
func(ctx flamego.Context, msg chan<- *object) {
ticker := time.NewTicker(1 * time.Second)
defer func() { ticker.Stop() }()
for {
select {
case <-ticker.C:
msg <- &object{Message: "Flamego"}
case <-ctx.Request().Context().Done():
return
}
}
},
)
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())
})
t.Run("close connection", func(t *testing.T) {
server := httptest.NewServer(f)
reqContext, cancel := context.WithCancel(context.Background())
req, err := http.NewRequestWithContext(reqContext, http.MethodGet, server.URL+"/ticker", nil)
require.NoError(t, err)
// Close request connection after 1 second.
go func() {
time.Sleep(1 * time.Second)
cancel()
}()
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
err = resp.Body.Close()
require.NoError(t, err)
// Sleep for 3 seconds to wait for new responses that may be in a closed request.
time.Sleep(3 * time.Second)
})
}