github.com/gofiber/fiber/v2@v2.47.0/middleware/favicon/favicon_test.go (about)

     1  //nolint:bodyclose // Much easier to just ignore memory leaks in tests
     2  package favicon
     3  
     4  import (
     5  	"fmt"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"os"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/gofiber/fiber/v2"
    13  	"github.com/gofiber/fiber/v2/utils"
    14  
    15  	"github.com/valyala/fasthttp"
    16  )
    17  
    18  // go test -run Test_Middleware_Favicon
    19  func Test_Middleware_Favicon(t *testing.T) {
    20  	t.Parallel()
    21  	app := fiber.New()
    22  
    23  	app.Use(New())
    24  
    25  	app.Get("/", func(c *fiber.Ctx) error {
    26  		return nil
    27  	})
    28  
    29  	// Skip Favicon middleware
    30  	resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
    31  	utils.AssertEqual(t, nil, err, "app.Test(req)")
    32  	utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode, "Status code")
    33  
    34  	resp, err = app.Test(httptest.NewRequest(fiber.MethodGet, "/favicon.ico", nil))
    35  	utils.AssertEqual(t, nil, err, "app.Test(req)")
    36  	utils.AssertEqual(t, fiber.StatusNoContent, resp.StatusCode, "Status code")
    37  
    38  	resp, err = app.Test(httptest.NewRequest(fiber.MethodOptions, "/favicon.ico", nil))
    39  	utils.AssertEqual(t, nil, err, "app.Test(req)")
    40  	utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode, "Status code")
    41  
    42  	resp, err = app.Test(httptest.NewRequest(fiber.MethodPut, "/favicon.ico", nil))
    43  	utils.AssertEqual(t, nil, err, "app.Test(req)")
    44  	utils.AssertEqual(t, fiber.StatusMethodNotAllowed, resp.StatusCode, "Status code")
    45  	utils.AssertEqual(t, strings.Join([]string{fiber.MethodGet, fiber.MethodHead, fiber.MethodOptions}, ", "), resp.Header.Get(fiber.HeaderAllow))
    46  }
    47  
    48  // go test -run Test_Middleware_Favicon_Not_Found
    49  func Test_Middleware_Favicon_Not_Found(t *testing.T) {
    50  	t.Parallel()
    51  	defer func() {
    52  		if err := recover(); err == nil {
    53  			t.Fatal("should cache panic")
    54  		}
    55  	}()
    56  
    57  	fiber.New().Use(New(Config{
    58  		File: "non-exist.ico",
    59  	}))
    60  }
    61  
    62  // go test -run Test_Middleware_Favicon_Found
    63  func Test_Middleware_Favicon_Found(t *testing.T) {
    64  	t.Parallel()
    65  	app := fiber.New()
    66  
    67  	app.Use(New(Config{
    68  		File: "../../.github/testdata/favicon.ico",
    69  	}))
    70  
    71  	app.Get("/", func(c *fiber.Ctx) error {
    72  		return nil
    73  	})
    74  
    75  	resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/favicon.ico", nil))
    76  	utils.AssertEqual(t, nil, err, "app.Test(req)")
    77  	utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode, "Status code")
    78  	utils.AssertEqual(t, "image/x-icon", resp.Header.Get(fiber.HeaderContentType))
    79  	utils.AssertEqual(t, "public, max-age=31536000", resp.Header.Get(fiber.HeaderCacheControl), "CacheControl Control")
    80  }
    81  
    82  // go test -run Test_Custom_Favicon_Url
    83  func Test_Custom_Favicon_Url(t *testing.T) {
    84  	app := fiber.New()
    85  	const customURL = "/favicon.svg"
    86  	app.Use(New(Config{
    87  		File: "../../.github/testdata/favicon.ico",
    88  		URL:  customURL,
    89  	}))
    90  
    91  	app.Get("/", func(c *fiber.Ctx) error {
    92  		return nil
    93  	})
    94  
    95  	resp, err := app.Test(httptest.NewRequest(http.MethodGet, customURL, nil))
    96  
    97  	utils.AssertEqual(t, nil, err, "app.Test(req)")
    98  	utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode, "Status code")
    99  	utils.AssertEqual(t, "image/x-icon", resp.Header.Get(fiber.HeaderContentType))
   100  }
   101  
   102  // mockFS wraps local filesystem for the purposes of
   103  // Test_Middleware_Favicon_FileSystem located below
   104  // TODO use os.Dir if fiber upgrades to 1.16
   105  type mockFS struct{}
   106  
   107  func (mockFS) Open(name string) (http.File, error) {
   108  	if name == "/" {
   109  		name = "."
   110  	} else {
   111  		name = strings.TrimPrefix(name, "/")
   112  	}
   113  	file, err := os.Open(name) //nolint:gosec // We're in a test func, so this is fine
   114  	if err != nil {
   115  		return nil, fmt.Errorf("failed to open: %w", err)
   116  	}
   117  	return file, nil
   118  }
   119  
   120  // go test -run Test_Middleware_Favicon_FileSystem
   121  func Test_Middleware_Favicon_FileSystem(t *testing.T) {
   122  	t.Parallel()
   123  	app := fiber.New()
   124  
   125  	app.Use(New(Config{
   126  		File:       "../../.github/testdata/favicon.ico",
   127  		FileSystem: mockFS{},
   128  	}))
   129  
   130  	resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/favicon.ico", nil))
   131  	utils.AssertEqual(t, nil, err, "app.Test(req)")
   132  	utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode, "Status code")
   133  	utils.AssertEqual(t, "image/x-icon", resp.Header.Get(fiber.HeaderContentType))
   134  	utils.AssertEqual(t, "public, max-age=31536000", resp.Header.Get(fiber.HeaderCacheControl), "CacheControl Control")
   135  }
   136  
   137  // go test -run Test_Middleware_Favicon_CacheControl
   138  func Test_Middleware_Favicon_CacheControl(t *testing.T) {
   139  	t.Parallel()
   140  	app := fiber.New()
   141  
   142  	app.Use(New(Config{
   143  		CacheControl: "public, max-age=100",
   144  		File:         "../../.github/testdata/favicon.ico",
   145  	}))
   146  
   147  	resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/favicon.ico", nil))
   148  	utils.AssertEqual(t, nil, err, "app.Test(req)")
   149  	utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode, "Status code")
   150  	utils.AssertEqual(t, "image/x-icon", resp.Header.Get(fiber.HeaderContentType))
   151  	utils.AssertEqual(t, "public, max-age=100", resp.Header.Get(fiber.HeaderCacheControl), "CacheControl Control")
   152  }
   153  
   154  // go test -v -run=^$ -bench=Benchmark_Middleware_Favicon -benchmem -count=4
   155  func Benchmark_Middleware_Favicon(b *testing.B) {
   156  	app := fiber.New()
   157  	app.Use(New())
   158  	app.Get("/", func(c *fiber.Ctx) error {
   159  		return nil
   160  	})
   161  	handler := app.Handler()
   162  
   163  	c := &fasthttp.RequestCtx{}
   164  	c.Request.SetRequestURI("/")
   165  
   166  	b.ReportAllocs()
   167  	b.ResetTimer()
   168  	for n := 0; n < b.N; n++ {
   169  		handler(c)
   170  	}
   171  }
   172  
   173  // go test -run Test_Favicon_Next
   174  func Test_Favicon_Next(t *testing.T) {
   175  	t.Parallel()
   176  	app := fiber.New()
   177  	app.Use(New(Config{
   178  		Next: func(_ *fiber.Ctx) bool {
   179  			return true
   180  		},
   181  	}))
   182  
   183  	resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
   184  	utils.AssertEqual(t, nil, err)
   185  	utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode)
   186  }