-
Notifications
You must be signed in to change notification settings - Fork 2
/
middleware_test.go
112 lines (90 loc) · 2.76 KB
/
middleware_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
package negronicloudwatch
import (
"net/http"
"net/http/httptest"
"testing"
"time"
cw "github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/codegangsta/negroni"
"github.com/gorilla/context"
"github.com/stretchr/testify/assert"
)
var (
nowTime = time.Now()
nowToday = nowTime.Format("2006-01-02")
)
type testClock struct{}
func (tc *testClock) Now() time.Time {
return nowTime
}
func (tc *testClock) Since(time.Time) time.Duration {
return 10 * time.Microsecond
}
func TestNewMiddleware_Name(t *testing.T) {
mw := New("us-east-1", "test")
assert.Equal(t, "test", mw.Namespace)
}
func TestNewMiddleware_LatencyMetricName(t *testing.T) {
mw := New("us-east-1", "test")
assert.Equal(t, "Latency", mw.LatencyMetricName)
}
func TestNewMiddleware_Service(t *testing.T) {
mw := New("us-east-1", "test")
assert.NotEqual(t, nil, mw.Service)
}
func setupServeHTTP(t *testing.T) (*Middleware, negroni.ResponseWriter, *http.Request) {
req, err := http.NewRequest("GET", "http://example.com/stuff?rly=ya", nil)
assert.Nil(t, err)
req.RequestURI = "http://example.com/stuff?rly=ya"
req.Method = "GET"
req.Header.Set("X-Request-Id", "22035D08-98EF-413C-BBA0-C4E66A11B28D")
req.Header.Set("X-Real-IP", "10.10.10.10")
mw := New("us-east-1", "test")
mw.clock = &testClock{}
if err := mw.ExcludeURL("/ping"); err != nil {
t.Fatalf("Can't exclude URL %q: %q", "/ping", err)
}
return mw, negroni.NewResponseWriter(httptest.NewRecorder()), req
}
func TestMiddleware_ServeHTTP(t *testing.T) {
mw, rec, req := setupServeHTTP(t)
mw.PutMetric = func(data []*cw.MetricDatum) {
assert.Len(t, data, 1)
assert.Len(t, data[0].Dimensions, 2)
assert.Equal(t, "10.10.10.10", *data[0].Dimensions[1].Value)
assert.Equal(t, "RemoteAddr", *data[0].Dimensions[1].Name)
assert.Equal(t, "Latency", *data[0].MetricName)
assert.Equal(t, "Microseconds", *data[0].Unit)
}
mw.ServeHTTP(rec, req, func(w http.ResponseWriter, r *http.Request) {
val := context.Get(req, PutMetric)
assert.NotEqual(t, nil, val)
w.WriteHeader(418)
})
}
func TestServeHTTPWithURLExcluded(t *testing.T) {
mw, rec, req := setupServeHTTP(t)
if err := mw.ExcludeURL(req.URL.Path); err != nil {
t.Fatalf("Can't exclude URL %q: %q", "req.URL.Path", err)
}
called := false
mw.PutMetric = func(data []*cw.MetricDatum) {
called = true
}
mw.ServeHTTP(rec, req, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(418)
})
assert.Equal(t, false, called)
}
func TestRealClock_Now(t *testing.T) {
rc := &realClock{}
tf := "2006-01-02T15:04:05"
assert.Equal(t, rc.Now().Format(tf), time.Now().Format(tf))
}
func TestRealClock_Since(t *testing.T) {
rc := &realClock{}
now := rc.Now()
time.Sleep(10 * time.Millisecond)
since := rc.Since(now)
assert.Regexp(t, "^1[0-5]\\.[0-9]+ms", since.String())
}