Skip to content
This repository was archived by the owner on Mar 8, 2024. It is now read-only.

Commit 52f0db7

Browse files
authored
chore(test): add unit test for round trip function (#18)
* chore(cache-interactor): rename the cache interactor * chore(test): add test for round trip * feat: export public function for full control * chore: update readme example
1 parent 93b4695 commit 52f0db7

13 files changed

Lines changed: 187 additions & 44 deletions

File tree

Makefile

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1+
ifndef $(GOPATH)
2+
GOPATH=$(shell go env GOPATH)
3+
export GOPATH
4+
endif
5+
16
.PHONY: mockery-prepare
7+
8+
# Install the mockery. This command will install the mockery in the GOPATH/bin folder
29
mockery-prepare:
3-
@echo "Installing mockery"
4-
@go get -u github.com/vektra/mockery
10+
@go get github.com/vektra/mockery/.../
11+
12+
# Use the mockery to generate mock interface
13+
mockery-gen:
14+
@rm -rf ./mocks
15+
$(GOPATH)/bin/mockery --dir ./cache/ --name ICacheInteractor
16+
517

618
.PHONY: short-test
719
short-test:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Short example:
4545

4646
// Inject the HTTP Client with httpcache
4747
client := &http.Client{}
48-
err := httpcache.NewWithInmemoryCache(client, time.Second*60)
48+
_, err := httpcache.NewWithInmemoryCache(client, time.Second*60)
4949
if err != nil {
5050
log.Fatal(err)
5151
}

cache/cache.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ const (
2121
// TODO (bxcodec): Add another storage type
2222
)
2323

24-
// Interactor ...
25-
type Interactor interface {
26-
Set(key string, value CachedResponse, duration time.Duration) error
24+
// ICacheInteractor ...
25+
type ICacheInteractor interface {
26+
Set(key string, value CachedResponse) error
2727
Get(key string) (CachedResponse, error)
2828
Delete(key string) error
29+
Flush() error
2930
Origin() string
3031
}
3132

cache/inmem/inmem.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package inmem
22

33
import (
4-
"time"
5-
64
memcache "github.com/bxcodec/gotcha/cache"
75
"github.com/bxcodec/httpcache/cache"
86
)
@@ -12,14 +10,13 @@ type inmemCache struct {
1210
}
1311

1412
// NewCache will return the inmemory cache handler
15-
func NewCache(c memcache.Cache) cache.Interactor {
13+
func NewCache(c memcache.Cache) cache.ICacheInteractor {
1614
return &inmemCache{
1715
cache: c,
1816
}
1917
}
2018

21-
func (i *inmemCache) Set(key string, value cache.CachedResponse, duration time.Duration) (err error) {
22-
// TODO(bxcodec): add custom duration here based on user response result on the fly
19+
func (i *inmemCache) Set(key string, value cache.CachedResponse) (err error) {
2320
return i.cache.Set(key, value)
2421
}
2522

@@ -39,3 +36,7 @@ func (i *inmemCache) Delete(key string) (err error) {
3936
func (i *inmemCache) Origin() string {
4037
return cache.CacheStorageInMemory
4138
}
39+
40+
func (i *inmemCache) Flush() error {
41+
return i.cache.ClearCache()
42+
}

cache/inmem/inmem_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestCacheInMemory(t *testing.T) {
2626
}
2727

2828
// Try to SET item
29-
err := cacheObj.Set(testKey, testVal, time.Second*5)
29+
err := cacheObj.Set(testKey, testVal)
3030
if err != nil {
3131
t.Fatalf("expected %v, got %v", nil, err)
3232
}

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
44
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
66
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
78
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
89
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
910
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
11+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1012
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1113
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
1214
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

helper/cacheheader/directive.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ func (cd *RequestCacheDirectives) addToken(token string) error {
259259
cd.NoCache = true
260260
case HeaderNoStore:
261261
cd.NoStore = true
262-
case "no-transform":
262+
case HeaderNoTransform:
263263
cd.NoTransform = true
264264
case "only-if-cached":
265265
cd.OnlyIfCached = true
@@ -292,7 +292,7 @@ func (cd *RequestCacheDirectives) addPair(token string, v string) error {
292292
err = ErrNoCacheNoArgs
293293
case HeaderNoStore:
294294
err = ErrNoStoreNoArgs
295-
case "no-transform":
295+
case HeaderNoTransform:
296296
err = ErrNoTransformNoArgs
297297
case "only-if-cached":
298298
err = ErrOnlyIfCachedNoArgs
@@ -467,7 +467,7 @@ func (cd *ResponseCacheDirectives) addToken(token string) error {
467467
cd.NoCachePresent = true
468468
case HeaderNoStore:
469469
cd.NoStore = true
470-
case "no-transform":
470+
case HeaderNoTransform:
471471
cd.NoTransform = true
472472
case "public":
473473
cd.Public = true
@@ -520,7 +520,7 @@ func (cd *ResponseCacheDirectives) addPair(token string, v string) error {
520520
}
521521
case HeaderNoStore:
522522
err = ErrNoStoreNoArgs
523-
case "no-transform":
523+
case HeaderNoTransform:
524524
err = ErrNoTransformNoArgs
525525
case "public":
526526
err = ErrPublicNoArgs

helper/cacheheader/object.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ func CachableObject(obj *Object, rv *ObjectResults) {
8181
if !hasFreshness(obj.ReqDirectives, obj.RespDirectives, obj.RespHeaders, obj.RespExpiresHeader, obj.CacheIsPrivate) {
8282
rv.OutReasons = append(rv.OutReasons, ReasonRequestMethodPOST)
8383
}
84-
8584
case "PUT":
8685
rv.OutReasons = append(rv.OutReasons, ReasonRequestMethodPUT)
8786

httpcache.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,22 @@ import (
1313
// NewWithCustomStorageCache will initiate the httpcache with your defined cache storage
1414
// To use your own cache storage handler, you need to implement the cache.Interactor interface
1515
// And pass it to httpcache.
16-
func NewWithCustomStorageCache(client *http.Client, cacheInteractor cache.Interactor) (err error) {
16+
func NewWithCustomStorageCache(client *http.Client, cacheInteractor cache.ICacheInteractor) (cacheHandler *CacheHandler, err error) {
1717
return newClient(client, cacheInteractor)
1818
}
1919

20-
func newClient(client *http.Client, cacheInteractor cache.Interactor) (err error) {
20+
func newClient(client *http.Client, cacheInteractor cache.ICacheInteractor) (cachedHandler *CacheHandler, err error) {
2121
if client.Transport == nil {
2222
client.Transport = http.DefaultTransport
2323
}
24-
client.Transport = NewRoundtrip(client.Transport, cacheInteractor)
24+
cachedHandler = NewRoundtrip(client.Transport, cacheInteractor)
25+
client.Transport = cachedHandler
2526
return
2627
}
2728

2829
// NewWithInmemoryCache will create a complete cache-support of HTTP client with using inmemory cache.
2930
// If the duration not set, the cache will use LFU algorithm
30-
func NewWithInmemoryCache(client *http.Client, duration ...time.Duration) (err error) {
31+
func NewWithInmemoryCache(client *http.Client, duration ...time.Duration) (cachedHandler *CacheHandler, err error) {
3132
var expiryTime time.Duration
3233
if len(duration) > 0 {
3334
expiryTime = duration[0]

mocks/ICacheInteractor.go

Lines changed: 90 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)