forked from bernerdschaefer/eventsource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_test.go
123 lines (98 loc) · 2.62 KB
/
handler_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
package eventsource
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
)
var emptyHandler Handler = func(string, *Encoder, <-chan bool) {}
type testCloseNotifier struct {
closed chan bool
http.ResponseWriter
}
func (n testCloseNotifier) Close() {
n.closed <- true
}
func (n testCloseNotifier) CloseNotify() <-chan bool {
return n.closed
}
func TestHandlerAcceptable(t *testing.T) {
t.Parallel()
table := []struct {
accept string
result bool
}{
{"", true},
{"text/event-stream", true},
{"text/*", true},
{"*/*", true},
{"text/event-stream; q=1.0", true},
{"text/*; q=1.0", true},
{"*/*; q=1.0", true},
{"text/html; q=1.0, text/*; q=0.8", true},
{"text/html; q=1.0, image/gif; q=0.6, image/jpeg; q=0.6", false},
}
for i, tt := range table {
if exp, got := tt.result, emptyHandler.acceptable(tt.accept); exp != got {
t.Errorf("%d. expected acceptable(%q) == %t, got %t", i, tt.accept, exp, got)
}
}
}
func TestHandlerValidatesAcceptHeader(t *testing.T) {
t.Parallel()
w, r := httptest.NewRecorder(), &http.Request{Header: map[string][]string{
"Accept": {"text/html"},
}}
emptyHandler.ServeHTTP(w, r)
if w.Code != http.StatusNotAcceptable {
t.Fatal("handler did not set 406 status")
}
}
func TestHandlerSetsContentType(t *testing.T) {
t.Parallel()
w, r := httptest.NewRecorder(), &http.Request{Header: map[string][]string{
"Accept": {"text/event-stream"},
}}
emptyHandler.ServeHTTP(w, r)
if w.Result().Header.Get("Content-Type") != "text/event-stream" {
t.Fatal("handler did not set appropriate content type")
}
if w.Code != http.StatusOK {
t.Fatal("handler did not set 200 status")
}
}
func TestHandlerEncode(t *testing.T) {
t.Parallel()
handler := func(_ string, enc *Encoder, _ <-chan bool) {
enc.Encode(Event{Data: []byte("hello")})
}
w, r := httptest.NewRecorder(), &http.Request{Header: map[string][]string{
"Accept": {"text/event-stream"},
}}
Handler(handler).ServeHTTP(w, r)
var event Event
NewDecoder(w.Body).Decode(&event)
if !reflect.DeepEqual(event, Event{Type: "message", Data: []byte("hello")}) {
t.Error("unexpected handler output")
}
}
func TestHandlerCloseNotify(t *testing.T) {
t.Parallel()
done := make(chan bool, 1)
handler := func(_ string, _ *Encoder, stop <-chan bool) {
<-stop
done <- true
}
w, r := httptest.NewRecorder(), &http.Request{Header: map[string][]string{
"Accept": {"text/event-stream"},
}}
closer := testCloseNotifier{make(chan bool, 1), w}
go Handler(handler).ServeHTTP(closer, r)
closer.Close()
select {
case <-done:
case <-time.After(time.Second):
t.Error("handler was not notified of close")
}
}