mirror of
https://github.com/kataras/iris.git
synced 2026-02-11 13:15:56 +00:00
HTTP error handlers per Party (docs and details in progress)
Former-commit-id: 7092ebed556b56d9f1769b9b23f2340c2a3a18f7
This commit is contained in:
@@ -64,9 +64,73 @@ func TestOnAnyErrorCode(t *testing.T) {
|
||||
}
|
||||
|
||||
func checkAndClearBuf(t *testing.T, buff *bytes.Buffer, expected string) {
|
||||
t.Helper()
|
||||
|
||||
if got, expected := buff.String(), expected; got != expected {
|
||||
t.Fatalf("expected middleware to run before the error handler, expected %s but got %s", expected, got)
|
||||
t.Fatalf("expected middleware to run before the error handler, expected: '%s' but got: '%s'", expected, got)
|
||||
}
|
||||
|
||||
buff.Reset()
|
||||
}
|
||||
|
||||
func TestPartyOnErrorCode(t *testing.T) {
|
||||
app := iris.New()
|
||||
app.Configure(iris.WithFireMethodNotAllowed)
|
||||
|
||||
globalNotFoundResponse := "custom not found"
|
||||
app.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context) {
|
||||
ctx.WriteString(globalNotFoundResponse)
|
||||
})
|
||||
|
||||
globalMethodNotAllowedResponse := "global: method not allowed"
|
||||
app.OnErrorCode(iris.StatusMethodNotAllowed, func(ctx iris.Context) {
|
||||
ctx.WriteString(globalMethodNotAllowedResponse)
|
||||
})
|
||||
|
||||
app.Get("/path", h)
|
||||
|
||||
h := func(ctx iris.Context) { ctx.WriteString(ctx.Path()) }
|
||||
usersResponse := "users: method allowed"
|
||||
|
||||
users := app.Party("/users")
|
||||
users.OnErrorCode(iris.StatusMethodNotAllowed, func(ctx iris.Context) {
|
||||
ctx.WriteString(usersResponse)
|
||||
})
|
||||
users.Get("/", h)
|
||||
// test setting the error code from a handler.
|
||||
users.Get("/badrequest", func(ctx iris.Context) { ctx.StatusCode(iris.StatusBadRequest) })
|
||||
|
||||
usersuserResponse := "users:user: method allowed"
|
||||
user := users.Party("/{id:int}")
|
||||
user.OnErrorCode(iris.StatusMethodNotAllowed, func(ctx iris.Context) {
|
||||
ctx.WriteString(usersuserResponse)
|
||||
})
|
||||
// usersuserNotFoundResponse := "users:user: not found"
|
||||
// user.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context) {
|
||||
// ctx.WriteString(usersuserNotFoundResponse)
|
||||
// })
|
||||
user.Get("/", h)
|
||||
|
||||
e := httptest.New(t, app)
|
||||
|
||||
e.GET("/notfound").Expect().Status(iris.StatusNotFound).Body().Equal(globalNotFoundResponse)
|
||||
e.POST("/path").Expect().Status(iris.StatusMethodNotAllowed).Body().Equal(globalMethodNotAllowedResponse)
|
||||
e.GET("/path").Expect().Status(iris.StatusOK).Body().Equal("/path")
|
||||
|
||||
e.POST("/users").Expect().Status(iris.StatusMethodNotAllowed).
|
||||
Body().Equal(usersResponse)
|
||||
|
||||
e.POST("/users/42").Expect().Status(iris.StatusMethodNotAllowed).
|
||||
Body().Equal(usersuserResponse)
|
||||
|
||||
e.GET("/users/42").Expect().Status(iris.StatusOK).
|
||||
Body().Equal("/users/42")
|
||||
// e.GET("/users/ab").Expect().Status(iris.StatusNotFound).Body().Equal(usersuserNotFoundResponse)
|
||||
|
||||
// if not registered to the party, then the root is taking action.
|
||||
e.GET("/users/ab/cd").Expect().Status(iris.StatusNotFound).Body().Equal(globalNotFoundResponse)
|
||||
|
||||
// if not registered to the party, and not in root, then just write the status text (fallback behavior)
|
||||
e.GET("/users/badrequest").Expect().Status(iris.StatusBadRequest).
|
||||
Body().Equal(http.StatusText(iris.StatusBadRequest))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user