Skip to content

Commit

Permalink
Merge pull request #196 from govau/fixtests
Browse files Browse the repository at this point in the history
Add basic tests for new caching
  • Loading branch information
shinji62 authored Aug 21, 2018
2 parents 62b9ecd + 550b26e commit 2c5a3f2
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 352 deletions.
15 changes: 4 additions & 11 deletions caching/caching.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package caching

import (
"errors"
"net/http"
"io"
"regexp"

cfclient "github.com/cloudfoundry-community/go-cfclient"
)

type App struct {
Expand Down Expand Up @@ -40,20 +38,15 @@ type CacheStore interface {
Set(key string, value interface{}) error
}

//go:generate counterfeiter . Caching
type Caching interface {
FillCache() error
GetApp(string) (*App, error)
}

//go:generate counterfeiter . CFSimpleClient
type CFSimpleClient interface {
DoGet(url string) (*http.Response, error)
}

type AppClient interface {
AppByGuid(appGuid string) (cfclient.App, error)
ListOrgs() ([]cfclient.Org, error)
OrgSpaces(guid string) ([]cfclient.Space, error)
GetAppByGuidNoInlineCall(appGuid string) (cfclient.App, error)
DoGet(url string) (io.ReadCloser, error)
}

func IsNeeded(wantedEvents string) bool {
Expand Down
16 changes: 14 additions & 2 deletions caching/caching_cfclientadapter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package caching

import (
"fmt"
"io"
"net/http"

cfclient "github.com/cloudfoundry-community/go-cfclient"
Expand All @@ -10,6 +12,16 @@ type CFClientAdapter struct {
CF *cfclient.Client
}

func (c *CFClientAdapter) DoGet(url string) (*http.Response, error) {
return c.CF.DoRequestWithoutRedirects(c.CF.NewRequest(http.MethodGet, url))
func (c *CFClientAdapter) DoGet(url string) (io.ReadCloser, error) {
resp, err := c.CF.DoRequestWithoutRedirects(c.CF.NewRequest(http.MethodGet, url))
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
resp.Body.Close()
return nil, fmt.Errorf("bad status code: %s", resp.Status)
}

return resp.Body, nil
}
9 changes: 2 additions & 7 deletions caching/caching_lazyfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"math/rand"
"net/http"
"strings"
"time"

Expand Down Expand Up @@ -195,13 +194,9 @@ func (c *CacheLazyFill) makeRequestAndDecodeJSON(url string, rv interface{}) err
if err != nil {
return err
}
defer resp.Body.Close()
defer resp.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status code: %s", resp.Status)
}

err = json.NewDecoder(resp.Body).Decode(rv)
err = json.NewDecoder(resp).Decode(rv)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 2c5a3f2

Please sign in to comment.