-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp-handler-with-stats_test.go
69 lines (52 loc) · 2.18 KB
/
http-handler-with-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
package tools
import (
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"strconv"
"testing"
)
func checkMetricsCalled(t *testing.T, statsd *MockStatsD, routeName string, response int, statusCode string) {
call := statsd.Calls
expectedTags := []string{"route:" + routeName, "response:" + strconv.Itoa(response)}
assert.Len(t, call, 3)
assert.Equal(t, "Histogram", call[0].Method)
assert.Equal(t, expectedTags, call[0].Args.Tags)
assert.Equal(t, "Incr", call[1].Method)
assert.Equal(t, fmt.Sprintf("web.response_code.%s", statusCode), call[1].Args.Name)
assert.Equal(t, expectedTags, call[1].Args.Tags)
assert.Equal(t, "Incr", call[2].Method)
assert.Equal(t, "web.response_code.all", call[2].Args.Name)
assert.Equal(t, expectedTags, call[2].Args.Tags)
}
type MockHandler struct {
response int
}
func (h MockHandler) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(h.response)
}
func TestHTTPHandlerWithStats(t *testing.T) {
statsd := &MockStatsD{}
logger := &MockLogger{}
router := &MockHandler{response: http.StatusOK}
httpHandler := HTTPHandlerWithStats("route", router, logger, statsd)
httpHandler.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("GET", "http://example.com", nil))
lastLogCall := logger.LastCall()
assert.NotNil(t, lastLogCall, "Expected call to be made")
assert.Equal(t, "Debug", lastLogCall.Method)
assert.Equal(t, "Request to http://example.com had response code 200 in 0ms", lastLogCall.Args.Msg)
checkMetricsCalled(t, statsd, "route", http.StatusOK, "200")
}
func TestHTTPHandlerWithStats_Error(t *testing.T) {
statsd := &MockStatsD{}
logger := &MockLogger{}
router := &MockHandler{response: http.StatusInternalServerError}
httpHandler := HTTPHandlerWithStats("route", router, logger, statsd)
httpHandler.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("GET", "http://example.com", nil))
lastLogCall := logger.LastCall()
assert.NotNil(t, lastLogCall, "Expected call to be made")
assert.Equal(t, "Debug", lastLogCall.Method)
assert.Equal(t, "Request to http://example.com had response code 500 in 0ms", lastLogCall.Args.Msg)
checkMetricsCalled(t, statsd, "route", http.StatusInternalServerError, "500")
}