Introduce an option function type

- Reduce the duplicate code by merging the flows and
determining the flow type by looking at the provided options.
- Options as a function type allows us to validate an individual
an option in its scope and makes it easier to compose the
built-in options with the third-party ones.
This commit is contained in:
Burcu Dogan
2014-11-07 11:36:41 +11:00
parent 49f4824137
commit 0cf6f9b144
15 changed files with 847 additions and 774 deletions

View File

@@ -9,12 +9,18 @@ import (
type mockTokenFetcher struct{ token *Token }
func (f *mockTokenFetcher) Fn() func(*Token) (*Token, error) {
return func(*Token) (*Token, error) {
return f.token, nil
}
}
func (f *mockTokenFetcher) FetchToken(existing *Token) (*Token, error) {
return f.token, nil
}
func TestInitialTokenRead(t *testing.T) {
tr := NewTransport(http.DefaultTransport, nil, &Token{AccessToken: "abc"})
tr := newTransport(http.DefaultTransport, nil, &Token{AccessToken: "abc"})
server := newMockServer(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Bearer abc" {
t.Errorf("Transport doesn't set the Authorization header from the initial token")
@@ -31,7 +37,7 @@ func TestTokenFetch(t *testing.T) {
AccessToken: "abc",
},
}
tr := NewTransport(http.DefaultTransport, fetcher, nil)
tr := newTransport(http.DefaultTransport, fetcher.Fn(), nil)
server := newMockServer(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Bearer abc" {
t.Errorf("Transport doesn't set the Authorization header from the fetched token")