-
Notifications
You must be signed in to change notification settings - Fork 0
/
midsimple_test.go
128 lines (108 loc) · 3.75 KB
/
midsimple_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
package midsimple
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
type testMiddleware struct {
result string
next http.Handler
}
func (m *testMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(m.result))
m.next.ServeHTTP(w, r)
}
type testHandler struct {
result string
}
func (h *testHandler) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(h.result))
}
func newTestMiddleware(result string) func(h http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return &testMiddleware{
result: result,
next: h,
}
}
}
func TestMidSimple_New(t *testing.T) {
ms := New(newTestMiddleware("1"), newTestMiddleware("2"))
resp := httptest.NewRecorder()
ms.Wrap(&testHandler{result: "handler"}).ServeHTTP(resp, nil)
assert.Equal(t, []byte("12handler"), resp.Body.Bytes())
}
func TestMidSimple_UseReturnsMidSimple(t *testing.T) {
ms := New()
returnedMs := ms.Use(newTestMiddleware("1"))
assert.Equal(t, ms, returnedMs)
}
func TestMidSimple_Reset(t *testing.T) {
ms := New()
ms.Use(newTestMiddleware("1"))
ms.Reset().
Use(newTestMiddleware("2"))
resp := httptest.NewRecorder()
ms.Wrap(&testHandler{result: "handler"}).ServeHTTP(resp, nil)
assert.Equal(t, []byte("2handler"), resp.Body.Bytes())
}
func TestHandlerFuncWrapper(t *testing.T) {
var success bool
h := handlerFuncWrapper{hfunc: func(http.ResponseWriter, *http.Request) { success = true }}
h.ServeHTTP(nil, nil)
assert.True(t, success)
}
func TestMidSimple_WrapHandlerRegular(t *testing.T) {
ms := New()
ms.Use(newTestMiddleware("1"))
ms.Use(newTestMiddleware("2"), newTestMiddleware("3"), newTestMiddleware("4")).
Use(newTestMiddleware("5"))
ms.Use(newTestMiddleware("6"))
resp := httptest.NewRecorder()
ms.Wrap(&testHandler{result: "handler"}).ServeHTTP(resp, nil)
assert.Equal(t, []byte("123456handler"), resp.Body.Bytes())
}
func TestMidSimple_WrapHandlerRevert(t *testing.T) {
ms := New()
ms.Use(newTestMiddleware("1"))
ms.Use(newTestMiddleware("2"), newTestMiddleware("3"), newTestMiddleware("4")).
Use(newTestMiddleware("5"))
ms.Use(newTestMiddleware("6"))
resp := httptest.NewRecorder()
ms.WrapRevert(&testHandler{result: "handler"}).ServeHTTP(resp, nil)
assert.Equal(t, []byte("654321handler"), resp.Body.Bytes())
}
func TestMidSimple_WrapHandlerFuncRegular(t *testing.T) {
ms := New()
ms.Use(newTestMiddleware("1"))
ms.Use(newTestMiddleware("2"), newTestMiddleware("3"), newTestMiddleware("4")).
Use(newTestMiddleware("5"))
ms.Use(newTestMiddleware("6"))
resp := httptest.NewRecorder()
ms.WrapFunc((&testHandler{result: "handler"}).ServeHTTP).ServeHTTP(resp, nil)
assert.Equal(t, []byte("123456handler"), resp.Body.Bytes())
}
func TestMidSimple_WrapHandlerFuncRevert(t *testing.T) {
ms := New()
ms.Use(newTestMiddleware("1"))
ms.Use(newTestMiddleware("2"), newTestMiddleware("3"), newTestMiddleware("4")).
Use(newTestMiddleware("5"))
ms.Use(newTestMiddleware("6"))
resp := httptest.NewRecorder()
ms.WrapRevertFunc((&testHandler{result: "handler"}).ServeHTTP).ServeHTTP(resp, nil)
assert.Equal(t, []byte("654321handler"), resp.Body.Bytes())
}
func TestMidSimple_WrapHandlerFuncRegularSplit(t *testing.T) {
ms := New()
ms.Use(newTestMiddleware("1"))
ms.Use(newTestMiddleware("2"), newTestMiddleware("3"), newTestMiddleware("4")).Use(newTestMiddleware("5"))
ms2 := ms.Split().Use(newTestMiddleware("6b"))
ms.Use(newTestMiddleware("6a"))
resp := httptest.NewRecorder()
ms.WrapFunc((&testHandler{result: "handler"}).ServeHTTP).ServeHTTP(resp, nil)
assert.Equal(t, "123456ahandler", resp.Body.String())
resp = httptest.NewRecorder()
ms2.WrapFunc((&testHandler{result: "handler"}).ServeHTTP).ServeHTTP(resp, nil)
assert.Equal(t, "123456bhandler", resp.Body.String())
}