-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheventsource_test.go
154 lines (116 loc) · 3.41 KB
/
eventsource_test.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package eventsource
import (
"net"
"net/http"
"net/http/httptest"
"net/http/httputil"
"testing"
"time"
)
type CloseNotifiterRecorder struct {
*httptest.ResponseRecorder
}
func NewCloseNotifierRecorder() *CloseNotifiterRecorder {
recorder := httptest.NewRecorder()
myRecorder := new(CloseNotifiterRecorder)
myRecorder.ResponseRecorder = recorder
return myRecorder
}
func (r CloseNotifiterRecorder) CloseNotify() <-chan bool {
channel := make(chan bool)
return channel
}
func TestServeHttpWritesTheProperHeaders(t *testing.T) {
req, err := http.NewRequest("GET", "http://example.com/foo", nil)
if err != nil {
t.Errorf("Failed to create request")
}
recorder := NewCloseNotifierRecorder()
eventHandler := func(es *Conn) {}
httpHandler := Handler(eventHandler)
httpHandler.ServeHTTP(recorder, req)
if recorder.Header().Get("Content-Type") != "text/event-stream" {
t.Errorf("Content-Type header not set to text/event-stream")
}
if recorder.Header().Get("Cache-Control") != "no-cache" {
t.Errorf("Cache-Control header not set to no-cache")
}
if recorder.Header().Get("Connection") != "keep-alive" {
t.Errorf("Connection header not set to keep-alive")
}
if recorder.Header().Get("Transfer-Encoding") != "chunked" {
t.Errorf("Transfer-Encoding header not set to chunked")
}
}
func TestServeHttpSetsResponseToOK(t *testing.T) {
req, err := http.NewRequest("GET", "http://example.com/foo", nil)
if err != nil {
t.Errorf("Failed to create request")
}
recorder := NewCloseNotifierRecorder()
eventHandler := func(es *Conn) {}
httpHandler := Handler(eventHandler)
httpHandler.ServeHTTP(recorder, req)
if recorder.Code != 200 {
t.Errorf("Response was not a 200")
}
}
func TestServeHttpFlushes(t *testing.T) {
req, err := http.NewRequest("GET", "http://example.com/foo", nil)
if err != nil {
t.Errorf("Failed to create request")
}
recorder := NewCloseNotifierRecorder()
eventHandler := func(es *Conn) {}
httpHandler := Handler(eventHandler)
httpHandler.ServeHTTP(recorder, req)
if !recorder.Flushed {
t.Errorf("Not flushed")
}
}
func TestAllowsNotificationThatClientWentAway(t *testing.T) {
clientWentAway := make(chan bool)
eventHandler := func(es *Conn) {
clientWentAway <- <-es.CloseNotify()
}
server := httptest.NewServer(Handler(eventHandler))
defer server.Close()
tcpConn, err := net.Dial("tcp", server.Listener.Addr().String())
if err != nil {
t.Errorf("Error opening client connection: %s", err)
}
conn := httputil.NewClientConn(tcpConn, nil)
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Errorf("Error creating request: %s", err)
}
err = conn.Write(req)
if err != nil {
t.Errorf("Error writing request: %s", err)
}
err = conn.Close()
if err != nil {
t.Errorf("Could not close client connection: %s", err)
}
select {
case <-clientWentAway:
// test worked
case <-time.After(1 * time.Second):
t.Errorf("Never told that the client went away.")
}
}
func TestConnWritingAMessage(t *testing.T) {
recorder := httptest.NewRecorder()
connection := &Conn{recorder, recorder, NewCloseNotifierRecorder()}
connection.Write("Hello World")
expectedBody := "data: Hello World\n\n"
switch recorder.Body.String() {
case expectedBody:
// body is equal so no need to do anything
default:
t.Errorf("Body (%s) did not match expectation (%s).", recorder.Body.String(), expectedBody)
}
if !recorder.Flushed {
t.Errorf("Writer did not get flushed.")
}
}