forked from spkg/httpctx
/
httpctx_test.go
61 lines (51 loc) · 1.37 KB
/
httpctx_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
package httpctx_test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/spkg/httpctx"
"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
)
func Test1(t *testing.T) {
assert := assert.New(t)
stack := httpctx.Use(middleware1, middleware2)
assert.NotNil(stack)
http.Handle("/api/whatever", stack.Handle(doWhateverHandler))
}
var doWhateverHandler = httpctx.HandlerFunc(doWhatever)
func doWhatever(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
return nil
}
func middleware1(f httpctx.Handler) httpctx.Handler {
return httpctx.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
return nil
})
}
func middleware2(f httpctx.Handler) httpctx.Handler {
return httpctx.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
return nil
})
}
func TestNewContext(t *testing.T) {
httpctx.NewContext(nil, nil, nil)
}
func TestHandler(t *testing.T) {
emptyFunc := func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
return nil
}
for i, tc := range []struct {
http.Handler
}{
{httpctx.HandleFunc(emptyFunc)},
{httpctx.Use(middleware1).Use(middleware2).HandleFunc(emptyFunc)},
} {
srv := httptest.NewServer(tc.Handler)
resp, err := http.Get(srv.URL)
srv.Close()
resp.Body.Close()
if err != nil {
t.Errorf("%d. %v", i, err)
}
}
}