mirror of https://github.com/gofiber/fiber.git
🐛 [Fix]: unhandle in strictmode (#2055)
* fix: unhandle in strictmode * 🐛 fix: error test * ✅ chore: add testcases for strictrouting * ✅ chore: fix test casepull/2077/head
parent
9fc80fcc92
commit
e8c93e6153
66
app_test.go
66
app_test.go
|
@ -423,6 +423,72 @@ func Test_App_Use_CaseSensitive(t *testing.T) {
|
|||
utils.AssertEqual(t, "/AbC", app.getString(body))
|
||||
}
|
||||
|
||||
func Test_App_Not_Use_StrictRouting(t *testing.T) {
|
||||
app := New()
|
||||
|
||||
app.Use("/abc", func(c *Ctx) error {
|
||||
return c.SendString(c.Path())
|
||||
})
|
||||
|
||||
g := app.Group("/foo")
|
||||
g.Use("/", func(c *Ctx) error {
|
||||
return c.SendString(c.Path())
|
||||
})
|
||||
|
||||
// wrong path in the requested route -> 404
|
||||
resp, err := app.Test(httptest.NewRequest(MethodGet, "/abc/", nil))
|
||||
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")
|
||||
|
||||
// right path in the requrested route -> 200
|
||||
resp, err = app.Test(httptest.NewRequest(MethodGet, "/abc", nil))
|
||||
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")
|
||||
|
||||
// wrong path with group in the requested route -> 404
|
||||
resp, err = app.Test(httptest.NewRequest(MethodGet, "/foo", nil))
|
||||
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")
|
||||
|
||||
// right path with group in the requrested route -> 200
|
||||
resp, err = app.Test(httptest.NewRequest(MethodGet, "/foo/", nil))
|
||||
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")
|
||||
}
|
||||
|
||||
func Test_App_Use_StrictRouting(t *testing.T) {
|
||||
app := New(Config{StrictRouting: true})
|
||||
|
||||
app.Get("/abc", func(c *Ctx) error {
|
||||
return c.SendString(c.Path())
|
||||
})
|
||||
|
||||
g := app.Group("/foo")
|
||||
g.Get("/", func(c *Ctx) error {
|
||||
return c.SendString(c.Path())
|
||||
})
|
||||
|
||||
// wrong path in the requested route -> 404
|
||||
resp, err := app.Test(httptest.NewRequest(MethodGet, "/abc/", nil))
|
||||
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||
utils.AssertEqual(t, StatusNotFound, resp.StatusCode, "Status code")
|
||||
|
||||
// right path in the requrested route -> 200
|
||||
resp, err = app.Test(httptest.NewRequest(MethodGet, "/abc", nil))
|
||||
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")
|
||||
|
||||
// wrong path with group in the requested route -> 404
|
||||
resp, err = app.Test(httptest.NewRequest(MethodGet, "/foo", nil))
|
||||
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||
utils.AssertEqual(t, StatusNotFound, resp.StatusCode, "Status code")
|
||||
|
||||
// right path with group in the requrested route -> 200
|
||||
resp, err = app.Test(httptest.NewRequest(MethodGet, "/foo/", nil))
|
||||
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")
|
||||
}
|
||||
|
||||
func Test_App_Add_Method_Test(t *testing.T) {
|
||||
app := New()
|
||||
defer func() {
|
||||
|
|
|
@ -191,7 +191,7 @@ func setETag(c *Ctx, weak bool) {
|
|||
}
|
||||
|
||||
func getGroupPath(prefix, path string) string {
|
||||
if len(path) == 0 || path == "/" {
|
||||
if len(path) == 0 {
|
||||
return prefix
|
||||
}
|
||||
|
||||
|
|
|
@ -148,14 +148,11 @@ func Benchmark_Utils_ETag_Weak(b *testing.B) {
|
|||
func Test_Utils_getGroupPath(t *testing.T) {
|
||||
t.Parallel()
|
||||
res := getGroupPath("/v1", "/")
|
||||
utils.AssertEqual(t, "/v1", res)
|
||||
utils.AssertEqual(t, "/v1/", res)
|
||||
|
||||
res = getGroupPath("/v1/", "/")
|
||||
utils.AssertEqual(t, "/v1/", res)
|
||||
|
||||
res = getGroupPath("/v1", "/")
|
||||
utils.AssertEqual(t, "/v1", res)
|
||||
|
||||
res = getGroupPath("/", "/")
|
||||
utils.AssertEqual(t, "/", res)
|
||||
|
||||
|
|
Loading…
Reference in New Issue