-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35c5c9f
commit 4334105
Showing
2 changed files
with
107 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package cache | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/datarhei/core/v16/http/cache" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCache(t *testing.T) { | ||
c, err := cache.NewLRUCache(cache.LRUConfig{ | ||
TTL: 300 * time.Second, | ||
MaxSize: 0, | ||
MaxFileSize: 16, | ||
AllowExtensions: []string{".js"}, | ||
BlockExtensions: []string{".ts"}, | ||
Logger: nil, | ||
}) | ||
|
||
require.NoError(t, err) | ||
|
||
e := echo.New() | ||
req := httptest.NewRequest(http.MethodGet, "/found.js", nil) | ||
rec := httptest.NewRecorder() | ||
ctx := e.NewContext(req, rec) | ||
|
||
handler := NewWithConfig(Config{ | ||
Cache: c, | ||
})(func(c echo.Context) error { | ||
if c.Request().URL.Path == "/found.js" { | ||
c.Response().Write([]byte("test")) | ||
} else if c.Request().URL.Path == "/toobig.js" { | ||
c.Response().Write([]byte("testtesttesttesttest")) | ||
} else if c.Request().URL.Path == "/blocked.ts" { | ||
c.Response().Write([]byte("blocked")) | ||
} | ||
|
||
c.Response().WriteHeader(http.StatusNotFound) | ||
return nil | ||
}) | ||
|
||
handler(ctx) | ||
|
||
require.Equal(t, "test", rec.Body.String()) | ||
require.Equal(t, 200, rec.Result().StatusCode) | ||
require.Equal(t, "MISS", rec.Result().Header.Get("x-cache")) | ||
|
||
rec = httptest.NewRecorder() | ||
ctx = e.NewContext(req, rec) | ||
|
||
handler(ctx) | ||
|
||
require.Equal(t, "test", rec.Body.String()) | ||
require.Equal(t, 200, rec.Result().StatusCode) | ||
require.Equal(t, "HIT", rec.Result().Header.Get("x-cache")[:3]) | ||
|
||
req = httptest.NewRequest(http.MethodGet, "/notfound.js", nil) | ||
rec = httptest.NewRecorder() | ||
ctx = e.NewContext(req, rec) | ||
|
||
handler(ctx) | ||
|
||
require.Equal(t, 404, rec.Result().StatusCode) | ||
require.Equal(t, "SKIP NOTOK", rec.Result().Header.Get("x-cache")) | ||
|
||
req = httptest.NewRequest(http.MethodGet, "/toobig.js", nil) | ||
rec = httptest.NewRecorder() | ||
ctx = e.NewContext(req, rec) | ||
|
||
handler(ctx) | ||
|
||
require.Equal(t, "testtesttesttesttest", rec.Body.String()) | ||
require.Equal(t, 200, rec.Result().StatusCode) | ||
require.Equal(t, "SKIP TOOBIG", rec.Result().Header.Get("x-cache")) | ||
|
||
req = httptest.NewRequest(http.MethodGet, "/blocked.ts", nil) | ||
rec = httptest.NewRecorder() | ||
ctx = e.NewContext(req, rec) | ||
|
||
handler(ctx) | ||
|
||
require.Equal(t, "blocked", rec.Body.String()) | ||
require.Equal(t, 200, rec.Result().StatusCode) | ||
require.Equal(t, "SKIP EXT", rec.Result().Header.Get("x-cache")) | ||
|
||
req = httptest.NewRequest(http.MethodPost, "/found.js", nil) | ||
rec = httptest.NewRecorder() | ||
ctx = e.NewContext(req, rec) | ||
|
||
handler(ctx) | ||
|
||
require.Equal(t, "test", rec.Body.String()) | ||
require.Equal(t, 200, rec.Result().StatusCode) | ||
require.Equal(t, "SKIP ONLYGET", rec.Result().Header.Get("x-cache")) | ||
} |