1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-24 02:04:12 +00:00
fiber/middleware/favicon_test.go

50 lines
1.3 KiB
Go
Raw Normal View History

2020-06-08 05:48:40 +02:00
package middleware
import (
"net/http/httptest"
"testing"
"github.com/gofiber/fiber"
"github.com/gofiber/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()
app.Use(Favicon())
2020-06-30 00:50:50 +02:00
app.Get("/", func(c *fiber.Ctx) {})
2020-06-08 05:48:40 +02:00
resp, err := app.Test(httptest.NewRequest("GET", "/favicon.ico", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 204, resp.StatusCode, "Status code")
resp, err = app.Test(httptest.NewRequest("OPTIONS", "/favicon.ico", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
resp, err = app.Test(httptest.NewRequest("PUT", "/favicon.ico", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 405, resp.StatusCode, "Status code")
utils.AssertEqual(t, "GET, HEAD, OPTIONS", resp.Header.Get(fiber.HeaderAllow))
}
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()
app.Use(Favicon())
app.Get("/", func(c *fiber.Ctx) {})
handler := app.Handler()
c := &fasthttp.RequestCtx{}
c.Request.SetRequestURI("/")
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
handler(c)
}
}