Skip to content

Commit

Permalink
πŸ› [Fix]: unhandle in strictmode (#2055)
Browse files Browse the repository at this point in the history
* fix: unhandle in strictmode

* πŸ› fix: error test

* βœ… chore: add testcases for strictrouting

* βœ… chore: fix test case
  • Loading branch information
wangjq4214 committed Sep 7, 2022
1 parent 9fc80fc commit e8c93e6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
66 changes: 66 additions & 0 deletions app_test.go
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion helpers.go
Expand Up @@ -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
}

Expand Down
5 changes: 1 addition & 4 deletions helpers_test.go
Expand Up @@ -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)

Expand Down

1 comment on commit e8c93e6

@ReneWerner87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: e8c93e6 Previous: 9a5ab89 Ratio
Benchmark_StatusMessage/default 9.818 ns/op 0 B/op 0 allocs/op 4.018 ns/op 0 B/op 0 allocs/op 2.44

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.