-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstream_test.go
More file actions
48 lines (41 loc) · 1.08 KB
/
stream_test.go
File metadata and controls
48 lines (41 loc) · 1.08 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
package requests
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/test-go/testify/require"
)
func TestStream(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
for i := range 3 {
_, _ = fmt.Fprintf(w, "data: Message %d\n", i)
w.(http.Flusher).Flush()
}
}))
defer server.Close()
doneCh := make(chan struct{})
dataReceived := make([]string, 0, 3)
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() {
close(doneCh)
}).Send(context.Background())
require.NoError(t, err)
select {
case <-doneCh:
case <-time.After(time.Second):
t.Fatal("expected stream callbacks to finish")
}
assert.Equal(t, 3, len(dataReceived))
}