1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-08 04:21:57 +00:00

update the subdomain redirect example using the rewrite middleware

This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-08-26 00:07:07 +03:00
parent 5e82fa5b89
commit 1780d97d44
4 changed files with 151 additions and 52 deletions

View File

@@ -980,7 +980,7 @@ func (api *APIBuilder) UseOnce(handlers ...context.Handler) {
// UseGlobal registers handlers that should run at the very beginning.
// It prepends those handler(s) to all routes,
// including all parties, subdomains.
// including all parties, subdomains and errors.
// It doesn't care about call order, it will prepend the handlers to all
// existing routes and the future routes that may being registered.
//

View File

@@ -8,6 +8,7 @@ package router_test
import (
"fmt"
"net/http"
"testing"
"github.com/kataras/iris/v12"
@@ -285,3 +286,72 @@ func TestUseRouterSubdomains(t *testing.T) {
e.GET("/notfound").WithURL("http://old.example.com").Expect().Status(iris.StatusNotFound).Body().
Equal("Not Found")
}
func TestUseWrapOrder(t *testing.T) {
var (
expectedBody = "#1 .WrapRouter\n#2 .UseRouter\n#3 .UseGlobal\n#4 .Use\n#5 Main Handler\n"
expectedNotFoundBody = "#3 .UseGlobal\n#1 .UseError\n#2 Main Error Handler\n"
makeMiddleware = func(body string) iris.Handler {
return func(ctx iris.Context) {
ctx.WriteString(body)
ctx.Next()
}
}
handler = func(ctx iris.Context) {
ctx.WriteString("#5 Main Handler\n")
}
errorHandler = func(ctx iris.Context) {
ctx.WriteString("#2 Main Error Handler\n")
}
useHandler = makeMiddleware("#4 .Use\n")
useGlobal = makeMiddleware("#3 .UseGlobal\n")
useError = func(ctx iris.Context) {
// UseError has captured the status code, because it runs
// after the router itself but only one error handlers.
ctx.WriteString("#1 .UseError\n")
ctx.Next()
}
useRouter = func(ctx iris.Context) {
if ctx.Path() == "/" {
ctx.WriteString("#2 .UseRouter\n")
}
ctx.Next()
}
wrapRouter = func(w http.ResponseWriter, r *http.Request, router http.HandlerFunc) {
if r.URL.Path == "/" {
w.Write([]byte("#1 .WrapRouter\n"))
// Note for beginners, reading this test:
// if we write something here on a not found page,
// in the raw net/http wrapper like this one,
// then the response writer sends 200 status OK
// (on first write) and so any error handler will not be fired as expected,
// these are basic things. If you w.WriteHeader you cannot change the status code later on too.
// In Iris handlers, if you write before status code set, then it sends 200
// and it cannot change too (if you want to change that behavior you use ctx.Record()).
// However if you
// just call ctx.StatusCode without content written then you are able to change the status code
// later on.
}
router(w, r)
}
)
app := iris.New()
app.Use(useHandler)
app.UseGlobal(useGlobal)
app.UseError(useError)
app.UseRouter(useRouter)
app.WrapRouter(wrapRouter)
app.OnErrorCode(iris.StatusNotFound, errorHandler)
app.Get("/", handler)
e := httptest.New(t, app)
e.GET("/NotFound").Expect().Status(iris.StatusNotFound).Body().Equal(expectedNotFoundBody)
e.GET("/").Expect().Status(iris.StatusOK).Body().Equal(expectedBody)
}