From 867f2fc0a80f0b969a530f84a8604cd6cbe70794 Mon Sep 17 00:00:00 2001 From: hi019 Date: Sat, 3 Oct 2020 10:41:14 -0400 Subject: [PATCH 01/11] =?UTF-8?q?=F0=9F=90=9B=20Fix=20passing=20partial=20?= =?UTF-8?q?cookie=20to=20csrf.New?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware/csrf/csrf.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/middleware/csrf/csrf.go b/middleware/csrf/csrf.go index a933c717..84d130f9 100644 --- a/middleware/csrf/csrf.go +++ b/middleware/csrf/csrf.go @@ -72,17 +72,18 @@ func New(config ...Config) fiber.Handler { if cfg.ContextKey == "" { cfg.ContextKey = ConfigDefault.ContextKey } - if cfg.Cookie == nil { - cfg.Cookie = ConfigDefault.Cookie + if cfg.Cookie != nil { if cfg.Cookie.Name == "" { cfg.Cookie.Name = "_csrf" } if cfg.Cookie.SameSite == "" { cfg.Cookie.SameSite = "Strict" } - } - if cfg.CookieExpires == 0 { - cfg.CookieExpires = ConfigDefault.CookieExpires + if cfg.CookieExpires == 0 { + cfg.CookieExpires = ConfigDefault.CookieExpires + } + } else { + cfg.Cookie = ConfigDefault.Cookie } } From 4898778e283dd428695cebfbf8f45ed6b60bb2a3 Mon Sep 17 00:00:00 2001 From: hi019 Date: Sat, 3 Oct 2020 10:50:29 -0400 Subject: [PATCH 02/11] =?UTF-8?q?=F0=9F=90=9B=20Move=20cookie=20expires=20?= =?UTF-8?q?outside=20of=20if?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware/csrf/csrf.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/middleware/csrf/csrf.go b/middleware/csrf/csrf.go index 84d130f9..e849dea1 100644 --- a/middleware/csrf/csrf.go +++ b/middleware/csrf/csrf.go @@ -72,6 +72,9 @@ func New(config ...Config) fiber.Handler { if cfg.ContextKey == "" { cfg.ContextKey = ConfigDefault.ContextKey } + if cfg.CookieExpires == 0 { + cfg.CookieExpires = ConfigDefault.CookieExpires + } if cfg.Cookie != nil { if cfg.Cookie.Name == "" { cfg.Cookie.Name = "_csrf" @@ -79,9 +82,6 @@ func New(config ...Config) fiber.Handler { if cfg.Cookie.SameSite == "" { cfg.Cookie.SameSite = "Strict" } - if cfg.CookieExpires == 0 { - cfg.CookieExpires = ConfigDefault.CookieExpires - } } else { cfg.Cookie = ConfigDefault.Cookie } From 1fe6d3b25de72ae2d91fe4de907c83d5386e1411 Mon Sep 17 00:00:00 2001 From: hi019 Date: Sat, 3 Oct 2020 10:52:06 -0400 Subject: [PATCH 03/11] Use default values for cookie fields --- middleware/csrf/csrf.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/middleware/csrf/csrf.go b/middleware/csrf/csrf.go index e849dea1..046d619a 100644 --- a/middleware/csrf/csrf.go +++ b/middleware/csrf/csrf.go @@ -77,10 +77,10 @@ func New(config ...Config) fiber.Handler { } if cfg.Cookie != nil { if cfg.Cookie.Name == "" { - cfg.Cookie.Name = "_csrf" + cfg.Cookie.Name = ConfigDefault.Cookie.Name } if cfg.Cookie.SameSite == "" { - cfg.Cookie.SameSite = "Strict" + cfg.Cookie.SameSite = ConfigDefault.Cookie.SameSite } } else { cfg.Cookie = ConfigDefault.Cookie From 102d167b9f50c828d6b3f3611550690978df6e72 Mon Sep 17 00:00:00 2001 From: kiyon Date: Sat, 3 Oct 2020 23:59:41 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=F0=9F=91=B7=20handle=20error=20of=20fmt.?= =?UTF-8?q?Fprintf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.go b/app.go index 82c0eb55..3395fb5d 100644 --- a/app.go +++ b/app.go @@ -752,7 +752,7 @@ func (app *App) startupMessage(addr string, tls bool, pids string) { if os.Getenv("TERM") == "dumb" || (!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd())) { out = colorable.NewNonColorable(os.Stdout) } - fmt.Fprintf(out, logo, + _, _ = fmt.Fprintf(out, logo, cBlack, centerValue(" Fiber v"+Version, 49), center(addr, 49), From 407cdfaf7aa2a5a2021c0d7f2ec34c95cd812f49 Mon Sep 17 00:00:00 2001 From: kiyon Date: Sun, 4 Oct 2020 00:05:28 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=F0=9F=91=B7=20add=20app=20test=20case=20?= =?UTF-8?q?to=20improve=20coverage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app_test.go b/app_test.go index 3c80a058..e6395e3d 100644 --- a/app_test.go +++ b/app_test.go @@ -1071,3 +1071,9 @@ func Test_App_Master_Process_Show_Startup_Message(t *testing.T) { New(Config{Prefork: true}). startupMessage(":3000", true, "") } + +func Test_App_Server(t *testing.T) { + app := New() + + utils.AssertEqual(t, false, app.Server() == nil) +} From ea79c4ef454bcd5fed459187d53025614a55e231 Mon Sep 17 00:00:00 2001 From: kiyon Date: Sun, 4 Oct 2020 00:25:46 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=F0=9F=91=B7=20add=20ctx=20test=20case=20?= =?UTF-8?q?to=20improve=20coverage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ctx_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/ctx_test.go b/ctx_test.go index b3fa736b..6c2b912b 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -746,6 +746,15 @@ func Test_Ctx_IP(t *testing.T) { utils.AssertEqual(t, "0.0.0.0", c.IP()) } +// go test -run Test_Ctx_IP_ProxyHeader +func Test_Ctx_IP_ProxyHeader(t *testing.T) { + t.Parallel() + app := New(Config{ProxyHeader: "Real-Ip"}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) + defer app.ReleaseCtx(c) + utils.AssertEqual(t, "", c.IP()) +} + // go test -run Test_Ctx_IPs -parallel func Test_Ctx_IPs(t *testing.T) { t.Parallel() @@ -1852,6 +1861,17 @@ func Benchmark_Ctx_Write(b *testing.B) { } } +// go test -run Test_Ctx_WriteString +func Test_Ctx_WriteString(t *testing.T) { + t.Parallel() + app := New() + c := app.AcquireCtx(&fasthttp.RequestCtx{}) + defer app.ReleaseCtx(c) + c.WriteString("Hello, ") + c.WriteString("World!") + utils.AssertEqual(t, "Hello, World!", string(c.Response().Body())) +} + // go test -run Test_Ctx_XHR func Test_Ctx_XHR(t *testing.T) { t.Parallel() @@ -2030,3 +2050,13 @@ func Benchmark_Ctx_BodyStreamWriter(b *testing.B) { }) } } + +func Test_Ctx_String(t *testing.T) { + t.Parallel() + + app := New() + c := app.AcquireCtx(&fasthttp.RequestCtx{}) + defer app.ReleaseCtx(c) + + utils.AssertEqual(t, "#0000000000000000 - 0.0.0.0:0 <-> 0.0.0.0:0 - GET http:///", c.String()) +} From 839c8f14473185dcd88b10b083bc32e4f2a09a3f Mon Sep 17 00:00:00 2001 From: kiyon Date: Sun, 4 Oct 2020 00:36:11 +0800 Subject: [PATCH 07/11] =?UTF-8?q?=F0=9F=91=B7=20fix=20group.Mount?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app_test.go | 16 ++++++++++++++++ group.go | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app_test.go b/app_test.go index 3c80a058..45bf1529 100644 --- a/app_test.go +++ b/app_test.go @@ -736,6 +736,22 @@ func Test_App_Group_Invalid(t *testing.T) { New().Group("/").Use(1) } +// go test -run Test_App_Group_Mount +func Test_App_Group_Mount(t *testing.T) { + micro := New() + micro.Get("/doe", func(c *Ctx) error { + return c.SendStatus(StatusOK) + }) + + app := New() + v1 := app.Group("/v1") + v1.Mount("/john", micro) + + resp, err := app.Test(httptest.NewRequest("GET", "/v1/john/doe", nil)) + utils.AssertEqual(t, nil, err, "app.Test(req)") + utils.AssertEqual(t, 200, resp.StatusCode, "Status code") +} + func Test_App_Group(t *testing.T) { var dummyHandler = testEmptyHandler diff --git a/group.go b/group.go index 6191e991..a3b10e4a 100644 --- a/group.go +++ b/group.go @@ -23,7 +23,7 @@ func (grp *Group) Mount(prefix string, fiber *App) Router { for m := range stack { for r := range stack[m] { route := grp.app.copyRoute(stack[m][r]) - grp.app.addRoute(route.Method, grp.app.addPrefixToRoute(prefix, route)) + grp.app.addRoute(route.Method, grp.app.addPrefixToRoute(getGroupPath(grp.prefix, prefix), route)) } } return grp From 618b7532304525af49ddca0489aabd7df01ebfe1 Mon Sep 17 00:00:00 2001 From: David Mazary Date: Sat, 3 Oct 2020 13:20:54 -0400 Subject: [PATCH 08/11] :rotating_light: Fix gosimple lint warnings --- app_test.go | 2 +- ctx.go | 2 +- ctx_test.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app_test.go b/app_test.go index 3593d31c..2a2809d4 100644 --- a/app_test.go +++ b/app_test.go @@ -121,7 +121,7 @@ func Test_App_ServerErrorHandler_SmallReadBuffer(t *testing.T) { }) request := httptest.NewRequest("GET", "/", nil) - logHeaderSlice := make([]string, 5000, 5000) + logHeaderSlice := make([]string, 5000) request.Header.Set("Very-Long-Header", strings.Join(logHeaderSlice, "-")) _, err := app.Test(request) diff --git a/ctx.go b/ctx.go index 64dd3284..5a03917f 100644 --- a/ctx.go +++ b/ctx.go @@ -708,7 +708,7 @@ func (c *Ctx) QueryParser(out interface{}) error { c.fasthttp.QueryArgs().VisitAll(func(key []byte, val []byte) { k := utils.UnsafeString(key) v := utils.UnsafeString(val) - if strings.Index(v, ",") > -1 && equalFieldType(out, reflect.Slice, k) { + if strings.Contains(v, ",") && equalFieldType(out, reflect.Slice, k) { values := strings.Split(v, ",") for i := 0; i < len(values); i++ { data[k] = append(data[k], values[i]) diff --git a/ctx_test.go b/ctx_test.go index 6c2b912b..b160aab9 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -1326,7 +1326,7 @@ func Test_Ctx_SendFile_Immutable(t *testing.T) { if err := c.SendFile("./.github/" + file + ".html"); err != nil { utils.AssertEqual(t, nil, err) } - utils.AssertEqual(t, "index", fmt.Sprintf("%s", file)) + utils.AssertEqual(t, "index", file) return c.SendString(file) }) // 1st try From 71d3e16be4ebe410377e688a5804a22dca0bcc97 Mon Sep 17 00:00:00 2001 From: David Mazary Date: Sat, 3 Oct 2020 13:21:26 -0400 Subject: [PATCH 09/11] :rotating_light: Remove unused isIPv6 func --- helpers.go | 4 ---- helpers_test.go | 24 ------------------------ 2 files changed, 28 deletions(-) diff --git a/helpers.go b/helpers.go index 464304cd..8c53cbf4 100644 --- a/helpers.go +++ b/helpers.go @@ -307,10 +307,6 @@ func isEtagStale(etag string, noneMatchBytes []byte) bool { return !matchEtag(getString(noneMatchBytes[start:end]), etag) } -func isIPv6(address string) bool { - return strings.Count(address, ":") >= 2 -} - func parseAddr(raw string) (host, port string) { if i := strings.LastIndex(raw, ":"); i != -1 { return raw[:i], raw[i+1:] diff --git a/helpers_test.go b/helpers_test.go index 223eee2c..5b198694 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -209,30 +209,6 @@ func Benchmark_Utils_Unescape(b *testing.B) { utils.AssertEqual(b, "/créer", unescaped) } -func Test_Utils_IPv6(t *testing.T) { - testCases := []struct { - string - bool - }{ - {"::FFFF:C0A8:1:3000", true}, - {"::FFFF:C0A8:0001:3000", true}, - {"0000:0000:0000:0000:0000:FFFF:C0A8:1:3000", true}, - {"::FFFF:C0A8:1%1:3000", true}, - {"::FFFF:192.168.0.1:3000", true}, - {"[::FFFF:C0A8:1]:3000", true}, - {"[::FFFF:C0A8:1%1]:3000", true}, - {":3000", false}, - {"127.0.0.1:3000", false}, - {"127.0.0.1:", false}, - {"0.0.0.0:3000", false}, - {"", false}, - } - - for _, c := range testCases { - utils.AssertEqual(t, c.bool, isIPv6(c.string)) - } -} - func Test_Utils_Parse_Address(t *testing.T) { testCases := []struct { addr, host, port string From dcea4d5ddbb9dabdb6ce34b2ad4d68ced6b0b4c3 Mon Sep 17 00:00:00 2001 From: Roman Date: Sat, 3 Oct 2020 23:33:57 +0200 Subject: [PATCH 10/11] =?UTF-8?q?=F0=9F=A9=B9fix=20the=20duplicated=20thir?= =?UTF-8?q?d=20party=20license=20links=20in=20the=20readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit right now the readme links to different third party licenses. The last part has some of the doubled. This PR removes them --- .github/README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/README.md b/.github/README.md index fec67ad7..0ecd5f81 100644 --- a/.github/README.md +++ b/.github/README.md @@ -588,6 +588,3 @@ Copyright (c) 2019-present [Fenny](https://github.com/fenny) and [Contributors]( - [gopsutil](https://github.com/shirou/gopsutil/blob/master/LICENSE) - [go-ole](https://github.com/go-ole/go-ole) - [wmi](https://github.com/StackExchange/wmi) -- [gopsutil](https://github.com/shirou/gopsutil/blob/master/LICENSE) -- [go-ole](https://github.com/go-ole/go-ole) -- [wmi](https://github.com/StackExchange/wmi) From 9df61e925c933fdf3d83804fae302a7d2f7a6ff2 Mon Sep 17 00:00:00 2001 From: Konstantinos Lypitkas Date: Sun, 4 Oct 2020 10:41:54 +0300 Subject: [PATCH 11/11] Increase coverage in cache middleware --- middleware/cache/cache_test.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/middleware/cache/cache_test.go b/middleware/cache/cache_test.go index 7324284d..81fb3263 100644 --- a/middleware/cache/cache_test.go +++ b/middleware/cache/cache_test.go @@ -152,5 +152,30 @@ func Test_Cache_Invalid_Method(t *testing.T) { body, err = ioutil.ReadAll(resp.Body) utils.AssertEqual(t, nil, err) utils.AssertEqual(t, "123", string(body)) - +} + +func Test_Cache_NothingToCache(t *testing.T) { + app := fiber.New() + + app.Use(New(Config{Expiration: -(time.Second * 1)})) + + app.Get("/", func(c *fiber.Ctx) error { + return c.SendString(time.Now().String()) + }) + + resp, err := app.Test(httptest.NewRequest("GET", "/", nil)) + utils.AssertEqual(t, nil, err) + body, err := ioutil.ReadAll(resp.Body) + utils.AssertEqual(t, nil, err) + + time.Sleep(500 * time.Millisecond) + + respCached, err := app.Test(httptest.NewRequest("GET", "/", nil)) + utils.AssertEqual(t, nil, err) + bodyCached, err := ioutil.ReadAll(respCached.Body) + utils.AssertEqual(t, nil, err) + + if bytes.Equal(body, bodyCached) { + t.Errorf("Cache should have expired: %s, %s", body, bodyCached) + } }