Skip to content

Commit 59bc7bd

Browse files
committed
add sender tests (#2)
1 parent 633e995 commit 59bc7bd

File tree

2 files changed

+123
-5
lines changed

2 files changed

+123
-5
lines changed

sender.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,7 @@ func (s *Sender) SendEvent(event Event) error {
150150
//When using this method, you must manually call Flush to writr the events.
151151
//The purpose of this method is to speed up sending large batches of events.
152152
func (s *Sender) SendQuick(event Event) error {
153-
err := event.Validate()
154-
if err != nil {
155-
return err
156-
}
157-
_, err = event.WriteTo(s.buf)
153+
_, err := event.WriteTo(s.buf)
158154
if err != nil {
159155
return err
160156
}

sender_test.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package sse
2+
3+
import (
4+
"bytes"
5+
"io/ioutil"
6+
"net/http"
7+
"net/http/httptest"
8+
"testing"
9+
)
10+
11+
func TestValidate(t *testing.T) {
12+
testtable := []struct {
13+
ev Event
14+
err error
15+
}{
16+
{Event{}, ErrNoData},
17+
{Event{Data: "wow\nmultiline"}, nil},
18+
{Event{Data: "wow\rmultiline"}, nil},
19+
{Event{Data: "\000"}, ErrIllegalRune{Rune: '\000', In: "\000", Name: "data"}},
20+
{Event{Name: "\000", Data: "test"}, ErrIllegalRune{Rune: '\000', In: "\000", Name: "name"}},
21+
{Event{Name: "\n", Data: "test"}, ErrIllegalRune{Rune: '\n', In: "\n", Name: "name"}},
22+
{Event{Name: "\r", Data: "test"}, ErrIllegalRune{Rune: '\r', In: "\r", Name: "name"}},
23+
{Event{Name: "test:testing", Data: "good"}, ErrNotNCName},
24+
}
25+
for _, tv := range testtable {
26+
err := tv.ev.Validate()
27+
if err != tv.err {
28+
t.Errorf("Expected error %q but got %q", tv.err, tv.err)
29+
}
30+
}
31+
}
32+
33+
func TestSendEvent(t *testing.T) {
34+
testtable := []struct {
35+
ev Event
36+
output string
37+
err error
38+
}{
39+
{Event{Data: "testing123"}, "data: testing123\n\n", nil},
40+
{Event{Name: "test", Data: "testing123"}, "event: test\ndata: testing123\n\n", nil},
41+
{Event{Name: "testmultiline1", Data: "testing123\n\rtesting456"}, "event: testmultiline1\ndata: testing123\ndata: testing456\n\n", nil},
42+
{Event{Name: "testmultiline2", Data: "testing123\ntesting456"}, "event: testmultiline2\ndata: testing123\ndata: testing456\n\n", nil},
43+
{Event{Name: "testmultiline3", Data: "testing123\rtesting456"}, "event: testmultiline3\ndata: testing123\ndata: testing456\n\n", nil},
44+
{Event{}, "", ErrNoData},
45+
}
46+
for _, tv := range testtable {
47+
rr := httptest.NewRecorder()
48+
s, err := NewSender(rr)
49+
if err != nil {
50+
t.Errorf("failed to create sender: %q", err)
51+
continue
52+
}
53+
err = s.SendEvent(tv.ev)
54+
if err != tv.err {
55+
t.Errorf("expected error %q but got %q", tv.err, err)
56+
continue
57+
}
58+
if err == nil && !rr.Flushed {
59+
t.Errorf("Did not flush (on test case %v)", tv)
60+
}
61+
output, _ := ioutil.ReadAll(rr.Result().Body)
62+
if !bytes.Equal(output, []byte(tv.output)) {
63+
t.Errorf("Expected %q but got %q", tv.output, string(output))
64+
}
65+
}
66+
}
67+
68+
func TestSendJSON(t *testing.T) {
69+
testtable := []struct {
70+
in interface{}
71+
output string
72+
err error
73+
}{
74+
{true, "data: true\n\n", nil},
75+
{[]int{1, 2, 3}, "data: [1,2,3]\n\n", nil},
76+
{nil, "data: null\n\n", nil},
77+
}
78+
for _, tv := range testtable {
79+
rr := httptest.NewRecorder()
80+
s, err := NewSender(rr)
81+
if err != nil {
82+
t.Errorf("failed to create sender: %q", err)
83+
continue
84+
}
85+
err = s.SendJSON(tv.in)
86+
if err != tv.err {
87+
t.Errorf("expected error %q but got %q", tv.err, err)
88+
continue
89+
}
90+
output, _ := ioutil.ReadAll(rr.Result().Body)
91+
if !bytes.Equal(output, []byte(tv.output)) {
92+
t.Errorf("Expected %q but got %q", tv.output, string(output))
93+
}
94+
}
95+
}
96+
97+
type badWriter struct{}
98+
99+
func (badWriter) Header() http.Header { return nil }
100+
func (badWriter) Write([]byte) (int, error) { return 0, nil }
101+
func (badWriter) WriteHeader(statusCode int) {}
102+
103+
func TestFlusher(t *testing.T) {
104+
testtbl := []struct {
105+
w http.ResponseWriter
106+
err error
107+
}{
108+
{httptest.NewRecorder(), nil},
109+
{badWriter{}, ErrFlushNotSupported},
110+
}
111+
for _, tv := range testtbl {
112+
rwf, err := Flusher(tv.w)
113+
if err != tv.err {
114+
t.Errorf("expected error %q but got %q", tv.err, err)
115+
continue
116+
}
117+
if err == nil && rwf == nil {
118+
t.Errorf("no error but no flusher returned (test case %v)", tv)
119+
continue
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)