This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gonion_test.go
144 lines (127 loc) · 4.27 KB
/
gonion_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package gonion
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestAppInitialization(t *testing.T) {
g := New()
g.Handle("GET", "/index2", http.HandlerFunc(getIndex2))
routes := g.BuildRoutes()
route := routes.routeFor("*", "/index2")
recorder := httptest.NewRecorder()
route.Handler.ServeHTTP(recorder, new(http.Request))
response := recorder.Body.String()
assert.Equal(t, response, "Success!")
}
type wrapper struct {
http.ResponseWriter
}
func (rw *wrapper) Write(b []byte) (int, error) {
bytes, _ := rw.ResponseWriter.Write([]byte("wrapper"))
moreBytes, _ := rw.ResponseWriter.Write(b)
return bytes + moreBytes, nil
}
func TestWrappingChainWithNewWriter(t *testing.T) {
g := New()
wrapperHandler := func(inner http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
inner.ServeHTTP(&wrapper{rw}, r)
})
}
g.Use().ChainLink(wrapperHandler)
g.Use().Func(func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("no-wrap"))
})
g.Use().ChainLink(wrapperHandler)
g.Use().ChainLink(wrapperHandler)
g.Handle("GET", "/index2", http.HandlerFunc(getIndex2))
routes := g.BuildRoutes()
route := routes.routeFor("*", "/index2")
recorder := httptest.NewRecorder()
route.Handler.ServeHTTP(recorder, new(http.Request))
response := recorder.Body.String()
assert.Equal(t, response, "wrapperno-wrapwrapperwrapperwrapperwrapperwrapperwrapperwrapperSuccess!")
}
func oneOfEachRoute() *Composer {
g := New()
addRoute := func(method string) {
g.Handle(method, "/", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte(method))
}))
}
addRoute("POST")
addRoute("GET")
addRoute("PUT")
addRoute("PATCH")
addRoute("DELETE")
return g
}
func assertRouteConstraintResponse(t *testing.T, c *Composer, method string, expectedResponse string) {
route := c.BuildRoutes().routeFor(method, "/")
recorder := httptest.NewRecorder()
route.Handler.ServeHTTP(recorder, nil)
assert.Equal(t, recorder.Body.String(), expectedResponse)
}
func TestConstrainingMiddleware_GetAppliesGetOnlyMiddleware(t *testing.T) {
g := oneOfEachRoute()
g.Only().Get().Use().Func(func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("GETONLY"))
})
assertRouteConstraintResponse(t, g, "GET", "GETONLYGET")
assertRouteConstraintResponse(t, g, "POST", "POST")
}
func TestConstrainingMiddleware_PutAppliesPutOnlyMiddleware(t *testing.T) {
g := oneOfEachRoute()
g.Only().Put().Use().Func(func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("PUTONLY"))
})
assertRouteConstraintResponse(t, g, "GET", "GET")
assertRouteConstraintResponse(t, g, "PUT", "PUTONLYPUT")
}
func TestConstrainingMiddleware_PatchAppliesPatchOnlyMiddleware(t *testing.T) {
g := oneOfEachRoute()
g.Only().Patch().Use().Func(func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("PATCHONLY"))
})
assertRouteConstraintResponse(t, g, "GET", "GET")
assertRouteConstraintResponse(t, g, "PATCH", "PATCHONLYPATCH")
}
func TestConstrainingMiddleware_DeleteAppliesDeleteOnlyMiddleware(t *testing.T) {
g := oneOfEachRoute()
g.Only().Delete().Use().Func(func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("DELETEONLY"))
})
assertRouteConstraintResponse(t, g, "GET", "GET")
assertRouteConstraintResponse(t, g, "DELETE", "DELETEONLYDELETE")
}
func TestConstrainingMiddleware_PostAppliesPostOnlyMiddleware(t *testing.T) {
g := oneOfEachRoute()
g.Only().Post().Use().Func(func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("POSTONLY"))
})
assertRouteConstraintResponse(t, g, "POST", "POSTONLYPOST")
assertRouteConstraintResponse(t, g, "GET", "GET")
}
func TestConstrainingMiddleware_WithArbitraryCondition(t *testing.T) {
g := oneOfEachRoute()
g.Only().When(func() bool {
return false
}).Use().Func(func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("nada"))
})
assertRouteConstraintResponse(t, g, "GET", "GET")
assertRouteConstraintResponse(t, g, "DELETE", "DELETE")
}
func (routes Routes) routeFor(method string, pattern string) *Route {
for _, r := range routes {
if r.Pattern == pattern && (method == "*" || method == r.Method) {
return r
}
}
return nil
}
func getIndex2(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("Success!"))
}