This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
forked from zabawaba99/firego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch_test.go
232 lines (188 loc) · 6.17 KB
/
watch_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
package firego
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zabawaba99/firego/internal/firetest"
)
func setupLargeResult() string {
return "start" + strings.Repeat("0", 64*1024) + "end"
}
func TestWatch(t *testing.T) {
server := firetest.New()
server.Start()
defer server.Close()
fb := New(server.URL, nil)
notifications := make(chan Event)
err := fb.Watch(notifications)
assert.NoError(t, err)
l := setupLargeResult()
server.Set("/foo", l)
select {
case event, ok := <-notifications:
assert.True(t, ok)
assert.Equal(t, "put", event.Type)
assert.Equal(t, "/", event.Path)
assert.Nil(t, event.Data)
case <-time.After(250 * time.Millisecond):
require.FailNow(t, "did not receive a notification initial notification")
}
select {
case event, ok := <-notifications:
assert.True(t, ok)
assert.Equal(t, "/foo", event.Path)
assert.EqualValues(t, l, event.Data)
var v string
assert.NoError(t, event.Value(&v))
assert.EqualValues(t, l, v)
case <-time.After(250 * time.Millisecond):
require.FailNow(t, "did not receive a notification")
}
}
func TestWatchRedirectPreservesHeader(t *testing.T) {
t.Parallel()
redirectServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
assert.Equal(t, []string{"text/event-stream"}, req.Header["Accept"])
w.Header().Set("Content-Type", "text/event-stream")
w.WriteHeader(http.StatusOK)
}))
defer redirectServer.Close()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Location", redirectServer.URL)
w.WriteHeader(http.StatusTemporaryRedirect)
}))
defer server.Close()
fb := New(server.URL, nil)
notifications := make(chan Event)
err := fb.Watch(notifications)
assert.NoError(t, err)
}
func TestWatchHeartbeatTimeout(t *testing.T) {
var fb Firebase
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
flusher, ok := w.(http.Flusher)
require.True(t, ok, "streaming unsupported")
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")
flusher.Flush()
time.Sleep(2 * fb.(*firebase).watchHeartbeat)
}))
defer server.Close()
notifications := make(chan Event)
fb = New(server.URL, nil)
fb.(*firebase).watchHeartbeat = 50 * time.Millisecond
if err := fb.Watch(notifications); err != nil {
t.Fatal(err)
}
event, ok := <-notifications
require.True(t, ok, "notifications closed")
assert.Equal(t, EventTypeError, event.Type, "event type doesn't match")
assert.Empty(t, event.Path, "event path is not empty")
assert.NotNil(t, event.Data, "event data is nil")
assert.Implements(t, new(error), event.Data)
t.Logf("%#v\n", event)
_, ok = <-notifications
require.False(t, ok, "notifications still open")
}
func TestWatchError(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
flusher, ok := w.(http.Flusher)
require.True(t, ok, "streaming unsupported")
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")
flusher.Flush()
}))
var (
notifications = make(chan Event)
fb = New(server.URL, nil)
)
defer server.Close()
if err := fb.Watch(notifications); err != nil {
t.Fatal(err)
}
go server.Close()
event, ok := <-notifications
require.True(t, ok, "notifications closed")
assert.Equal(t, EventTypeError, event.Type, "event type doesn't match")
assert.Empty(t, event.Path, "event path is not empty")
assert.NotNil(t, event.Data, "event data is nil")
assert.Implements(t, new(error), event.Data)
}
func TestWatchAuthRevoked(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
flusher, ok := w.(http.Flusher)
require.True(t, ok, "streaming unsupported")
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")
flusher.Flush()
fmt.Fprintf(w, "event: %s\ndata: %q\n\n", EventTypeAuthRevoked, "token expired")
}))
defer server.Close()
var (
notifications = make(chan Event)
fb = New(server.URL, nil)
)
if err := fb.Watch(notifications); err != nil {
t.Fatal(err)
}
event, ok := <-notifications
require.True(t, ok, "notifications closed")
assert.Equal(t, EventTypeAuthRevoked, event.Type, "event type doesn't match")
assert.Empty(t, event.Path, "event path is not empty")
assert.Equal(t, event.Data, `"token expired"`, "event data does not match")
}
func TestWatch_Issue66(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
flusher, ok := w.(http.Flusher)
require.True(t, ok, "streaming unsupported")
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")
flusher.Flush()
}))
defer server.Close()
// create an initial sse connection
fb := New(server.URL, nil)
notifications := make(chan Event)
err := fb.Watch(notifications)
require.NoError(t, err)
// read the first connection
<-notifications
// close the server connection and read the error event
go server.Close()
<-notifications
// call stop watching - everything should be a-ok
fb.StopWatching()
_, ok := <-notifications
assert.False(t, ok)
}
func TestStopWatch(t *testing.T) {
t.Parallel()
server := firetest.New()
server.Start()
defer server.Close()
fb := New(server.URL, nil)
notifications := make(chan Event)
err := fb.Watch(notifications)
assert.NoError(t, err)
<-notifications // get initial notification
fb.StopWatching()
_, ok := <-notifications
assert.False(t, ok, "notifications should be closed")
}