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:
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user