Cache read operation should be handled during construction.

This commit is contained in:
Burcu Dogan 2014-05-26 11:53:51 +02:00
parent 6bb3577bf0
commit 909f098dcd
3 changed files with 91 additions and 30 deletions

View File

@ -7,19 +7,34 @@ package oauth2
import ( import (
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"os"
) )
// Cache represents a token cacher. // Cache represents a token cacher.
type Cache interface { type Cache interface {
// Read reads a cache token from the specified file. // Token returns the initial token retrieved from the cache,
Read() (token *Token) // if there is no existing token nil value is returned.
Token() (token *Token)
// Write writes a token to the specified file. // Write writes a token to the specified file.
Write(token *Token) Write(token *Token)
} }
// NewFileCache creates a new file cache. // NewFileCache creates a new file cache.
func NewFileCache(filename string) *FileCache { func NewFileCache(filename string) (cache *FileCache, err error) {
return &FileCache{filename: filename} data, err := ioutil.ReadFile(filename)
if os.IsNotExist(err) {
// no token has cached before, skip reading
return &FileCache{filename: filename}, nil
}
if err != nil {
return
}
var token Token
if err = json.Unmarshal(data, &token); err != nil {
return
}
cache = &FileCache{filename: filename, initialToken: &token}
return
} }
// FileCache represents a file based token cacher. // FileCache represents a file based token cacher.
@ -28,19 +43,15 @@ type FileCache struct {
// during read or write operations. // during read or write operations.
ErrorHandler func(error) ErrorHandler func(error)
filename string initialToken *Token
filename string
} }
// Read reads a cache token from the specified file. // Token returns the initial token read from the cache. It should be used to
func (f *FileCache) Read() (token *Token) { // warm the authorization mechanism, token refreshes and later writes don't
data, err := ioutil.ReadFile(f.filename) // change the returned value. If no token is cached before, returns nil.
if err == nil { func (f *FileCache) Token() (token *Token) {
err = json.Unmarshal(data, token) return f.initialToken
}
if f.ErrorHandler != nil {
f.ErrorHandler(err)
}
return
} }
// Write writes a token to the specified file. // Write writes a token to the specified file.

View File

@ -5,23 +5,73 @@
package oauth2 package oauth2
import ( import (
"os" "io/ioutil"
"path"
"testing" "testing"
) )
func TestFileCacheErrorHandling(t *testing.T) { var tokenBody = `{"access_token":"abc123","token_type":"Bearer","refresh_token":"def789","expiry":"0001-01-01T00:00:00Z"}`
var lastErr error
fileCache := NewFileCache("/path/that/doesnt/exist") func TestNewFileCacheNotExist(t *testing.T) {
fileCache.ErrorHandler = func(err error) { cache, err := NewFileCache("/path/that/doesnt/exist")
lastErr = err if err != nil {
t.Fatalf("NewFileCache shouldn't return an error for if cache file doesn't exist, but returned %v", err)
} }
fileCache.Read() if cache == nil {
if !os.IsNotExist(lastErr) { t.Fatalf("A file cache should be inited with a non existing cache file")
t.Fatalf("Read should have invoked the error handling func with os.ErrNotExist, but read err is %v", lastErr) }
} }
lastErr = nil
fileCache.Write(&Token{}) func TestNewFileCache(t *testing.T) {
if !os.IsNotExist(lastErr) { f, err := ioutil.TempFile("", "")
t.Fatalf("Write should have invoked the error handling func with os.ErrNotExist, but read err is %v", lastErr) if err != nil {
t.Fatal(err)
}
_, err = f.WriteString(tokenBody)
if err != nil {
t.Fatal(err)
}
cache, err := NewFileCache(f.Name())
if err != nil {
t.Fatalf("Cache should have read the file cache at %v, but recieved %v", f.Name(), err)
}
token := cache.Token()
if token.AccessToken != "abc123" {
t.Fatalf("Cached access token is %v, expected to be abc123", token.AccessToken)
}
if token.RefreshToken != "def789" {
t.Fatalf("Cached refresh token is %v, expected to be def789", token.RefreshToken)
}
}
func TestFileCacheWrite(t *testing.T) {
dirName, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
cache, err := NewFileCache(path.Join(dirName, "cache-file"))
cache.ErrorHandler = func(err error) {
if err != nil {
t.Fatalf("Cache write should have been succeeded succesfully, recieved %v", err)
}
}
if err != nil {
t.Fatal(err)
}
cache.Write(&Token{
AccessToken: "abc123",
TokenType: "Bearer",
RefreshToken: "def789",
})
data, err := ioutil.ReadFile(cache.filename)
if err != nil {
t.Fatal(err)
}
if string(data) != tokenBody {
t.Fatalf("Written token is different than the expected, %v is found", data)
} }
} }

View File

@ -98,7 +98,7 @@ func (t *authorizedTransport) RoundTrip(req *http.Request) (resp *http.Response,
// Try to initialize the token from the cache. // Try to initialize the token from the cache.
if token == nil { if token == nil {
if c := t.fetcher.Cache(); c != nil { if c := t.fetcher.Cache(); c != nil {
token = c.Read() token = c.Token()
} }
} }