-
Notifications
You must be signed in to change notification settings - Fork 8
/
apollo_test.go
87 lines (71 loc) · 1.97 KB
/
apollo_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
// Apollo provides `net/context`-aware middleware chaining
package apollo
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"context"
"github.com/stretchr/testify/assert"
)
func TestHandlerFunc(t *testing.T) {
assert := assert.New(t)
assert.NotPanics(func() {
ctx := context.Background()
r, _ := http.NewRequest("GET", "http://github.com/", nil)
w := httptest.NewRecorder()
handler := HandlerFunc(handlerOne)
assert.Implements((*Handler)(nil), handler)
handler.ServeHTTP(ctx, w, r)
assert.Equal(w.Code, 200)
assert.Equal(w.Body.String(), "h1\n")
})
}
func TestAddsContextServe(t *testing.T) {
assert := assert.New(t)
adapter := addsContext{
ctx: context.Background(),
handler: HandlerFunc(handlerOne),
}
assert.NotPanics(func() {
r, _ := http.NewRequest("GET", "http://github.com/", nil)
w := httptest.NewRecorder()
adapter.ServeHTTP(w, r)
assert.Equal(w.Code, 200)
assert.Equal(w.Body.String(), "h1\n")
})
}
func TestStripsContextServe(t *testing.T) {
assert := assert.New(t)
adapter := stripsContext{http.HandlerFunc(handlerZero)}
assert.NotPanics(func() {
ctx := context.Background()
r, _ := http.NewRequest("GET", "http://github.com/", nil)
w := httptest.NewRecorder()
adapter.ServeHTTP(ctx, w, r)
assert.Equal(w.Code, 200)
assert.Equal(w.Body.String(), "h0\n")
})
}
func TestWrap(t *testing.T) {
assert := assert.New(t)
assert.NotPanics(func() {
con := Wrap(middleZero)
assert.IsType(con, *new(Constructor))
})
}
func TestWrapChains(t *testing.T) {
assert := assert.New(t)
ctx := NewTestContext(context.Background(), 10)
value, _ := FromContext(ctx)
assert.Equal(value, 10)
chain := New(middleOne, Wrap(middleZero), middleTwo).With(ctx).ThenFunc(handlerContext)
ts := httptest.NewServer(chain)
defer ts.Close()
res, err := http.Get(ts.URL)
assert.NoError(err)
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
assert.Equal(200, res.StatusCode)
assert.Equal("m1\nm0\nm2\n10\n", string(body))
}