From e8c93e6153687be33638d8b5ecfebe186f70e569 Mon Sep 17 00:00:00 2001 From: Jinquan Wang <35188480+wangjq4214@users.noreply.github.com> Date: Wed, 7 Sep 2022 17:05:37 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20[Fix]:=20unhandle=20in=20strictm?= =?UTF-8?q?ode=20(#2055)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: unhandle in strictmode * 🐛 fix: error test * ✅ chore: add testcases for strictrouting * ✅ chore: fix test case --- app_test.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ helpers.go | 2 +- helpers_test.go | 5 +--- 3 files changed, 68 insertions(+), 5 deletions(-) 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)