-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
executable file
·72 lines (58 loc) · 2.03 KB
/
context_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
package scaffold
import (
"net/http"
"net/http/httptest"
"testing"
. "github.com/smartystreets/goconvey/convey"
"golang.org/x/net/context"
)
func TestContext(t *testing.T) {
Convey("Given wrapped and unwapped middlware functions", t, func() {
vars := map[int]string{0: "param1", 2: "param2"}
m1 := Middleware(func(next Handler) Handler {
return HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
p1 := GetParam(ctx, "param1")
p2 := GetParam(ctx, "param2")
So(p1, ShouldEqual, "value1")
So(p2, ShouldEqual, "value2")
next.CtxServeHTTP(ctx, w, r)
})
})
m1 = wrapMiddleware(vars, m1)
m2 := Middleware(func(next Handler) Handler {
return HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
p1 := GetParam(ctx, "param1")
p2 := GetParam(ctx, "param2")
So(p1, ShouldBeEmpty)
So(p2, ShouldBeEmpty)
next.CtxServeHTTP(ctx, w, r)
})
})
Convey("When the handler is called", func() {
handler := Handler(NewMockHandler())
handler = m2(m1(m2(handler)))
req, err := http.NewRequest("GET", "http://example.com/value1/and/value2/end", nil)
So(err, ShouldBeNil)
Convey("Then the params should be in only the wrapped middleware context", func() {
handler.CtxServeHTTP(context.Background(), httptest.NewRecorder(), req)
})
})
})
Convey("Given a wrapped handler", t, func() {
vars := map[int]string{0: "param1", 2: "param2"}
handler := Handler(HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
p1 := GetParam(ctx, "param1")
p2 := GetParam(ctx, "param2")
So(p1, ShouldEqual, "value1")
So(p2, ShouldEqual, "value2")
}))
handler = wrapHandler(vars, handler)
Convey("When the handler is called", func() {
req, err := http.NewRequest("GET", "http://example.com/value1/and/value2/end", nil)
So(err, ShouldBeNil)
Convey("Then the params should be in the handler context", func() {
handler.CtxServeHTTP(context.Background(), httptest.NewRecorder(), req)
})
})
})
}