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
/
Copy pathtesting.go
79 lines (70 loc) · 1.92 KB
/
testing.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
package signalr
import (
"net/http"
"strings"
"github.com/gorilla/websocket"
)
// TestCompleteHandler combines the negotiate, connect, reconnect, and start
// handlers found in this package into one complete response handler.
func TestCompleteHandler(w http.ResponseWriter, r *http.Request) {
switch {
case strings.Contains(r.URL.Path, "/negotiate"):
TestNegotiate(w, r)
case strings.Contains(r.URL.Path, "/connect"):
TestConnect(w, r)
case strings.Contains(r.URL.Path, "/reconnect"):
TestReconnect(w, r)
case strings.Contains(r.URL.Path, "/start"):
TestStart(w, r)
}
}
// TestNegotiate provides a sample "/negotiate" handling function.
//
// If an error occurs while writing the response data, it will panic.
func TestNegotiate(w http.ResponseWriter, r *http.Request) {
// nolint:lll
_, err := w.Write([]byte(`{"ConnectionToken":"hello world","ConnectionId":"1234-ABC","URL":"/signalr","ProtocolVersion":"1337"}`))
if err != nil {
panic(err)
}
}
// TestConnect provides a sample "/connect" handling function.
//
// If an error occurs while upgrading the websocket, it will panic.
func TestConnect(w http.ResponseWriter, r *http.Request) {
upgrader := websocket.Upgrader{}
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
panic(err)
}
go func() {
for {
_, _, rerr := c.ReadMessage()
if rerr != nil {
return
}
}
}()
go func() {
for {
werr := c.WriteMessage(websocket.TextMessage, []byte(`{"S":1}`))
if werr != nil {
return
}
}
}()
}
// TestReconnect provides a sample "/reconnect" handling function. It simply
// calls TestConnect.
func TestReconnect(w http.ResponseWriter, r *http.Request) {
TestConnect(w, r)
}
// TestStart provides a sample "/start" handling function.
//
// If an error occurs while writing the response data, it will panic.
func TestStart(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(`{"Response":"started"}`))
if err != nil {
panic(err)
}
}