-
Notifications
You must be signed in to change notification settings - Fork 8
/
fixtures_test.go
59 lines (47 loc) · 1.3 KB
/
fixtures_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
// Apollo provides `net/context`-aware middleware chaining
package apollo
import (
"net/http"
"strconv"
"context"
)
func handlerZero(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("h0\n"))
}
func handlerOne(ctx context.Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte("h1\n"))
}
func handlerContext(ctx context.Context, w http.ResponseWriter, r *http.Request) {
if value, ok := FromContext(ctx); ok {
contents := strconv.Itoa(value) + "\n"
w.Write([]byte(contents))
}
}
func middleZero(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("m0\n"))
h.ServeHTTP(w, r)
})
}
func middleOne(h Handler) Handler {
return HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte("m1\n"))
h.ServeHTTP(ctx, w, r)
})
}
func middleTwo(h Handler) Handler {
return HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte("m2\n"))
h.ServeHTTP(ctx, w, r)
})
}
// TestContext
type key int
const testKey key = 0
func NewTestContext(ctx context.Context, dummy int) context.Context {
return context.WithValue(ctx, testKey, dummy)
}
func FromContext(ctx context.Context) (int, bool) {
dummy, ok := ctx.Value(testKey).(int)
return dummy, ok
}