Skip to content

Commit

Permalink
Fix wrong status code (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioppermann committed Oct 28, 2022
1 parent 35c5c9f commit 4334105
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 18 deletions.
25 changes: 7 additions & 18 deletions http/middleware/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,18 @@ func NewWithConfig(config Config) echo.MiddlewareFunc {

if req.Method != "GET" {
res.Header().Set("X-Cache", "SKIP ONLYGET")

if err := next(c); err != nil {
c.Error(err)
}

return nil
return next(c)
}

res.Header().Set("Cache-Control", fmt.Sprintf("max-age=%.0f", config.Cache.TTL().Seconds()))

key := strings.TrimPrefix(req.URL.Path, config.Prefix)

if !config.Cache.IsExtensionCacheable(path.Ext(req.URL.Path)) {
res.Header().Set("X-Cache", "SKIP EXT")

if err := next(c); err != nil {
c.Error(err)
}

return nil
return next(c)
}

if obj, expireIn, _ := config.Cache.Get(key); obj == nil {
// cache miss

writer := res.Writer

w := &cacheWriter{
Expand All @@ -105,13 +92,15 @@ func NewWithConfig(config Config) echo.MiddlewareFunc {

if res.Status != 200 {
res.Header().Set("X-Cache", "SKIP NOTOK")
res.Writer.WriteHeader(res.Status)
return nil
}

size := uint64(w.body.Len())

if !config.Cache.IsSizeCacheable(size) {
res.Header().Set("X-Cache", "SKIP TOOBIG")
res.Writer.WriteHeader(res.Status)
return nil
}

Expand All @@ -123,11 +112,13 @@ func NewWithConfig(config Config) echo.MiddlewareFunc {

if err := config.Cache.Put(key, o, size); err != nil {
res.Header().Set("X-Cache", "SKIP TOOBIG")
res.Writer.WriteHeader(res.Status)
return nil
}

res.Header().Set("Cache-Control", fmt.Sprintf("max-age=%.0f", expireIn.Seconds()))
res.Header().Set("X-Cache", "MISS")
res.Writer.WriteHeader(res.Status)
} else {
// cache hit
o := obj.(*cacheObject)
Expand Down Expand Up @@ -190,7 +181,5 @@ func (w *cacheWriter) WriteHeader(code int) {
}

func (w *cacheWriter) Write(body []byte) (int, error) {
n, err := w.body.Write(body)

return n, err
return w.body.Write(body)
}
100 changes: 100 additions & 0 deletions http/middleware/cache/cache_test.go
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"))
}

0 comments on commit 4334105

Please sign in to comment.