-
Notifications
You must be signed in to change notification settings - Fork 53
/
stats_test.go
156 lines (108 loc) · 2.98 KB
/
stats_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
package stats
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"golang.org/x/net/websocket"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var testHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("bar"))
})
func TestSimple(t *testing.T) {
s := New()
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "http://example.com/foo", nil)
s.Handler(testHandler).ServeHTTP(res, req)
assert.Equal(t, 200, res.Code)
assert.Equal(t, map[string]int{"200": 1}, s.ResponseCounts)
}
func TestGetStats(t *testing.T) {
s := New()
var stats = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
stats := s.Data()
b, _ := json.Marshal(stats)
w.Write(b)
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "http://example.com/foo", nil)
s.Handler(testHandler).ServeHTTP(res, req)
res = httptest.NewRecorder()
s.Handler(stats).ServeHTTP(res, req)
assert.Equal(t, "application/json", res.Header().Get("Content-Type"))
var data map[string]interface{}
err := json.Unmarshal(res.Body.Bytes(), &data)
assert.Nil(t, err)
assert.Equal(t, float64(1), data["total_count"].(float64))
}
func TestRace(t *testing.T) {
s := New()
ch1 := make(chan bool)
ch2 := make(chan bool)
go func() {
now := time.Now()
for true {
select {
case _ = <-ch1:
return
default:
s.End(now, WithStatusCode(200))
}
}
}()
go func() {
dt := s.Data()
for true {
select {
case _ = <-ch2:
return
default:
_ = dt.TotalStatusCodeCount["200"]
}
}
}()
time.Sleep(time.Second)
ch1 <- true
ch2 <- true
}
func TestWebsocketIgnore(t *testing.T) {
s := New()
handler := websocket.Handler(func(conn *websocket.Conn) {
conn.Write([]byte("TEST"))
})
srv := httptest.NewServer(s.Handler(handler))
url := strings.Replace(srv.URL, "http", "ws", 1)
ws, err := websocket.Dial(url, "", srv.URL)
require.NoError(t, err)
bytes := make([]byte, 255)
n, err := ws.Read(bytes)
require.NoError(t, err)
assert.Equal(t, "TEST", string(bytes[:n]))
assert.Equal(t, map[string]int{}, s.ResponseCounts)
}
func TestWebsocketErrorNotIgnore(t *testing.T) {
s := New()
srv := httptest.NewServer(s.Handler(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusInternalServerError)
})))
url := strings.Replace(srv.URL, "http", "ws", 1)
_, err := websocket.Dial(url, "", srv.URL)
require.Error(t, err)
assert.Equal(t, map[string]int{"500": 1}, s.ResponseCounts)
}
func TestIgnoreHijackedConnection(t *testing.T) {
s := New()
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "http://example.com/foo", nil)
s.Handler(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.(http.Hijacker).Hijack()
})).ServeHTTP(res, req)
assert.Equal(t, map[string]int{}, s.ResponseCounts)
}