Skip to content

Commit

Permalink
Merge pull request #1189 from dechristopher/master
Browse files Browse the repository at this point in the history
🔥 Add optional FileSystem config to middleware/favicon
  • Loading branch information
kiyonlin committed Feb 26, 2021
2 parents 57cfb50 + 13c4245 commit 5675716
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
27 changes: 23 additions & 4 deletions middleware/favicon/favicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package favicon

import (
"io/ioutil"
"net/http"
"strconv"

"github.com/gofiber/fiber/v2"
Expand All @@ -17,12 +18,18 @@ type Config struct {
// File holds the path to an actual favicon that will be cached
//
// Optional. Default: ""
File string
File string `json:"file"`

// FileSystem is an optional alternate filesystem to search for the favicon in.
// An example of this could be an embedded or network filesystem
//
// Optional. Default: nil
FileSystem http.FileSystem `json:"-"`

// CacheControl defines how the Cache-Control header in the response should be set
//
// Optional. Default: "public, max-age=31536000"
CacheControl string
CacheControl string `json:"cache_control"`
}

// ConfigDefault is the default config
Expand Down Expand Up @@ -66,9 +73,21 @@ func New(config ...Config) fiber.Handler {
iconLen string
)
if cfg.File != "" {
if icon, err = ioutil.ReadFile(cfg.File); err != nil {
panic(err)
// read from configured filesystem if present
if cfg.FileSystem != nil {
f, err := cfg.FileSystem.Open(cfg.File)
if err != nil {
panic(err)
}
if icon, err = ioutil.ReadAll(f); err != nil {
panic(err)
}
} else {
if icon, err = ioutil.ReadFile(cfg.File); err != nil {
panic(err)
}
}

iconLen = strconv.Itoa(len(icon))
}

Expand Down
42 changes: 41 additions & 1 deletion middleware/favicon/favicon_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package favicon

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

"github.com/valyala/fasthttp"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"github.com/valyala/fasthttp"
)

// go test -run Test_Middleware_Favicon
Expand Down Expand Up @@ -71,6 +75,41 @@ 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{},
}))

resp, err := app.Test(httptest.NewRequest("GET", "/favicon.ico", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode, "Status code")
utils.AssertEqual(t, "image/x-icon", resp.Header.Get(fiber.HeaderContentType))
utils.AssertEqual(t, "public, max-age=31536000", resp.Header.Get(fiber.HeaderCacheControl), "CacheControl Control")
}

// go test -run Test_Middleware_Favicon_CacheControl
func Test_Middleware_Favicon_CacheControl(t *testing.T) {
app := fiber.New()
Expand All @@ -79,6 +118,7 @@ func Test_Middleware_Favicon_CacheControl(t *testing.T) {
CacheControl: "public, max-age=100",
File: "../../.github/testdata/favicon.ico",
}))

resp, err := app.Test(httptest.NewRequest("GET", "/favicon.ico", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode, "Status code")
Expand Down

0 comments on commit 5675716

Please sign in to comment.