Skip to content

Commit

Permalink
support disablecache
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed May 19, 2024
1 parent ad70fe6 commit 545a5eb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions middleware/static/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func New(root string, cfg ...Config) fiber.Handler {
Compress: config.Compress,
CompressedFileSuffix: c.App().Config().CompressedFileSuffix,
CacheDuration: config.CacheDuration,
SkipCache: config.CacheDuration < 0,
IndexNames: []string{"index.html"},
PathNotFound: func(fctx *fasthttp.RequestCtx) {
fctx.Response.SetStatusCode(fiber.StatusNotFound)
Expand Down
36 changes: 36 additions & 0 deletions middleware/static/static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func Test_Static_Direct(t *testing.T) {
body, err = io.ReadAll(resp.Body)
require.NoError(t, err)
require.Contains(t, string(body), "test_routes")
t.Fail()
}

// go test -run Test_Static_MaxAge
Expand Down Expand Up @@ -116,6 +117,41 @@ func Test_Static_Custom_CacheControl(t *testing.T) {
require.Equal(t, "", normalResp.Header.Get(fiber.HeaderCacheControl), "CacheControl Control")
}

func Test_Static_Disable_Cache(t *testing.T) {
t.Parallel()
app := fiber.New()

file, err := os.Create("../../.github/test.txt")
require.NoError(t, err)
_, err = file.WriteString("Hello, World!")
require.NoError(t, err)

// Remove the file even if the test fails
defer os.Remove("../../.github/test.txt")

Check failure on line 130 in middleware/static/static_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `os.Remove` is not checked (errcheck)

app.Get("/*", New("../../.github/", Config{
CacheDuration: -1,
}))

resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/test.txt", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, "", resp.Header.Get(fiber.HeaderCacheControl), "CacheControl Control")

body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Contains(t, string(body), "Hello, World!")

require.NoError(t, os.Remove("../../.github/test.txt"))

resp, err = app.Test(httptest.NewRequest(fiber.MethodGet, "/test.txt", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, "", resp.Header.Get(fiber.HeaderCacheControl), "CacheControl Control")

body, err = io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, string(body), "Cannot GET /test.txt")

Check failure on line 152 in middleware/static/static_test.go

View workflow job for this annotation

GitHub Actions / lint

expected-actual: need to reverse actual and expected values (testifylint)
}

// go test -run Test_Static_Download
func Test_Static_Download(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit 545a5eb

Please sign in to comment.