From 738e710766478ea6dd72208b1f71a44c86f3dfa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Muhammed=20Efe=20=C3=87etin?= Date: Sun, 7 Aug 2022 19:06:05 +0300 Subject: [PATCH] :recycle: v3 (enhancement): use fs.FS as Filesystem type of favicon middleware --- middleware/favicon/favicon.go | 4 ++-- middleware/favicon/favicon_test.go | 24 ++---------------------- 2 files changed, 4 insertions(+), 24 deletions(-) diff --git a/middleware/favicon/favicon.go b/middleware/favicon/favicon.go index ef0fbb6a7f..ce5d042115 100644 --- a/middleware/favicon/favicon.go +++ b/middleware/favicon/favicon.go @@ -2,7 +2,7 @@ package favicon import ( "io" - "net/http" + "io/fs" "os" "strconv" @@ -25,7 +25,7 @@ type Config struct { // An example of this could be an embedded or network filesystem // // Optional. Default: nil - FileSystem http.FileSystem `json:"-"` + FileSystem fs.FS `json:"-"` // CacheControl defines how the Cache-Control header in the response should be set // diff --git a/middleware/favicon/favicon_test.go b/middleware/favicon/favicon_test.go index b18e4b850b..9d9e1984ce 100644 --- a/middleware/favicon/favicon_test.go +++ b/middleware/favicon/favicon_test.go @@ -1,10 +1,8 @@ package favicon import ( - "net/http" "net/http/httptest" "os" - "strings" "testing" "github.com/valyala/fasthttp" @@ -75,31 +73,13 @@ func Test_Middleware_Favicon_Found(t *testing.T) { utils.AssertEqual(t, "public, max-age=31536000", resp.Header.Get(fiber.HeaderCacheControl), "CacheControl Control") } -// mockFS wraps local filesystem for the purposes of -// Test_Middleware_Favicon_FileSystem located below -// TODO use os.Dir if fiber upgrades to 1.16 -type mockFS struct{} - -func (m mockFS) Open(name string) (http.File, error) { - if name == "/" { - name = "." - } else { - name = strings.TrimPrefix(name, "/") - } - file, err := os.Open(name) - if err != nil { - return nil, err - } - return file, nil -} - // go test -run Test_Middleware_Favicon_FileSystem func Test_Middleware_Favicon_FileSystem(t *testing.T) { app := fiber.New() app.Use(New(Config{ - File: "../../.github/testdata/favicon.ico", - FileSystem: mockFS{}, + File: "favicon.ico", + FileSystem: os.DirFS("../../.github/testdata"), })) resp, err := app.Test(httptest.NewRequest("GET", "/favicon.ico", nil))