diff --git a/app_test.go b/app_test.go index d186193a..aadb70d7 100644 --- a/app_test.go +++ b/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() { diff --git a/helpers.go b/helpers.go index 73e882e3..c3360ad0 100644 --- a/helpers.go +++ b/helpers.go @@ -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 } diff --git a/helpers_test.go b/helpers_test.go index 03c1e687..8afb71bb 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -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)