forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy_routerwrapper_test.go
46 lines (37 loc) · 1.17 KB
/
policy_routerwrapper_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
package iris_test
import (
"net/http"
"testing"
. "gopkg.in/kataras/iris.v6"
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
"gopkg.in/kataras/iris.v6/httptest"
)
func TestRouterWrapperPolicySimple(t *testing.T) {
w1 := RouterWrapperPolicy(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
w.Write([]byte("DATA "))
next(w, r) // continue to the main router
})
w2 := RouterWrapperPolicy(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
if r.RequestURI == "/" {
next(w, r) // continue to w1
return
}
// else don't execute the router and the handler and fire not found
w.WriteHeader(StatusNotFound)
})
app := New()
app.Adapt(
httprouter.New(),
w1, // order matters, second wraps the first and so on, so the last(w2) is responsible to execute the next wrapper (if more than one) and the router
w2,
// w2 -> w1 -> httprouter -> handler
)
app.Get("/", func(ctx *Context) {
ctx.Write([]byte("OK"))
})
app.Get("/routerDoesntContinue", func(ctx *Context) {
})
e := httptest.New(app, t)
e.GET("/").Expect().Status(StatusOK).Body().Equal("DATA OK")
e.GET("/routerDoesntContinue").Expect().Status(StatusNotFound)
}