|
| 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