Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame^] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "github.com/racker/perigee" |
| 6 | ) |
| 7 | |
| 8 | // This reauth-handler does nothing, and returns no error. |
| 9 | func doNothing() error { |
| 10 | return nil |
| 11 | } |
| 12 | |
| 13 | func TestOtherErrorsPropegate(t *testing.T) { |
| 14 | calls := 0 |
| 15 | c := TestContext().WithReauthHandler(doNothing) |
| 16 | |
| 17 | myObj, err := c.WithReauth(func() (interface{}, error) { |
| 18 | calls++ |
| 19 | return nil, &perigee.UnexpectedResponseCodeError{ |
| 20 | Expected: []int{204}, |
| 21 | Actual: 404, |
| 22 | } |
| 23 | }) |
| 24 | |
| 25 | if myObj != nil { |
| 26 | t.Errorf("Returned nil myObj; got %#v", myObj) |
| 27 | return |
| 28 | } |
| 29 | if err == nil { |
| 30 | t.Error("Expected MyError to be returned; got nil instead.") |
| 31 | return |
| 32 | } |
| 33 | if _, ok := err.(*perigee.UnexpectedResponseCodeError); !ok { |
| 34 | t.Error("Expected UnexpectedResponseCodeError; got %#v", err) |
| 35 | return |
| 36 | } |
| 37 | if calls != 1 { |
| 38 | t.Errorf("Expected the body to be invoked once; found %d calls instead", calls) |
| 39 | return |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func Test401ErrorCausesBodyInvokation2ndTime(t *testing.T) { |
| 44 | calls := 0 |
| 45 | c := TestContext().WithReauthHandler(doNothing) |
| 46 | |
| 47 | myObj, err := c.WithReauth(func() (interface{}, error) { |
| 48 | calls++ |
| 49 | return nil, &perigee.UnexpectedResponseCodeError{ |
| 50 | Expected: []int{204}, |
| 51 | Actual: 401, |
| 52 | } |
| 53 | }) |
| 54 | |
| 55 | if myObj != nil { |
| 56 | t.Errorf("Returned nil myObj; got %#v", myObj) |
| 57 | return |
| 58 | } |
| 59 | if err == nil { |
| 60 | t.Error("Expected MyError to be returned; got nil instead.") |
| 61 | return |
| 62 | } |
| 63 | if calls != 2 { |
| 64 | t.Errorf("Expected the body to be invoked once; found %d calls instead", calls) |
| 65 | return |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func TestReauthAttemptShouldHappen(t *testing.T) { |
| 70 | calls := 0 |
| 71 | c := TestContext().WithReauthHandler(func() error { |
| 72 | calls++ |
| 73 | return nil |
| 74 | }) |
| 75 | c.WithReauth(func() (interface{}, error) { |
| 76 | return nil, &perigee.UnexpectedResponseCodeError{ |
| 77 | Expected: []int{204}, |
| 78 | Actual: 401, |
| 79 | } |
| 80 | }) |
| 81 | |
| 82 | if calls != 1 { |
| 83 | t.Errorf("Expected Reauthenticator to be called once; found %d instead", calls) |
| 84 | return |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | type MyError struct {} |
| 89 | func (*MyError) Error() string { |
| 90 | return "MyError instance" |
| 91 | } |
| 92 | |
| 93 | func TestReauthErrorShouldPropegate(t *testing.T) { |
| 94 | c := TestContext().WithReauthHandler(func() error { |
| 95 | return &MyError{} |
| 96 | }) |
| 97 | |
| 98 | _, err := c.WithReauth(func() (interface{}, error) { |
| 99 | return nil, &perigee.UnexpectedResponseCodeError{ |
| 100 | Expected: []int{204}, |
| 101 | Actual: 401, |
| 102 | } |
| 103 | }) |
| 104 | |
| 105 | if _, ok := err.(*MyError); !ok { |
| 106 | t.Errorf("Expected a MyError; got %#v", err) |
| 107 | return |
| 108 | } |
| 109 | } |