This repository has been archived by the owner on Aug 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
testing_test.go
161 lines (149 loc) · 3.79 KB
/
testing_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
package signalr_test
import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/carterjones/signalr"
"github.com/gorilla/websocket"
)
// This is some testception right here...
type writeFailer struct {
http.ResponseWriter
err string
}
func (w writeFailer) Write(p []byte) (int, error) {
return 0, errors.New(w.err)
}
func catchErr(f http.HandlerFunc, w http.ResponseWriter, r *http.Request) (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
f(w, r)
return nil
}
func TestTestCompleteHandler(t *testing.T) {
cases := map[string]struct {
path string
header http.Header
isWebsocketCall bool
customWriter http.ResponseWriter
customOrigin string
exp string
wantErr string
}{
"negotiate": {
path: "/negotiate",
exp: `{"ConnectionToken":"hello world","ConnectionId":"1234-ABC","URL":"/signalr","ProtocolVersion":"1337"}`,
},
"negotiate failure": {
path: "/negotiate",
customWriter: writeFailer{err: "sample negotiate error"},
wantErr: "sample negotiate error",
},
"connect": {
path: "/connect",
header: http.Header{
"Upgrade": []string{"websocket"},
"Connection": []string{"upgrade"},
"Sec-Websocket-Version": []string{"13"},
"Sec-Websocket-Key": []string{"blablabla"},
},
isWebsocketCall: true,
exp: `{"S":1}`,
},
"connect failure": {
path: "/connect",
header: http.Header{
"Upgrade": []string{"websocket"},
"Connection": []string{"upgrade"},
"Sec-Websocket-Version": []string{"13"},
"Sec-Websocket-Key": []string{"blablabla"},
},
customOrigin: "blabla",
isWebsocketCall: true,
wantErr: "websocket: request origin not allowed by Upgrader.CheckOrigin",
},
"reconnect": {
path: "/reconnect",
header: http.Header{
"Upgrade": []string{"websocket"},
"Connection": []string{"upgrade"},
"Sec-Websocket-Version": []string{"13"},
"Sec-Websocket-Key": []string{"blablabla"},
},
isWebsocketCall: true,
exp: `{"S":1}`,
},
"start": {
path: "/start",
exp: `{"Response":"started"}`,
},
"start failure": {
path: "/start",
customWriter: writeFailer{err: "sample start error"},
wantErr: "sample start error",
},
}
for id, tc := range cases {
tc := tc
recorder := httptest.NewRecorder()
var customErr error
ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case tc.customOrigin != "":
r.Header.Set("Origin", tc.customOrigin)
customErr = catchErr(signalr.TestCompleteHandler, w, r)
case tc.customWriter != nil:
customErr = catchErr(signalr.TestCompleteHandler, tc.customWriter, r)
case !tc.isWebsocketCall:
signalr.TestCompleteHandler(recorder, r)
default:
signalr.TestCompleteHandler(w, r)
}
}))
ts.Start()
c := ts.Client()
u := ts.URL + tc.path
var err error
var act string
if tc.isWebsocketCall {
var conn *websocket.Conn
var p []byte
u = strings.Replace(u, "http://", "ws://", -1)
conn, _, err = websocket.DefaultDialer.Dial(u, nil)
if customErr == nil {
if err != nil {
panic(err)
}
_, p, err = conn.ReadMessage()
act = string(p)
}
} else {
var req *http.Request
req, err = http.NewRequest("GET", u, nil)
if err != nil {
panic(err)
}
req.Header = tc.header
_, err = c.Do(req)
if err != nil {
panic(err)
}
act = recorder.Body.String()
}
if tc.wantErr != "" {
if customErr != nil {
errMatches(t, id, customErr, tc.wantErr)
} else {
errMatches(t, id, err, tc.wantErr)
}
} else {
equals(t, id, tc.exp, act)
ok(t, id, err)
}
}
}