forked from arl/statsviz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers_test.go
181 lines (140 loc) · 3.89 KB
/
handlers_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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package statsviz
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
"github.com/gorilla/websocket"
)
func testIndex(t *testing.T, f http.Handler, url string) {
t.Helper()
req := httptest.NewRequest("GET", url, nil)
w := httptest.NewRecorder()
f.ServeHTTP(w, req)
resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
t.Errorf("http status %v, want %v", resp.StatusCode, http.StatusOK)
}
if resp.Header.Get("Content-Type") != "text/html; charset=utf-8" {
t.Errorf("header[Content-Type] %s, want %s", resp.Header.Get("Content-Type"), "text/html; charset=utf-8")
}
if !strings.Contains(string(body), "goroutines") {
t.Errorf("body doesn't contain %q", "goroutines")
}
}
func TestIndex(t *testing.T) {
t.Parallel()
testIndex(t, Index, "http://example.com/debug/statsviz/")
}
func TestIndexAtRoot(t *testing.T) {
t.Parallel()
testIndex(t, IndexAtRoot("/debug/"), "http://example.com/debug/")
testIndex(t, IndexAtRoot("/debug"), "http://example.com/debug/")
testIndex(t, IndexAtRoot("/"), "http://example.com/")
testIndex(t, IndexAtRoot("/test/"), "http://example.com/test/")
}
func testWs(t *testing.T, f http.Handler, URL string) {
t.Helper()
s := httptest.NewServer(f)
defer s.Close()
// Build a "ws://" url using the httptest server URL and the URL argument.
u1, err := url.Parse(s.URL)
if err != nil {
t.Fatal(err)
}
u2, err := url.Parse(URL)
if err != nil {
t.Fatal(err)
}
u1.Scheme = "ws"
u1.Path = u2.Path
// Connect to the server
ws, _, err := websocket.DefaultDialer.Dial(u1.String(), nil)
if err != nil {
t.Fatalf("%v", err)
}
defer ws.Close()
// Wait for 2 messages and check that the payload is what we expect.
for i := 0; i < 2; i++ {
_, p, err := ws.ReadMessage()
if err != nil {
t.Fatalf("%v", err)
}
var stats stats
if err := json.Unmarshal(p, &stats); err != nil {
t.Fatal(err)
}
}
}
func TestWs(t *testing.T) {
t.Parallel()
testWs(t, http.HandlerFunc(Ws), "http://example.com/debug/statsviz/ws")
}
func TestWsCantUpgrade(t *testing.T) {
url := "http://example.com/debug/statsviz/ws"
req := httptest.NewRequest("GET", url, nil)
w := httptest.NewRecorder()
Ws(w, req)
if w.Result().StatusCode != http.StatusBadRequest {
t.Errorf("responded %v to %q with non-websocket-upgradable conn, want %v", w.Result().StatusCode, url, http.StatusBadRequest)
}
}
func testRegister(t *testing.T, f http.Handler, baseURL string) {
testIndex(t, f, baseURL)
ws := strings.TrimRight(baseURL, "/") + "/ws"
testWs(t, f, ws)
}
func TestRegister(t *testing.T) {
t.Run("default", func(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
if err := Register(mux); err != nil {
t.Fatal(err)
}
testRegister(t, mux, "http://example.com/debug/statsviz/")
})
t.Run("root", func(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
if err := Register(mux, Root("")); err != nil {
t.Fatal(err)
}
testRegister(t, mux, "http://example.com/")
})
t.Run("root2", func(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
if err := Register(mux, Root("/root/to/statsviz")); err != nil {
t.Fatal(err)
}
testRegister(t, mux, "http://example.com/root/to/statsviz/")
})
t.Run("root+frequency", func(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
err := Register(mux, Root("/root/to/statsviz"), SendFrequency(100*time.Millisecond))
if err != nil {
t.Fatal(err)
}
testRegister(t, mux, "http://example.com/root/to/statsviz/")
})
t.Run("non-positive frequency", func(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
err := Register(mux, Root("/root/to/statsviz"), SendFrequency(0))
if err == nil {
t.Fatal(err)
}
})
}
func TestRegisterDefault(t *testing.T) {
if err := RegisterDefault(); err != nil {
t.Fatal(err)
}
testRegister(t, http.DefaultServeMux, "http://example.com/debug/statsviz/")
}