1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-23 04:47:02 +00:00

overlap routing: and mvc: allow setting status code from a dependency or a middleware

This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-08-28 04:11:56 +03:00
parent 933534574a
commit a6ec94e1a6
10 changed files with 181 additions and 63 deletions

View File

@@ -1,6 +1,7 @@
package router
import (
"errors"
"fmt"
"net/http"
"os"
@@ -143,8 +144,30 @@ func overlapRoute(r *Route, next *Route) {
if !ctx.IsStopped() {
return
}
} else if !defaultOverlapFilter(ctx) {
return
} else {
prevStatusCode := ctx.GetStatusCode()
if !defaultOverlapFilter(ctx) {
return
}
// set the status code that it was stopped with.
// useful for dependencies with StopWithStatus(XXX)
// instead of raw ctx.StopExecution().
// The func_result now also catch the last registered status code
// of the chain, unless the controller returns an integer.
// See _examples/authenticated-controller.
if prevStatusCode > 0 {
// An exception when stored error
// exists and it's type of ErrNotFound.
// Example:
// Version was not found:
// we need to be able to send the status on the last not found version
// but reset the status code if a next available matched version was found.
// see: versioning.Handler.
if !errors.Is(ctx.GetErr(), context.ErrNotFound) {
ctx.StatusCode(prevStatusCode)
}
}
}
ctx.SetErr(nil) // clear any stored error.
@@ -1051,7 +1074,7 @@ func (api *APIBuilder) Reset() Party {
return api
}
// ResetRouterFilters deactivates any pervious registered
// ResetRouterFilters deactivates any previous registered
// router filters and the parents ones for this Party.
//
// Returns this Party.

View File

@@ -121,7 +121,7 @@ type Party interface {
//
// Returns this Party.
Reset() Party
// ResetRouterFilters deactivates any pervious registered
// ResetRouterFilters deactivates any previous registered
// router filters and the parents ones for this Party.
//
// Returns this Party.

View File

@@ -1,6 +1,7 @@
package router_test
import (
"bytes"
"reflect"
"testing"
@@ -11,6 +12,10 @@ import (
func TestRegisterRule(t *testing.T) {
app := iris.New()
// collect the error on RouteError rule.
buf := new(bytes.Buffer)
app.Logger().SetTimeFormat("").DisableNewLine().SetOutput(buf)
v1 := app.Party("/v1")
v1.SetRegisterRule(iris.RouteSkip)
@@ -43,9 +48,9 @@ func TestRegisterRule(t *testing.T) {
if route := v1.Get("/", getHandler); route != nil {
t.Fatalf("expected duplicated route, with RouteError rule, to be nil but got: %#+v", route)
}
// if expected, got := 1, len(v1.GetReporter().Errors); expected != got {
// t.Fatalf("expected api builder's errors length to be: %d but got: %d", expected, got)
// }
if expected, got := "[ERRO] new route: GET /v1 conflicts with an already registered one: GET /v1 route", buf.String(); expected != got {
t.Fatalf("expected api builder's error to be:\n'%s'\nbut got:\n'%s'", expected, got)
}
}
func testRegisterRule(e *httptest.Expect, expectedGetBody string) {
@@ -73,6 +78,7 @@ func TestRegisterRuleOverlap(t *testing.T) {
ctx.StopWithStatus(iris.StatusUnauthorized)
})
usersRouter.Get("/", func(ctx iris.Context) {
ctx.StatusCode(iris.StatusOK)
ctx.WriteString("data")
})
@@ -99,6 +105,7 @@ func TestRegisterRuleOverlap(t *testing.T) {
ctx.StopWithText(iris.StatusUnauthorized, "no access")
})
usersRouter.Get("/p3", func(ctx iris.Context) {
ctx.StatusCode(iris.StatusOK)
ctx.WriteString("p3 data")
})