-
Notifications
You must be signed in to change notification settings - Fork 2
/
api_test.go
194 lines (160 loc) · 4.7 KB
/
api_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
package firebase
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func verifyStreamRequest(request *http.Request) {
Expect(request.Method).To(Equal("GET"))
Expect(request.Header.Get("Accept")).To(Equal("text/event-stream"))
}
var _ = Describe("Firebase SSE/Event Source client", func() {
var (
testServer *httptest.Server
testClient *client
testAPI Api
handler func(w http.ResponseWriter, r *http.Request)
nullHandler func(w http.ResponseWriter, r *http.Request)
stopChannel chan bool
)
BeforeEach(func() {
nullHandler = func(w http.ResponseWriter, r *http.Request) {
verifyStreamRequest(r)
// no events, just terminate the session
}
})
JustBeforeEach(func() {
testServer, testClient = fakeServer(http.HandlerFunc(handler))
testClient = testClient.Child("").(*client)
testAPI = testClient.api
stopChannel = make(chan bool)
})
AfterEach(func() {
close(stopChannel)
testServer.Close()
})
Context("When the connection terminates with EOF", func() {
BeforeEach(func() {
handler = nullHandler
})
It("Receives an empty event", func() {
events, err := testAPI.Stream(testClient.url, testAuth, nil, nil,
stopChannel)
Expect(err).To(BeNil())
Eventually(events).Should(Receive(Equal(RawEvent{})))
})
})
Context("Handling a single event", func() {
BeforeEach(func() {
handler = func(w http.ResponseWriter, r *http.Request) {
verifyStreamRequest(r)
fmt.Fprintln(w, "event: hi")
fmt.Fprintln(w, "data: there")
}
})
It("Fires a single event", func() {
expectedEvent := RawEvent{Event: "hi", Data: "there"}
events, err := testAPI.Stream(testClient.url, testAuth, nil, nil,
stopChannel)
Expect(err).To(BeNil())
Eventually(events).Should(Receive(Equal(expectedEvent)))
})
})
Context("Handling two events", func() {
BeforeEach(func() {
handler = func(w http.ResponseWriter, r *http.Request) {
verifyStreamRequest(r)
fmt.Fprintln(w, "event: hi")
fmt.Fprintln(w, "data: there")
fmt.Fprintf(w, "\n")
fmt.Fprintln(w, "event: hey")
fmt.Fprintln(w, "data: you")
}
})
It("Fires two events", func() {
expectedEvent1 := RawEvent{Event: "hi", Data: "there"}
expectedEvent2 := RawEvent{Event: "hey", Data: "you"}
events, err := testAPI.Stream(testClient.url, testAuth, nil, nil,
stopChannel)
Expect(err).To(BeNil())
Eventually(events).Should(Receive(Equal(expectedEvent1)))
Eventually(events).Should(Receive(Equal(expectedEvent2)))
})
})
})
var _ = Describe("Parsing timeouts / tunables from env variables", func() {
var (
testVariable = "FIREBASE_TIMEOUT_TEST"
defaultDuration = time.Duration(time.Second)
defaultTunable = 1
)
AfterEach(func() {
os.Setenv(testVariable, "")
})
Context("Custom timeout specified via environment variable", func() {
var (
testTimeout = "5m7s"
expectedDuration = time.Duration(5*time.Minute + 7*time.Second)
)
BeforeEach(func() {
os.Setenv(testVariable, testTimeout)
})
It("Returns the correct duration from an environment variable", func() {
amount := parseTimeout(testVariable, defaultDuration)
Expect(amount).To(Equal(expectedDuration))
})
})
Context("No custom timeout specified via environment variable", func() {
BeforeEach(func() {
os.Setenv(testVariable, "")
})
It("Returns the default duration", func() {
amount := parseTimeout(testVariable, defaultDuration)
Expect(amount).To(Equal(defaultDuration))
})
})
Context("Unparsable timeout specified via environment variable", func() {
BeforeEach(func() {
os.Setenv(testVariable, "zzzz")
})
It("Returns the default duration", func() {
amount := parseTimeout(testVariable, defaultDuration)
Expect(amount).To(Equal(defaultDuration))
})
})
Context("Custom tunable specified via env variable", func() {
var (
testTunable = "10"
expectedValue = 10
)
BeforeEach(func() {
os.Setenv(testVariable, testTunable)
})
It("Returns the correct tunable from an environment variable", func() {
tunable := parseTunable(testVariable, defaultTunable)
Expect(tunable).To(Equal(expectedValue))
})
})
Context("No custom tunable specified via env variable", func() {
BeforeEach(func() {
os.Setenv(testVariable, "")
})
It("Returns the default tunable", func() {
tunable := parseTunable(testVariable, defaultTunable)
Expect(tunable).To(Equal(defaultTunable))
})
})
Context("Unparsable tunable specified via env variable", func() {
BeforeEach(func() {
os.Setenv(testVariable, "zzzz")
})
It("Returns the default tunable", func() {
tunable := parseTunable(testVariable, defaultTunable)
Expect(tunable).To(Equal(defaultTunable))
})
})
})