From 756d02831e1abb6125f80d05a141f55e5230a34e Mon Sep 17 00:00:00 2001 From: Jinquan Wang Date: Sat, 27 Aug 2022 10:10:13 +0800 Subject: [PATCH 1/4] fix: unhandle in strictmode --- helpers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpers.go b/helpers.go index b4046b4730..efa632c0f3 100644 --- a/helpers.go +++ b/helpers.go @@ -222,7 +222,7 @@ func setETag(c *Ctx, weak bool) { } func getGroupPath(prefix, path string) string { - if len(path) == 0 || path == "/" { + if len(path) == 0 { return prefix } From dc43184b857925aeae4ceffb096aa2f13e1e62f6 Mon Sep 17 00:00:00 2001 From: Jinquan Wang Date: Sat, 27 Aug 2022 10:17:55 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=90=9B=20fix:=20error=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helpers_test.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/helpers_test.go b/helpers_test.go index 7b45540de7..35591de291 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -142,14 +142,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) From 74fa6ebfa1a0e4f3f7d95dd80aded7c5d8057cb5 Mon Sep 17 00:00:00 2001 From: Jinquan Wang Date: Tue, 30 Aug 2022 20:33:31 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E2=9C=85=20chore:=20add=20testcases=20for?= =?UTF-8?q?=20strictrouting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app_test.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/app_test.go b/app_test.go index fcb623bdfb..d1f4491ae7 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, 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, 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.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, 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() { From 27493e4701e205b04252b90778f742b0e1c311c8 Mon Sep 17 00:00:00 2001 From: Jinquan Wang Date: Tue, 30 Aug 2022 21:15:55 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E2=9C=85=20chore:=20fix=20test=20case?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app_test.go | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/app_test.go b/app_test.go index d1f4491ae7..90a80348ae 100644 --- a/app_test.go +++ b/app_test.go @@ -435,18 +435,18 @@ func Test_App_Not_Use_StrictRouting(t *testing.T) { 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") + // 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") + // 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)) + 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") @@ -459,27 +459,27 @@ func Test_App_Not_Use_StrictRouting(t *testing.T) { func Test_App_Use_StrictRouting(t *testing.T) { app := New(Config{StrictRouting: true}) - app.Use("/abc", func(c *Ctx) error { + app.Get("/abc", func(c *Ctx) error { return c.SendString(c.Path()) }) g := app.Group("/foo") - g.Use("/", func(c *Ctx) error { + 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") + // 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") + // 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)) + 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")