-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream_test.go
46 lines (39 loc) · 1.09 KB
/
stream_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
package requests
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestStream(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
for i := 0; i < 3; i++ {
fmt.Fprintf(w, "data: Message %d\n", i)
w.(http.Flusher).Flush()
time.Sleep(100 * time.Millisecond)
}
}))
defer server.Close()
doneCh := make(chan struct{})
dataReceived := make([]string, 0)
client := Create(&Config{BaseURL: server.URL})
_, err := client.Get("/").Stream(func(data []byte) error {
dataReceived = append(dataReceived, string(data))
assert.Contains(t, string(data), "data: Message")
return nil
}).StreamErr(func(err error) {
assert.NoError(t, err)
}).StreamDone(func() {
assert.Equal(t, 3, len(dataReceived))
close(doneCh)
}).Send(context.Background())
assert.NoError(t, err)
// Proper synchronization to ensure all callbacks have completed
time.Sleep(1 * time.Second)
<-doneCh
assert.Equal(t, 3, len(dataReceived))
}