forked from antage/eventsource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventsource_test.go
187 lines (150 loc) · 4.66 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package eventsource
import (
"io"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
type testEnv struct {
eventSource EventSource
server *httptest.Server
}
func setup(t *testing.T) *testEnv {
t.Log("Setup testing environment")
e := new(testEnv)
e.eventSource = New(nil, nil)
e.server = httptest.NewServer(e.eventSource)
return e
}
func setupWithHeaders(t *testing.T, headers [][]byte) *testEnv {
t.Log("Setup testing environment")
e := new(testEnv)
e.eventSource = New(
nil,
func(*http.Request) [][]byte {
return headers
},
)
e.server = httptest.NewServer(e.eventSource)
return e
}
func teardown(t *testing.T, e *testEnv) {
t.Log("Teardown testing environment")
e.eventSource.Close()
e.server.Close()
}
func checkError(t *testing.T, e error) {
if e != nil {
t.Error(e)
}
}
func read(t *testing.T, c net.Conn) []byte {
resp := make([]byte, 1024)
_, err := c.Read(resp)
if err != nil && err != io.EOF {
t.Error(err)
}
return resp
}
func startEventStream(t *testing.T, e *testEnv) (net.Conn, []byte) {
url := e.server.URL
t.Log("open connection")
conn, err := net.Dial("tcp", strings.Replace(url, "http://", "", 1))
checkError(t, err)
t.Log("send GET request to the connection")
_, err = conn.Write([]byte("GET / HTTP/1.1\n\n"))
checkError(t, err)
resp := read(t, conn)
t.Logf("got response: \n%s", resp)
return conn, resp
}
func expectResponse(t *testing.T, c net.Conn, expecting string) {
time.Sleep(100 * time.Millisecond)
resp := read(t, c)
if !strings.Contains(string(resp), expecting) {
t.Errorf("expected:\n%s\ngot:\n%s\n", expecting, resp)
}
}
func TestConnection(t *testing.T) {
e := setup(t)
defer teardown(t, e)
conn, resp := startEventStream(t, e)
defer conn.Close()
if !strings.Contains(string(resp), "HTTP/1.1 200 OK\n") {
t.Error("the response has no HTTP status")
}
if !strings.Contains(string(resp), "Content-Type: text/event-stream\n") {
t.Error("the response has no Content-Type header with value 'text/event-stream'")
}
}
func TestConnectionWithCustomHeaders(t *testing.T) {
e := setupWithHeaders(t, [][]byte{
[]byte("X-Accel-Buffering: no"),
[]byte("Access-Control-Allow-Origin: *"),
})
defer teardown(t, e)
conn, resp := startEventStream(t, e)
defer conn.Close()
if !strings.Contains(string(resp), "HTTP/1.1 200 OK\n") {
t.Error("the response has no HTTP status")
}
if !strings.Contains(string(resp), "Content-Type: text/event-stream\n") {
t.Error("the response has no Content-Type header with value 'text/event-stream'")
}
if !strings.Contains(string(resp), "X-Accel-Buffering: no\n") {
t.Error("the response has no X-Accel-Buffering header with value 'no'")
}
if !strings.Contains(string(resp), "Access-Control-Allow-Origin: *\n") {
t.Error("the response has no Access-Control-Allow-Origin header with value '*'")
}
}
func TestMessageSending(t *testing.T) {
e := setup(t)
defer teardown(t, e)
conn, _ := startEventStream(t, e)
defer conn.Close()
t.Log("send message 'test'")
e.eventSource.SendMessage("test", "", "")
expectResponse(t, conn, "data: test\n\n")
t.Log("send message 'test' with id '1'")
e.eventSource.SendMessage("test", "", "1")
expectResponse(t, conn, "id: 1\ndata: test\n\n")
t.Log("send message 'test' with id '1\n1'")
e.eventSource.SendMessage("test", "", "1\n1")
expectResponse(t, conn, "id: 11\ndata: test\n\n")
t.Log("send message 'test' with event type 'notification'")
e.eventSource.SendMessage("test", "notification", "")
expectResponse(t, conn, "event: notification\ndata: test\n\n")
t.Log("send message 'test' with event type 'notification\n2'")
e.eventSource.SendMessage("test", "notification\n2", "")
expectResponse(t, conn, "event: notification2\ndata: test\n\n")
t.Log("send message 'test\ntest2\ntest3\n'")
e.eventSource.SendMessage("test\ntest2\ntest3\n", "", "")
expectResponse(t, conn, "data: test\ndata: test2\ndata: test3\ndata: \n\n")
}
func TestStalledMessages(t *testing.T) {
e := setup(t)
defer teardown(t, e)
conn, _ := startEventStream(t, e)
conn2, _ := startEventStream(t, e)
t.Log("send message 'test' to both connections")
e.eventSource.SendMessage("test", "", "")
expectResponse(t, conn, "data: test\n\n")
expectResponse(t, conn2, "data: test\n\n")
conn.Close()
conn2.Close()
t.Log("send message with no open connections")
e.eventSource.SendMessage("test", "", "1")
connNew, _ := startEventStream(t, e)
t.Log("send a message to new connection")
e.eventSource.SendMessage("test", "", "1\n1")
expectResponse(t, connNew, "id: 11\ndata: test\n\n")
for i := 0; i < 10; i++ {
t.Log("sending additional message ", i)
e.eventSource.SendMessage("test", "", "")
expectResponse(t, connNew, "data: test\n\n")
}
}