Skip to content

Commit

Permalink
test(response): add test for cache in response
Browse files Browse the repository at this point in the history
  • Loading branch information
aldor007 committed Apr 21, 2019
1 parent 8b5f578 commit d818e17
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 6 deletions.
9 changes: 5 additions & 4 deletions pkg/cache/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (
)

type mockValue struct {
value interface{}
value interface{}
expiration time.Duration
}
var internal map[string]mockValue

var internal map[string]mockValue

type redisMock struct {
}

Expand Down Expand Up @@ -56,9 +58,8 @@ func TestRedisCache_Get_and_Set(t *testing.T) {
assert.Equal(t, res2.StatusCode, 300)
assert.Equal(t, res2.GetTTL(), 60)


internalItem := internal[obj.GetResponseCacheKey()]
assert.Equal(t, internalItem.expiration, time.Second * time.Duration(res.GetTTL()))
assert.Equal(t, internalItem.expiration, time.Second*time.Duration(res.GetTTL()))

r.Delete(obj)
_, err = r.Get(obj)
Expand Down
33 changes: 33 additions & 0 deletions pkg/object/file_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,39 @@ func TestNewFileUnknownPreset(t *testing.T) {
assert.Nil(t, err, "Unexpected to have error when parsing path")
}

func TestObjectType(t *testing.T) {
mortConfig := &config.Config{}
mortConfig.Load("testdata/bucket-transform-query-parent-storage.yml")
obj, err := NewFileObject(pathToURL("/bucket/parent.jpg?width=100&operation=watermark&opacity=0.5&minAmpl=0.5&image=http://www&position=top-left"), mortConfig)
assert.Nil(t, err)

assert.Equal(t, obj.Type(), "transform")
assert.Equal(t, obj.Parent.Type(), "parent")
}

func TestObjectCacheKeyQuery(t *testing.T) {
mortConfig := &config.Config{}
mortConfig.Load("testdata/bucket-transform-query-parent-storage.yml")
obj, err := NewFileObject(pathToURL("/bucket/parent.jpg?width=100&operation=watermark&opacity=0.5&minAmpl=0.5&image=http://www&position=top-left"), mortConfig)
assert.Nil(t, err)

assert.Equal(t, obj.GetResponseCacheKey(), "/parent.jpg/90e8c676de60865efa843590826a08e1")
}

func TestObjectCacheKeyPreset(t *testing.T) {
mortConfig := config.GetInstance()
mortConfig.Load("testdata/bucket-transform-hash.yml")
obj, err := NewFileObject(pathToURL("/bucket/width/bucket/height/bucket/parent.jpg"), mortConfig)

assert.Nil(t, err, "Unexpected to have error when parsing path")

assert.Equal(t, "/6ca/hei/height-bucket-parent.jpg-6ca0dabe9909875a", obj.GetResponseCacheKey())

obj.Range = "bytes=1-10"

assert.Equal(t, "/6ca/hei/height-bucket-parent.jpg-6ca0dabe9909875abytes=1-10", obj.GetResponseCacheKey())
}

func BenchmarkNewFileObject(b *testing.B) {

benchmarks := []struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (r *RequestProcessor) process(req *http.Request, obj *object.FileObject) *r
res = updateHeaders(obj, r.handleGET(req, obj))
}

if res.IsCachable() && res.ContentLength != -1 && res.ContentLength < r.serverConfig.Cache.MaxCacheItemSize {
if res.IsCacheable() && res.ContentLength != -1 && res.ContentLength < r.serverConfig.Cache.MaxCacheItemSize {
resCpy, err := res.Copy()
if err == nil {
go func() {
Expand Down
3 changes: 2 additions & 1 deletion pkg/response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func New(statusCode int, body io.ReadCloser) *Response {
res.ContentLength = -1
}
res.Headers = make(http.Header)

return &res
}

Expand Down Expand Up @@ -277,7 +278,7 @@ func (r *Response) CopyHeadersFrom(src *Response) {
r.errorValue = src.errorValue
}

func (r *Response) IsCachable() bool {
func (r *Response) IsCacheable() bool {
r.parseCacheHeaders()
return r.StatusCode > 199 && r.StatusCode < 299 && r.cachable
}
Expand Down
36 changes: 36 additions & 0 deletions pkg/response/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/aldor007/mort/pkg/object"
"github.com/aldor007/mort/pkg/transforms"
"github.com/stretchr/testify/assert"
"github.com/vmihailenco/msgpack"
"io"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -219,6 +220,41 @@ func TestIsRangeOrCond(t *testing.T) {
assert.False(t, helpers.IsRangeOrCondition(req))
}

func TestResponse_IsCachable(t *testing.T) {
res := NewNoContent(200)

assert.False(t, res.IsCacheable())

res.Headers.Set("cache-control", "public, max-age=600")

assert.True(t, res.IsCacheable())
assert.Equal(t, res.GetTTL(), 600)
}

func TestResponse_DecodeMsgpack(t *testing.T) {
res := NewString(400, "testuje")
res.Headers.Set("etag", "md5")
res.Headers.Set("cache-control", "private")
res.ReadBody()
buf, err := msgpack.Marshal(res)

assert.Nil(t, err)

var resMsg Response
err = msgpack.Unmarshal(buf, &resMsg)

assert.Nil(t, err)

b, err := resMsg.ReadBody()
assert.Nil(t, err)

assert.Equal(t, resMsg.StatusCode, 400)
assert.Equal(t, resMsg.Headers.Get("etag"), "md5")
assert.Equal(t, resMsg.GetTTL(), -1)
assert.Equal(t, string(b), "testuje")

}

func BenchmarkNewBuf(b *testing.B) {
buf := make([]byte, 1000)
for i := 0; i < b.N; i++ {
Expand Down

0 comments on commit d818e17

Please sign in to comment.