mirror of
https://github.com/gofiber/fiber.git
synced 2025-05-15 20:21:09 +00:00
* Fixes for some of the failing tests * Add readiness check to serverStart() * Use net/http client for tests listen test * Use different key for this test * Run Proxy Middleware tests in parallel. Add nil checks for potential issues pointed by nilaway * Enable parallel client tests * Do not run timing sensitive tests in parallel * Remove TODO * Revert Test_Proxy_DoTimeout_Timeout, and remove t.Parallel() for it * Do not calculate favicon len on each handler call * Revert logic change * Increase timeout of SaveFile tests * Do not run time sensitive tests in parallel * The Agent can't be run in parallel * Do not run time sensitive tests in parallel * Fixes based on uber/nilaway * Revert change to Client test * Run parallel * Update client_test.go * Update client_test.go * Update cache_test.go * Update cookiejar_test.go * Remove parallel for test using timeouts * Remove t.Parallel() from logger middleware tests * Do not use testify.require in a goroutine * Fix import, and update golangci-lint * Remove changes to template_chain.go * Run more tests in parallel * Add more parallel tests * Add more parallel tests * SetLogger can't run in parallel * Run more tests in parallel, fix issue with goroutine in limiter middleware * Update internal/storage/memory, add more benchmarks * Increase sleep for csrf test by 100 milliseconds. Implement asserted and parallel benchmarks for Session middleware * Add 100 milliseconds to sleep during test * Revert name change * fix: Inconsistent and flaky unit-tests * fix: Inconsistent and flaky unit-tests * fix: Inconsistent and flaky unit-tests * fix: Inconsistent and flaky unit-tests * fix: Inconsistent and flaky unit-tests * fix: Inconsistent and flaky unit-tests * fix: Inconsistent and flaky unit-tests * fix: Inconsistent and flaky unit-tests * fix: Inconsistent and flaky unit-tests * fix: Inconsistent and flaky unit-tests --------- Co-authored-by: M. Efe Çetin <efectn@protonmail.com> Co-authored-by: René <rene@gofiber.io>
189 lines
5.3 KiB
Go
189 lines
5.3 KiB
Go
package favicon
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
)
|
|
|
|
// go test -run Test_Middleware_Favicon
|
|
func Test_Middleware_Favicon(t *testing.T) {
|
|
t.Parallel()
|
|
app := fiber.New()
|
|
|
|
app.Use(New())
|
|
|
|
app.Get("/", func(_ fiber.Ctx) error {
|
|
return nil
|
|
})
|
|
|
|
// Skip Favicon middleware
|
|
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
|
|
require.NoError(t, err, "app.Test(req)")
|
|
require.Equal(t, fiber.StatusOK, resp.StatusCode, "Status code")
|
|
|
|
resp, err = app.Test(httptest.NewRequest(fiber.MethodGet, "/favicon.ico", nil))
|
|
require.NoError(t, err, "app.Test(req)")
|
|
require.Equal(t, fiber.StatusNoContent, resp.StatusCode, "Status code")
|
|
|
|
resp, err = app.Test(httptest.NewRequest(fiber.MethodOptions, "/favicon.ico", nil))
|
|
require.NoError(t, err, "app.Test(req)")
|
|
require.Equal(t, fiber.StatusOK, resp.StatusCode, "Status code")
|
|
|
|
resp, err = app.Test(httptest.NewRequest(fiber.MethodPut, "/favicon.ico", nil))
|
|
require.NoError(t, err, "app.Test(req)")
|
|
require.Equal(t, fiber.StatusMethodNotAllowed, resp.StatusCode, "Status code")
|
|
require.Equal(t, "GET, HEAD, OPTIONS", resp.Header.Get(fiber.HeaderAllow))
|
|
}
|
|
|
|
// go test -run Test_Middleware_Favicon_Not_Found
|
|
func Test_Middleware_Favicon_Not_Found(t *testing.T) {
|
|
t.Parallel()
|
|
defer func() {
|
|
if err := recover(); err == nil {
|
|
t.Error("should cache panic")
|
|
return
|
|
}
|
|
}()
|
|
|
|
fiber.New().Use(New(Config{
|
|
File: "non-exist.ico",
|
|
}))
|
|
}
|
|
|
|
// go test -run Test_Middleware_Favicon_Found
|
|
func Test_Middleware_Favicon_Found(t *testing.T) {
|
|
t.Parallel()
|
|
app := fiber.New()
|
|
|
|
app.Use(New(Config{
|
|
File: "../../.github/testdata/favicon.ico",
|
|
}))
|
|
|
|
app.Get("/", func(_ fiber.Ctx) error {
|
|
return nil
|
|
})
|
|
|
|
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/favicon.ico", nil))
|
|
require.NoError(t, err, "app.Test(req)")
|
|
require.Equal(t, fiber.StatusOK, resp.StatusCode, "Status code")
|
|
require.Equal(t, "image/x-icon", resp.Header.Get(fiber.HeaderContentType))
|
|
require.Equal(t, "public, max-age=31536000", resp.Header.Get(fiber.HeaderCacheControl), "CacheControl Control")
|
|
}
|
|
|
|
// go test -run Test_Custom_Favicon_Url
|
|
func Test_Custom_Favicon_URL(t *testing.T) {
|
|
app := fiber.New()
|
|
const customURL = "/favicon.svg"
|
|
app.Use(New(Config{
|
|
File: "../../.github/testdata/favicon.ico",
|
|
URL: customURL,
|
|
}))
|
|
|
|
app.Get("/", func(_ fiber.Ctx) error {
|
|
return nil
|
|
})
|
|
|
|
resp, err := app.Test(httptest.NewRequest(http.MethodGet, customURL, nil))
|
|
|
|
require.NoError(t, err, "app.Test(req)")
|
|
require.Equal(t, fiber.StatusOK, resp.StatusCode, "Status code")
|
|
require.Equal(t, "image/x-icon", resp.Header.Get(fiber.HeaderContentType))
|
|
}
|
|
|
|
// go test -run Test_Custom_Favicon_Data
|
|
func Test_Custom_Favicon_Data(t *testing.T) {
|
|
data, err := os.ReadFile("../../.github/testdata/favicon.ico")
|
|
require.NoError(t, err)
|
|
|
|
app := fiber.New()
|
|
|
|
app.Use(New(Config{
|
|
Data: data,
|
|
}))
|
|
|
|
app.Get("/", func(_ fiber.Ctx) error {
|
|
return nil
|
|
})
|
|
|
|
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/favicon.ico", nil))
|
|
require.NoError(t, err, "app.Test(req)")
|
|
require.Equal(t, fiber.StatusOK, resp.StatusCode, "Status code")
|
|
require.Equal(t, "image/x-icon", resp.Header.Get(fiber.HeaderContentType))
|
|
require.Equal(t, "public, max-age=31536000", resp.Header.Get(fiber.HeaderCacheControl), "CacheControl Control")
|
|
}
|
|
|
|
// go test -run Test_Middleware_Favicon_FileSystem
|
|
func Test_Middleware_Favicon_FileSystem(t *testing.T) {
|
|
t.Parallel()
|
|
app := fiber.New()
|
|
|
|
app.Use(New(Config{
|
|
File: "favicon.ico",
|
|
FileSystem: os.DirFS("../../.github/testdata"),
|
|
}))
|
|
|
|
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/favicon.ico", nil))
|
|
require.NoError(t, err, "app.Test(req)")
|
|
require.Equal(t, fiber.StatusOK, resp.StatusCode, "Status code")
|
|
require.Equal(t, "image/x-icon", resp.Header.Get(fiber.HeaderContentType))
|
|
require.Equal(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) {
|
|
t.Parallel()
|
|
app := fiber.New()
|
|
|
|
app.Use(New(Config{
|
|
CacheControl: "public, max-age=100",
|
|
File: "../../.github/testdata/favicon.ico",
|
|
}))
|
|
|
|
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/favicon.ico", nil))
|
|
require.NoError(t, err, "app.Test(req)")
|
|
require.Equal(t, fiber.StatusOK, resp.StatusCode, "Status code")
|
|
require.Equal(t, "image/x-icon", resp.Header.Get(fiber.HeaderContentType))
|
|
require.Equal(t, "public, max-age=100", resp.Header.Get(fiber.HeaderCacheControl), "CacheControl Control")
|
|
}
|
|
|
|
// go test -v -run=^$ -bench=Benchmark_Middleware_Favicon -benchmem -count=4
|
|
func Benchmark_Middleware_Favicon(b *testing.B) {
|
|
app := fiber.New()
|
|
app.Use(New())
|
|
app.Get("/", func(_ fiber.Ctx) error {
|
|
return nil
|
|
})
|
|
handler := app.Handler()
|
|
|
|
c := &fasthttp.RequestCtx{}
|
|
c.Request.SetRequestURI("/")
|
|
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for n := 0; n < b.N; n++ {
|
|
handler(c)
|
|
}
|
|
}
|
|
|
|
// go test -run Test_Favicon_Next
|
|
func Test_Favicon_Next(t *testing.T) {
|
|
t.Parallel()
|
|
app := fiber.New()
|
|
app.Use(New(Config{
|
|
Next: func(_ fiber.Ctx) bool {
|
|
return true
|
|
},
|
|
}))
|
|
|
|
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
|
|
require.NoError(t, err)
|
|
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)
|
|
}
|