-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsse_test.go
More file actions
49 lines (42 loc) · 1.05 KB
/
Copy pathsse_test.go
File metadata and controls
49 lines (42 loc) · 1.05 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
49
package httpserver
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestWriteData(t *testing.T) {
rec := httptest.NewRecorder()
flusher, ok := any(rec).(http.Flusher)
if !ok {
t.Fatalf("recorder does not implement http.Flusher")
}
if err := WriteData(rec, flusher, map[string]string{"delta": "hello"}); err != nil {
t.Fatalf("WriteData failed: %v", err)
}
body := rec.Body.String()
if !strings.Contains(body, `data: {"delta":"hello"}`) {
t.Fatalf("unexpected body: %q", body)
}
}
func TestHeartbeat(t *testing.T) {
rec := httptest.NewRecorder()
flusher, ok := any(rec).(http.Flusher)
if !ok {
t.Fatalf("recorder does not implement http.Flusher")
}
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() {
defer close(done)
Heartbeat(ctx, rec, flusher, 5*time.Millisecond)
}()
time.Sleep(50 * time.Millisecond)
cancel()
<-done
if !strings.Contains(rec.Body.String(), ": ping\n\n") {
t.Fatalf("expected heartbeat ping, got body=%q", rec.Body.String())
}
}