mirror of
https://github.com/kataras/iris.git
synced 2025-12-20 03:17:04 +00:00
Add a simple and pure .Regex middleware for routers that don't support regex route path validations out-of-the-box
Former-commit-id: 84cf1fb267e54543ad6d419b2ca39658b2773b58
This commit is contained in:
@@ -134,8 +134,6 @@ func TestHTTPRouterSimpleParty(t *testing.T) {
|
||||
}
|
||||
|
||||
app.Config.VHost = "0.0.0.0:" + strconv.Itoa(getRandomNumber(2222, 2399))
|
||||
// app.Config.Tester.Debug = true
|
||||
// app.Config.Tester.ExplicitURL = true
|
||||
e := httptest.New(app, t)
|
||||
|
||||
request := func(reqPath string) {
|
||||
@@ -199,8 +197,7 @@ func TestHTTPRouterParamDecodedDecodeURL(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestHTTPRouterRouteURLPath(t *testing.T) {
|
||||
app := iris.New()
|
||||
app.Adapt(httprouter.New())
|
||||
app := newHTTPRouterApp()
|
||||
|
||||
app.None("/profile/:user_id/:ref/*anything", nil).ChangeName("profile")
|
||||
app.Boot()
|
||||
@@ -213,8 +210,7 @@ func TestHTTPRouterRouteURLPath(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestHTTPRouterFireMethodNotAllowed(t *testing.T) {
|
||||
app := iris.New()
|
||||
app.Adapt(httprouter.New())
|
||||
app := newHTTPRouterApp()
|
||||
app.Config.FireMethodNotAllowed = true
|
||||
h := func(ctx *iris.Context) {
|
||||
ctx.WriteString(ctx.Method())
|
||||
@@ -235,3 +231,18 @@ func TestHTTPRouterFireMethodNotAllowed(t *testing.T) {
|
||||
|
||||
e.POST("/mypath").Expect().Status(iris.StatusMethodNotAllowed).Body().Equal("Hello from my custom 405 page")
|
||||
}
|
||||
|
||||
func TestHTTPRouterRegexMiddleware(t *testing.T) {
|
||||
app := newHTTPRouterApp()
|
||||
|
||||
app.Get("/users/:userid", app.Regex("userid", "[0-9]+$"), func(ctx *iris.Context) {})
|
||||
app.Get("/profile/:username", app.Regex("username", "[a-zA-Z]+$"), func(ctx *iris.Context) {})
|
||||
|
||||
e := httptest.New(app, t)
|
||||
|
||||
e.GET("/users/42").Expect().Status(iris.StatusOK)
|
||||
e.GET("/users/sarantaduo").Expect().Status(iris.StatusNotFound)
|
||||
|
||||
e.GET("/profile/gerasimosmaropoulos").Expect().Status(iris.StatusOK)
|
||||
e.GET("/profile/anumberof42").Expect().Status(iris.StatusNotFound)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user