Simplify WithReauth() and its dependency workflow.
This change also threads the desired AccessProvider to the reauth
handler, so the reauth handler doesn't have to fish it out through some
hard to maintain back-channel.
diff --git a/context.go b/context.go
index 86f4ef5..2c70ba5 100644
--- a/context.go
+++ b/context.go
@@ -14,7 +14,7 @@
// ReauthHandlerFunc functions are responsible for somehow performing the task of
// reauthentication.
-type ReauthHandlerFunc func() error
+type ReauthHandlerFunc func(AccessProvider) error
// Context structures encapsulate Gophercloud-global state in a manner which
// facilitates easier unit testing. As a user of this SDK, you'll never
diff --git a/reauth.go b/reauth.go
index 98fba55..5637ea2 100644
--- a/reauth.go
+++ b/reauth.go
@@ -10,14 +10,14 @@
// Do not confuse this function with WithReauth()! Although they work together to support reauthentication,
// WithReauth() actually contains the decision-making logic to determine when to perform a reauth,
// while WithReauthHandler() is used to configure what a reauth actually entails.
-func (c *Context) WithReauth(f func() (interface{}, error)) (interface{}, error) {
- result, err := f()
+func (c *Context) WithReauth(ap AccessProvider, f func() error) error {
+ err := f()
cause, ok := err.(*perigee.UnexpectedResponseCodeError)
if ok && cause.Actual == 401 {
- err = c.reauthHandler()
+ err = c.reauthHandler(ap)
if err == nil {
- result, err = f()
+ err = f()
}
}
- return result, err
+ return err
}
\ No newline at end of file
diff --git a/reauth_test.go b/reauth_test.go
index e04968a..c1911e3 100644
--- a/reauth_test.go
+++ b/reauth_test.go
@@ -6,7 +6,7 @@
)
// This reauth-handler does nothing, and returns no error.
-func doNothing() error {
+func doNothing(_ AccessProvider) error {
return nil
}
@@ -14,18 +14,14 @@
calls := 0
c := TestContext().WithReauthHandler(doNothing)
- myObj, err := c.WithReauth(func() (interface{}, error) {
+ err := c.WithReauth(nil, func() error {
calls++
- return nil, &perigee.UnexpectedResponseCodeError{
+ return &perigee.UnexpectedResponseCodeError{
Expected: []int{204},
Actual: 404,
}
})
- if myObj != nil {
- t.Errorf("Returned nil myObj; got %#v", myObj)
- return
- }
if err == nil {
t.Error("Expected MyError to be returned; got nil instead.")
return
@@ -44,18 +40,14 @@
calls := 0
c := TestContext().WithReauthHandler(doNothing)
- myObj, err := c.WithReauth(func() (interface{}, error) {
+ err := c.WithReauth(nil, func() error {
calls++
- return nil, &perigee.UnexpectedResponseCodeError{
+ return &perigee.UnexpectedResponseCodeError{
Expected: []int{204},
Actual: 401,
}
})
- if myObj != nil {
- t.Errorf("Returned nil myObj; got %#v", myObj)
- return
- }
if err == nil {
t.Error("Expected MyError to be returned; got nil instead.")
return
@@ -68,12 +60,12 @@
func TestReauthAttemptShouldHappen(t *testing.T) {
calls := 0
- c := TestContext().WithReauthHandler(func() error {
+ c := TestContext().WithReauthHandler(func(_ AccessProvider) error {
calls++
return nil
})
- c.WithReauth(func() (interface{}, error) {
- return nil, &perigee.UnexpectedResponseCodeError{
+ c.WithReauth(nil, func() error {
+ return &perigee.UnexpectedResponseCodeError{
Expected: []int{204},
Actual: 401,
}
@@ -91,12 +83,12 @@
}
func TestReauthErrorShouldPropegate(t *testing.T) {
- c := TestContext().WithReauthHandler(func() error {
+ c := TestContext().WithReauthHandler(func(_ AccessProvider) error {
return &MyError{}
})
- _, err := c.WithReauth(func() (interface{}, error) {
- return nil, &perigee.UnexpectedResponseCodeError{
+ err := c.WithReauth(nil, func() error {
+ return &perigee.UnexpectedResponseCodeError{
Expected: []int{204},
Actual: 401,
}
@@ -107,3 +99,27 @@
return
}
}
+
+type MyAccess struct {}
+func (my *MyAccess) FirstEndpointUrlByCriteria(ApiCriteria) string {
+ return ""
+}
+func (my *MyAccess) AuthToken() string {
+ return ""
+}
+
+func TestReauthHandlerUsesSameAccessProvider(t *testing.T) {
+ fakeAccess := &MyAccess{}
+ c := TestContext().WithReauthHandler(func(acc AccessProvider) error {
+ if acc != fakeAccess {
+ t.Errorf("Expected acc = fakeAccess")
+ }
+ return nil
+ })
+ c.WithReauth(fakeAccess, func() error {
+ return &perigee.UnexpectedResponseCodeError{
+ Expected: []int{204},
+ Actual: 401,
+ }
+ })
+}