Skip to content

Commit

Permalink
add tests and more expressive error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rybit committed Nov 17, 2017
1 parent 2812133 commit e9a5623
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
4 changes: 2 additions & 2 deletions coupons/coupons.go
Expand Up @@ -83,7 +83,7 @@ func (c *couponCacheFromURL) load() error {
}
resp, err := c.client.Do(req)
if err != nil {
return err
return errors.Wrap(err, "Failed to make request for coupon information")
}

if resp.StatusCode != http.StatusOK {
Expand All @@ -95,7 +95,7 @@ func (c *couponCacheFromURL) load() error {
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
if err := decoder.Decode(couponsResponse); err != nil {
return err
return errors.Wrap(err, "Failed to parse response.")
}

for key, coupon := range couponsResponse.Coupons {
Expand Down
47 changes: 35 additions & 12 deletions coupons/coupons_test.go
Expand Up @@ -89,12 +89,7 @@ func TestExplicitLookup(t *testing.T) {
SiteURL: svr.URL,
}
c.Coupons.URL = "this/is/where/the/coupons/are"

cacheFace, err := NewCouponCacheFromURL(c)
require.NoError(t, err)
require.NotNil(t, cacheFace)
cache, ok := cacheFace.(*couponCacheFromURL)
require.True(t, ok)
cache := newCache(t, c)
assert.Equal(t, svr.URL+"/this/is/where/the/coupons/are", cache.url)

coupon, err := cache.Lookup("meow")
Expand All @@ -107,6 +102,7 @@ func TestExplicitLookup(t *testing.T) {
assert.Error(t, err)
assert.IsType(t, new(CouponNotFound), err)
assert.Nil(t, coupon)
assert.Equal(t, 1, callCount)
}

func TestCacheExpiration(t *testing.T) {
Expand All @@ -121,13 +117,9 @@ func TestCacheExpiration(t *testing.T) {
c := &conf.Configuration{
SiteURL: "this is garbage",
}
// note that this is an absolute path, it is what is used
c.Coupons.URL = svr.URL + "/this/is/where/the/coupons/are"

cacheFace, err := NewCouponCacheFromURL(c)
require.NoError(t, err)
require.NotNil(t, cacheFace)
cache, ok := cacheFace.(*couponCacheFromURL)
require.True(t, ok)
cache := newCache(t, c)
assert.Equal(t, svr.URL+"/this/is/where/the/coupons/are", cache.url)

coupon, err := cache.Lookup("meow")
Expand All @@ -145,3 +137,34 @@ func TestCacheExpiration(t *testing.T) {
assert.Nil(t, coupon)
assert.Equal(t, 2, callCount)
}

func TestMalformedResponse(t *testing.T) {
var callCount int
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
callCount++
w.WriteHeader(http.StatusOK)
w.Write([]byte("this is not json"))
}))
defer svr.Close()

c := &conf.Configuration{
SiteURL: svr.URL,
}
c.Coupons.URL = "/this/is/where/the/coupons/are"
cache := newCache(t, c)
assert.Equal(t, svr.URL+"/this/is/where/the/coupons/are", cache.url)

coupon, err := cache.Lookup("meow")
assert.Error(t, err)
assert.Nil(t, coupon)
assert.Equal(t, 1, callCount)
}

func newCache(t *testing.T, c *conf.Configuration) *couponCacheFromURL {
cacheFace, err := NewCouponCacheFromURL(c)
require.NoError(t, err)
require.NotNil(t, cacheFace)
cache, ok := cacheFace.(*couponCacheFromURL)
require.True(t, ok)
return cache
}

0 comments on commit e9a5623

Please sign in to comment.