mirror of
https://github.com/kataras/iris.git
synced 2025-12-20 03:17:04 +00:00
Former-commit-id: 8a8a25ab8fb33a342c8d05fc7eae7cafd5bb02b2
This commit is contained in:
50
policy_routerwrapper_test.go
Normal file
50
policy_routerwrapper_test.go
Normal file
@@ -0,0 +1,50 @@
|
||||
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.OnError(StatusNotFound, func(ctx *Context) {
|
||||
ctx.Writef("not found")
|
||||
})
|
||||
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user