forked from wuman/firego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch_test.go
128 lines (104 loc) · 3.14 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
package firego
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zabawaba99/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)
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 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 TestStopWatch(t *testing.T) {
t.Parallel()
server := firetest.New()
server.Start()
defer server.Close()
fb := New(server.URL, nil)
notifications := make(chan Event)
go func() {
err := fb.Watch(notifications)
assert.NoError(t, err)
}()
<-notifications // get initial notification
fb.StopWatching()
_, ok := <-notifications
assert.False(t, ok, "notifications should be closed")
}