2020-09-13 11:20:11 +02:00
|
|
|
package favicon
|
2020-06-08 05:48:40 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2020-09-13 11:20:11 +02:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2020-09-14 12:12:29 +02:00
|
|
|
"github.com/gofiber/fiber/v2/utils"
|
2020-06-30 00:50:50 +02:00
|
|
|
"github.com/valyala/fasthttp"
|
2020-06-08 05:48:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// go test -run Test_Middleware_Favicon
|
|
|
|
func Test_Middleware_Favicon(t *testing.T) {
|
|
|
|
app := fiber.New()
|
|
|
|
|
2020-09-13 11:20:11 +02:00
|
|
|
app.Use(New())
|
2020-06-08 05:48:40 +02:00
|
|
|
|
2020-09-13 11:20:11 +02:00
|
|
|
app.Get("/", func(c *fiber.Ctx) error {
|
|
|
|
return nil
|
|
|
|
})
|
2020-06-08 05:48:40 +02:00
|
|
|
|
2020-07-07 17:00:35 +08:00
|
|
|
// Skip Favicon middleware
|
|
|
|
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
|
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
|
|
|
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode, "Status code")
|
|
|
|
|
|
|
|
resp, err = app.Test(httptest.NewRequest("GET", "/favicon.ico", nil))
|
2020-06-08 05:48:40 +02:00
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
2020-07-07 17:00:35 +08:00
|
|
|
utils.AssertEqual(t, fiber.StatusNoContent, resp.StatusCode, "Status code")
|
2020-06-08 05:48:40 +02:00
|
|
|
|
|
|
|
resp, err = app.Test(httptest.NewRequest("OPTIONS", "/favicon.ico", nil))
|
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
2020-07-07 17:00:35 +08:00
|
|
|
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode, "Status code")
|
2020-06-08 05:48:40 +02:00
|
|
|
|
|
|
|
resp, err = app.Test(httptest.NewRequest("PUT", "/favicon.ico", nil))
|
|
|
|
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
2020-07-07 17:00:35 +08:00
|
|
|
utils.AssertEqual(t, fiber.StatusMethodNotAllowed, resp.StatusCode, "Status code")
|
2020-06-08 05:48:40 +02:00
|
|
|
utils.AssertEqual(t, "GET, HEAD, OPTIONS", resp.Header.Get(fiber.HeaderAllow))
|
|
|
|
}
|
2020-06-30 00:50:50 +02:00
|
|
|
|
2020-07-07 17:00:35 +08:00
|
|
|
// go test -run Test_Middleware_Favicon_Not_Found
|
|
|
|
func Test_Middleware_Favicon_Not_Found(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err == nil {
|
|
|
|
t.Fatal("should cache panic")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-09-13 11:20:11 +02:00
|
|
|
fiber.New().Use(New(Config{
|
|
|
|
File: "non-exist.ico",
|
|
|
|
}))
|
2020-07-07 17:00:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// go test -run Test_Middleware_Favicon_Found
|
|
|
|
func Test_Middleware_Favicon_Found(t *testing.T) {
|
|
|
|
app := fiber.New()
|
|
|
|
|
2020-09-13 11:20:11 +02:00
|
|
|
app.Use(New(Config{
|
|
|
|
File: "../../.github/testdata/favicon.ico",
|
|
|
|
}))
|
2020-07-07 17:00:35 +08:00
|
|
|
|
2020-09-13 11:20:11 +02:00
|
|
|
app.Get("/", func(c *fiber.Ctx) error {
|
|
|
|
return nil
|
|
|
|
})
|
2020-07-07 17:00:35 +08:00
|
|
|
|
|
|
|
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))
|
2020-10-05 08:35:29 +02:00
|
|
|
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()
|
|
|
|
|
|
|
|
app.Use(New(Config{
|
|
|
|
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")
|
|
|
|
utils.AssertEqual(t, "image/x-icon", resp.Header.Get(fiber.HeaderContentType))
|
|
|
|
utils.AssertEqual(t, "public, max-age=100", resp.Header.Get(fiber.HeaderCacheControl), "CacheControl Control")
|
2020-07-07 17:00:35 +08:00
|
|
|
}
|
|
|
|
|
2020-06-30 00:50:50 +02:00
|
|
|
// go test -v -run=^$ -bench=Benchmark_Middleware_Favicon -benchmem -count=4
|
|
|
|
func Benchmark_Middleware_Favicon(b *testing.B) {
|
|
|
|
app := fiber.New()
|
2020-09-13 11:20:11 +02:00
|
|
|
app.Use(New())
|
|
|
|
app.Get("/", func(c *fiber.Ctx) error {
|
|
|
|
return nil
|
|
|
|
})
|
2020-06-30 00:50:50 +02:00
|
|
|
handler := app.Handler()
|
|
|
|
|
|
|
|
c := &fasthttp.RequestCtx{}
|
|
|
|
c.Request.SetRequestURI("/")
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
handler(c)
|
|
|
|
}
|
|
|
|
}
|
2020-09-16 09:44:05 +08:00
|
|
|
|
|
|
|
// go test -run Test_Favicon_Next
|
|
|
|
func Test_Favicon_Next(t *testing.T) {
|
2020-09-17 13:41:06 +08:00
|
|
|
app := fiber.New()
|
2020-09-16 09:44:05 +08:00
|
|
|
app.Use(New(Config{
|
|
|
|
Next: func(_ *fiber.Ctx) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode)
|
|
|
|
}
|