forked from thoas/stats
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstats_test.go
More file actions
62 lines (40 loc) · 1.23 KB
/
Copy pathstats_test.go
File metadata and controls
62 lines (40 loc) · 1.23 KB
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
package stats
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
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, res.Code, 200)
assert.Equal(t, s.ResponseCounts, map[string]int{"200": 1})
}
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().Add("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, res.Header().Get("Content-Type"), "application/json")
var data map[string]interface{}
err := json.Unmarshal(res.Body.Bytes(), &data)
assert.Nil(t, err)
assert.Equal(t, data["total_count"].(float64), float64(1))
}