iris/policy_routerwrapper_test.go
Gerasimos (Makis) Maropoulos f487cd0029 Add some _examples in the main repository too.
Former-commit-id: 98895c34115ec2076b431332f0ffe9645adf7590
2017-03-13 15:16:12 +02:00

47 lines
1.2 KiB
Go

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)
}