This repository has been archived by the owner on Sep 4, 2018. It is now read-only.
forked from yml/botbot-eventsource
-
Notifications
You must be signed in to change notification settings - Fork 9
/
main_test.go
91 lines (82 loc) · 2.07 KB
/
main_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
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
)
type closeNotifyingRecorder struct {
*httptest.ResponseRecorder
closed chan bool
}
func newCloseNotifyingRecorder() *closeNotifyingRecorder {
return &closeNotifyingRecorder{
httptest.NewRecorder(),
make(chan bool, 1),
}
}
func (c *closeNotifyingRecorder) close() {
c.closed <- true
}
func (c *closeNotifyingRecorder) CloseNotify() <-chan bool {
return c.closed
}
func TestEventSourceHandler(t *testing.T) {
h := NewHub()
setup := func() {
err := h.client.Set("existing-token", []byte("channelName"))
if err != nil {
t.Error("[Error] An error occured while setting up the test", err)
}
}
tearDown := func() {
_, err := h.client.Del("existing-token")
if err != nil {
t.Error("[Error] An error occured while tearing down the test", err)
}
_, err = h.client.Del("bad-token")
if err != nil {
t.Error("[Error] An error occured while tearing down the test", err)
}
}
tests := []struct {
Desc string
Handler func(http.ResponseWriter, *http.Request)
Path string
Status int
}{
{
Desc: "Access the EventSourceHandler with a bad-token",
Handler: h.EventSourceHandler,
Path: ssePath + "bad-token",
Status: http.StatusUnauthorized,
},
{
Desc: "Access the EventSourceHandler with an existing-token",
Handler: h.EventSourceHandler,
Path: ssePath + "existing-token",
Status: http.StatusOK,
},
}
for _, test := range tests {
fmt.Println("[Testing]", test.Desc)
setup()
defer tearDown()
record := newCloseNotifyingRecorder()
req := &http.Request{
Method: "GET",
URL: &url.URL{Path: test.Path},
}
go test.Handler(record, req)
// walk around a timing issue
// When we connect to the eventsrouce endpoint the connection is hold open
// so we need to start the handler in a goroutine else the test is blocking
time.Sleep(time.Second * 1)
record.CloseNotify()
if got, want := record.Code, test.Status; got != want {
t.Errorf("%s: response code = %d, want %d", test.Desc, got, want)
}
}
}