...
1 package eventsource
2
3 import (
4 "bytes"
5 "testing"
6 )
7
8 type testEvent struct {
9 id, event, data string
10 }
11
12 func (e *testEvent) Id() string { return e.id }
13 func (e *testEvent) Event() string { return e.event }
14 func (e *testEvent) Data() string { return e.data }
15
16 var encoderTests = []struct {
17 event *testEvent
18 output string
19 }{
20 {&testEvent{"1", "Add", "This is a test"}, "id: 1\nevent: Add\ndata: This is a test\n\n"},
21 {&testEvent{"", "", "This message, it\nhas two lines."}, "data: This message, it\ndata: has two lines.\n\n"},
22 }
23
24 func TestRoundTrip(t *testing.T) {
25 buf := new(bytes.Buffer)
26 enc := NewEncoder(buf, false)
27 dec := NewDecoder(buf)
28 for _, tt := range encoderTests {
29 want := tt.event
30 if err := enc.Encode(want); err != nil {
31 t.Fatal(err)
32 }
33 if buf.String() != tt.output {
34 t.Errorf("Expected: %s Got: %s", tt.output, buf.String())
35 }
36 ev, err := dec.Decode()
37 if err != nil {
38 t.Fatal(err)
39 }
40 if ev.Id() != want.Id() || ev.Event() != want.Event() || ev.Data() != want.Data() {
41 t.Errorf("Expected: %s %s %s Got: %s %s %s", want.Id(), want.Event(), want.Data(), ev.Id(), ev.Event(), ev.Data())
42 }
43 }
44 }
45
View as plain text