Skip to content

Commit

Permalink
♻️ v3 (enhancement): use fs.FS as Filesystem type of favicon middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Aug 7, 2022
1 parent adcb1f2 commit 738e710
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 24 deletions.
4 changes: 2 additions & 2 deletions middleware/favicon/favicon.go
Expand Up @@ -2,7 +2,7 @@ package favicon

import (
"io"
"net/http"
"io/fs"
"os"
"strconv"

Expand All @@ -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
//
Expand Down
24 changes: 2 additions & 22 deletions middleware/favicon/favicon_test.go
@@ -1,10 +1,8 @@
package favicon

import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"

"github.com/valyala/fasthttp"
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 738e710

Please sign in to comment.