-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathctxflag_test.go
130 lines (121 loc) · 3.52 KB
/
ctxflag_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
package web
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/signalfx/golib/v3/log"
. "github.com/smartystreets/goconvey/convey"
)
type handleCall struct {
ctx context.Context
rw http.ResponseWriter
r *http.Request
}
type handleStack []handleCall
func (h *handleStack) ServeHTTPC(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
*h = append(*h, handleCall{ctx, rw, r})
}
func TestHeadersInReq(t *testing.T) {
Convey("When setup,", t, func() {
h := HeadersInRequest{
Headers: map[string]string{
"X-Hello": "world",
},
}
ctx := context.Background()
r := &Recorder{
Queue: make(chan Request, 10),
}
chain := NewHandler(ctx, r.AsHandler()).Add(&h)
rw := httptest.NewRecorder()
chain.ServeHTTPC(ctx, rw, nil)
So(rw.Header().Get("X-Hello"), ShouldEqual, "world")
})
}
func TestCtxWithFlag(t *testing.T) {
Convey("When setup,", t, func() {
d := log.CtxDimensions{}
c := CtxWithFlag{
CtxFlagger: &d,
HeaderName: "X-Test",
}
ctx := context.Background()
r := &Recorder{
Queue: make(chan Request, 10),
}
chain := NewHandler(ctx, r.AsHandler()).Add(&c)
rw := httptest.NewRecorder()
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, "", nil)
So(err, ShouldBeNil)
chain.ServeHTTPC(ctx, rw, req)
So(rw.Header().Get("X-Test"), ShouldNotEqual, "")
})
}
func TestRecorder(t *testing.T) {
Convey("When setup,", t, func() {
r := &Recorder{
Queue: make(chan Request, 10),
}
Convey("Should add with ServeHTTP,", func() {
r.ServeHTTP(nil, nil)
So(len(r.Queue), ShouldEqual, 1)
})
Convey("Should call next,", func() {
i := 0
c := HandlerFunc(func(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
i++
})
r.ServeHTTPC(context.Background(), nil, nil, c)
So(len(r.Queue), ShouldEqual, 1)
So(i, ShouldEqual, 1)
})
})
}
func TestHeaderCtxFlag(t *testing.T) {
Convey("When setup,", t, func() {
h := HeaderCtxFlag{}
hs := handleStack([]handleCall{})
ctx := context.Background()
Convey("should not setup by default,", func() {
req, err := http.NewRequestWithContext(context.Background(), "", "", nil)
So(err, ShouldBeNil)
rw := httptest.NewRecorder()
h.CreateMiddleware(&hs).ServeHTTPC(ctx, rw, req)
So(len(hs), ShouldEqual, 1)
So(h.HasFlag(hs[0].ctx), ShouldBeFalse)
})
Convey("With headername set,", func() {
h.HeaderName = "X-Debug"
Convey("And flag set,", func() {
h.SetFlagStr("enabled")
Convey("should fail when not set correctly,", func() {
req, err := http.NewRequestWithContext(context.Background(), "", "", nil)
So(err, ShouldBeNil)
req.Header.Add(h.HeaderName, "not-enabled")
rw := httptest.NewRecorder()
h.ServeHTTPC(ctx, rw, req, &hs)
So(len(hs), ShouldEqual, 1)
So(h.HasFlag(hs[0].ctx), ShouldBeFalse)
})
Convey("should check headers,", func() {
req, err := http.NewRequestWithContext(context.Background(), "", "", nil)
So(err, ShouldBeNil)
req.Header.Add(h.HeaderName, "enabled")
rw := httptest.NewRecorder()
h.ServeHTTPC(ctx, rw, req, &hs)
So(len(hs), ShouldEqual, 1)
So(h.HasFlag(hs[0].ctx), ShouldBeTrue)
})
Convey("should check query params,", func() {
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "http://localhost?X-Debug=enabled", nil)
So(err, ShouldBeNil)
rw := httptest.NewRecorder()
h.ServeHTTPC(ctx, rw, req, &hs)
So(len(hs), ShouldEqual, 1)
So(h.HasFlag(hs[0].ctx), ShouldBeTrue)
})
})
})
})
}