blob: e04968a3812ef4f060bf6e5600b789dfd91594cd [file] [log] [blame]
Samuel A. Falvo II1206f852013-07-15 17:56:51 -07001package gophercloud
2
3import (
4 "testing"
5 "github.com/racker/perigee"
6)
7
8// This reauth-handler does nothing, and returns no error.
9func doNothing() error {
10 return nil
11}
12
13func 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
43func 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
69func 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
88type MyError struct {}
89func (*MyError) Error() string {
90 return "MyError instance"
91}
92
93func 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}