-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathgroup_test.go
145 lines (116 loc) · 2.8 KB
/
group_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
package lars
import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/websocket"
. "gopkg.in/go-playground/assert.v1"
)
// NOTES:
// - Run "go test" to run tests
// - Run "gocov test | gocov report" to report on test converage by file
// - Run "gocov test | gocov annotate -" to report on all code and functions, those ,marked with "MISS" were never called
//
// or
//
// -- may be a good idea to change to output path to somewherelike /tmp
// go test -coverprofile cover.out && go tool cover -html=cover.out -o cover.html
//
func TestWebsockets(t *testing.T) {
origin := "http://localhost"
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
o := r.Header.Get(Origin)
return o == origin
},
}
l := New()
l.WebSocket(upgrader, "/ws", func(c Context) {
messageType, b, err := c.WebSocket().ReadMessage()
if err != nil {
return
}
if err == nil {
err := c.WebSocket().WriteMessage(messageType, b)
if err != nil {
panic(err)
}
}
})
server := httptest.NewServer(l.Serve())
defer server.Close()
addr := server.Listener.Addr().String()
header := make(http.Header, 0)
header.Set(Origin, origin)
url := fmt.Sprintf("ws://%s/ws", addr)
ws, _, err := websocket.DefaultDialer.Dial(url, header)
if err != nil {
log.Fatal("dial:", err)
}
Equal(t, err, nil)
defer ws.Close()
err = ws.WriteMessage(websocket.TextMessage, []byte("websockets in action!"))
Equal(t, err, nil)
typ, b, err := ws.ReadMessage()
Equal(t, err, nil)
Equal(t, typ, websocket.TextMessage)
Equal(t, "websockets in action!", string(b))
wsBad, res, err := websocket.DefaultDialer.Dial(url, nil)
NotEqual(t, err, nil)
Equal(t, wsBad, nil)
Equal(t, res.StatusCode, http.StatusForbidden)
}
func TestGrouplogic(t *testing.T) {
var aa, bb, cc, tl int
aM := func(c Context) {
aa++
c.Next()
}
bM := func(c Context) {
bb++
c.Next()
}
cM := func(c Context) {
cc++
c.Next()
}
l := New()
l.Use(func(c Context) {
tl++
c.Next()
})
a := l.GroupWithMore("/a", aM)
a.Get("/test", func(c Context) {
c.JSON(http.StatusOK, "a-ok")
})
b := a.GroupWithMore("/b", bM)
b.Get("/test", func(c Context) {
c.JSON(http.StatusOK, "b-ok")
})
c := b.GroupWithMore("/c", cM)
c.Get("/test", func(c Context) {
c.JSON(http.StatusOK, "c-ok")
})
code, body := request(GET, "/a/test", l)
Equal(t, code, http.StatusOK)
Equal(t, body, "\"a-ok\"")
Equal(t, tl, 1)
Equal(t, aa, 1)
code, body = request(GET, "/a/b/test", l)
Equal(t, code, http.StatusOK)
Equal(t, body, "\"b-ok\"")
Equal(t, tl, 2)
Equal(t, aa, 2)
Equal(t, bb, 1)
code, body = request(GET, "/a/b/c/test", l)
Equal(t, code, http.StatusOK)
Equal(t, body, "\"c-ok\"")
Equal(t, tl, 3)
Equal(t, aa, 3)
Equal(t, bb, 2)
Equal(t, cc, 1)
}