🐛 [Fix]: unhandle in strictmode (#2055)

* fix: unhandle in strictmode

* 🐛 fix: error test

*  chore: add testcases for strictrouting

*  chore: fix test case
pull/2077/head
Jinquan Wang 2022-09-07 17:05:37 +08:00 committed by GitHub
parent 9fc80fcc92
commit e8c93e6153
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 5 deletions

View File

@ -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() {

View File

@ -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
}

View File

@ -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)