-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathmiddleware_test.go
47 lines (37 loc) · 973 Bytes
/
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
package feature_test
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/influxdata/influxdb/v2/kit/feature"
"go.uber.org/zap/zaptest"
)
func Test_Handler(t *testing.T) {
var (
w = &httptest.ResponseRecorder{}
r = httptest.NewRequest(http.MethodGet, "http://nowhere.test", new(bytes.Buffer)).
WithContext(context.Background())
original = r.Context()
)
handler := &checkHandler{t: t, f: func(t *testing.T, r *http.Request) {
if r.Context() == original {
t.Error("expected annotated context")
}
}}
subject := feature.NewHandler(zaptest.NewLogger(t), feature.DefaultFlagger(), feature.Flags(), handler)
subject.ServeHTTP(w, r)
if !handler.called {
t.Error("expected handler to be called")
}
}
type checkHandler struct {
t *testing.T
f func(t *testing.T, r *http.Request)
called bool
}
func (h *checkHandler) ServeHTTP(_ http.ResponseWriter, r *http.Request) {
h.called = true
h.f(h.t, r)
}