Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
| 4 | "net/url" |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 5 | "reflect" |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 6 | "testing" |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 7 | "time" |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 8 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 9 | th "github.com/gophercloud/gophercloud/testhelper" |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 10 | ) |
| 11 | |
Jon Perritt | 1e17aec | 2014-10-06 14:33:34 -0500 | [diff] [blame] | 12 | func TestMaybeString(t *testing.T) { |
| 13 | testString := "" |
| 14 | var expected *string |
| 15 | actual := MaybeString(testString) |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 16 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | 1e17aec | 2014-10-06 14:33:34 -0500 | [diff] [blame] | 17 | |
| 18 | testString = "carol" |
| 19 | expected = &testString |
| 20 | actual = MaybeString(testString) |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 21 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 22 | } |
| 23 | |
Jon Perritt | 1e17aec | 2014-10-06 14:33:34 -0500 | [diff] [blame] | 24 | func TestMaybeInt(t *testing.T) { |
| 25 | testInt := 0 |
| 26 | var expected *int |
| 27 | actual := MaybeInt(testInt) |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 28 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | 1e17aec | 2014-10-06 14:33:34 -0500 | [diff] [blame] | 29 | |
| 30 | testInt = 4 |
| 31 | expected = &testInt |
| 32 | actual = MaybeInt(testInt) |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 33 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 34 | } |
| 35 | |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 36 | func TestBuildQueryString(t *testing.T) { |
Jon Perritt | e43f3de | 2015-02-12 11:45:34 -0700 | [diff] [blame] | 37 | type testVar string |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 38 | opts := struct { |
Jon Perritt | e43f3de | 2015-02-12 11:45:34 -0700 | [diff] [blame] | 39 | J int `q:"j"` |
| 40 | R string `q:"r,required"` |
| 41 | C bool `q:"c"` |
| 42 | S []string `q:"s"` |
| 43 | TS []testVar `q:"ts"` |
| 44 | TI []int `q:"ti"` |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 45 | }{ |
Jon Perritt | e43f3de | 2015-02-12 11:45:34 -0700 | [diff] [blame] | 46 | J: 2, |
| 47 | R: "red", |
| 48 | C: true, |
| 49 | S: []string{"one", "two", "three"}, |
| 50 | TS: []testVar{"a", "b"}, |
| 51 | TI: []int{1, 2}, |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 52 | } |
Jon Perritt | e43f3de | 2015-02-12 11:45:34 -0700 | [diff] [blame] | 53 | expected := &url.URL{RawQuery: "c=true&j=2&r=red&s=one&s=two&s=three&ti=1&ti=2&ts=a&ts=b"} |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 54 | actual, err := BuildQueryString(&opts) |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 55 | if err != nil { |
| 56 | t.Errorf("Error building query string: %v", err) |
| 57 | } |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 58 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 59 | |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 60 | opts = struct { |
Jon Perritt | e43f3de | 2015-02-12 11:45:34 -0700 | [diff] [blame] | 61 | J int `q:"j"` |
| 62 | R string `q:"r,required"` |
| 63 | C bool `q:"c"` |
| 64 | S []string `q:"s"` |
| 65 | TS []testVar `q:"ts"` |
| 66 | TI []int `q:"ti"` |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 67 | }{ |
| 68 | J: 2, |
| 69 | C: true, |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 70 | } |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 71 | _, err = BuildQueryString(&opts) |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 72 | if err == nil { |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 73 | t.Errorf("Expected error: 'Required field not set'") |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 74 | } |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 75 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 76 | |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 77 | _, err = BuildQueryString(map[string]interface{}{"Number": 4}) |
| 78 | if err == nil { |
| 79 | t.Errorf("Expected error: 'Options type is not a struct'") |
| 80 | } |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 81 | } |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 82 | |
| 83 | func TestBuildHeaders(t *testing.T) { |
| 84 | testStruct := struct { |
| 85 | Accept string `h:"Accept"` |
Jon Perritt | dfeb33b | 2014-10-06 16:28:58 -0500 | [diff] [blame] | 86 | Num int `h:"Number,required"` |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 87 | Style bool `h:"Style"` |
| 88 | }{ |
| 89 | Accept: "application/json", |
| 90 | Num: 4, |
| 91 | Style: true, |
| 92 | } |
| 93 | expected := map[string]string{"Accept": "application/json", "Number": "4", "Style": "true"} |
| 94 | actual, err := BuildHeaders(&testStruct) |
| 95 | th.CheckNoErr(t, err) |
| 96 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | dfeb33b | 2014-10-06 16:28:58 -0500 | [diff] [blame] | 97 | |
| 98 | testStruct.Num = 0 |
| 99 | _, err = BuildHeaders(&testStruct) |
| 100 | if err == nil { |
| 101 | t.Errorf("Expected error: 'Required header not set'") |
| 102 | } |
| 103 | |
| 104 | _, err = BuildHeaders(map[string]interface{}{"Number": 4}) |
| 105 | if err == nil { |
| 106 | t.Errorf("Expected error: 'Options type is not a struct'") |
| 107 | } |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | func TestIsZero(t *testing.T) { |
| 111 | var testMap map[string]interface{} |
| 112 | testMapValue := reflect.ValueOf(testMap) |
| 113 | expected := true |
| 114 | actual := isZero(testMapValue) |
| 115 | th.CheckEquals(t, expected, actual) |
| 116 | testMap = map[string]interface{}{"empty": false} |
| 117 | testMapValue = reflect.ValueOf(testMap) |
| 118 | expected = false |
| 119 | actual = isZero(testMapValue) |
| 120 | th.CheckEquals(t, expected, actual) |
| 121 | |
| 122 | var testArray [2]string |
| 123 | testArrayValue := reflect.ValueOf(testArray) |
| 124 | expected = true |
| 125 | actual = isZero(testArrayValue) |
| 126 | th.CheckEquals(t, expected, actual) |
| 127 | testArray = [2]string{"one", "two"} |
| 128 | testArrayValue = reflect.ValueOf(testArray) |
| 129 | expected = false |
| 130 | actual = isZero(testArrayValue) |
| 131 | th.CheckEquals(t, expected, actual) |
| 132 | |
| 133 | var testStruct struct { |
| 134 | A string |
| 135 | B time.Time |
| 136 | } |
| 137 | testStructValue := reflect.ValueOf(testStruct) |
| 138 | expected = true |
| 139 | actual = isZero(testStructValue) |
| 140 | th.CheckEquals(t, expected, actual) |
| 141 | testStruct = struct { |
| 142 | A string |
| 143 | B time.Time |
| 144 | }{ |
| 145 | B: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), |
| 146 | } |
| 147 | testStructValue = reflect.ValueOf(testStruct) |
| 148 | expected = false |
| 149 | actual = isZero(testStructValue) |
| 150 | th.CheckEquals(t, expected, actual) |
Jamie Hannaford | f68c3e4 | 2014-11-18 13:02:09 +0100 | [diff] [blame] | 151 | } |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 152 | |
Jamie Hannaford | f68c3e4 | 2014-11-18 13:02:09 +0100 | [diff] [blame] | 153 | func TestQueriesAreEscaped(t *testing.T) { |
| 154 | type foo struct { |
| 155 | Name string `q:"something"` |
| 156 | Shape string `q:"else"` |
| 157 | } |
| 158 | |
| 159 | expected := &url.URL{RawQuery: "else=Triangl+e&something=blah%2B%3F%21%21foo"} |
| 160 | |
| 161 | actual, err := BuildQueryString(foo{Name: "blah+?!!foo", Shape: "Triangl e"}) |
| 162 | th.AssertNoErr(t, err) |
| 163 | |
| 164 | th.AssertDeepEquals(t, expected, actual) |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 165 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 166 | |
| 167 | func TestBuildRequestBody(t *testing.T) { |
| 168 | type PasswordCredentials struct { |
| 169 | Username string `json:"username" required:"true"` |
| 170 | Password string `json:"password" required:"true"` |
| 171 | } |
| 172 | |
| 173 | type TokenCredentials struct { |
| 174 | ID string `json:"id,omitempty" required:"true"` |
| 175 | } |
| 176 | |
| 177 | type orFields struct { |
| 178 | Filler int `json:"filler,omitempty"` |
| 179 | F1 int `json:"f1,omitempty" or:"F2"` |
| 180 | F2 int `json:"f2,omitempty" or:"F1"` |
| 181 | } |
| 182 | |
| 183 | // AuthOptions wraps a gophercloud AuthOptions in order to adhere to the AuthOptionsBuilder |
| 184 | // interface. |
| 185 | type AuthOptions struct { |
| 186 | PasswordCredentials `json:"passwordCredentials,omitempty" xor:"TokenCredentials"` |
| 187 | |
| 188 | // The TenantID and TenantName fields are optional for the Identity V2 API. |
| 189 | // Some providers allow you to specify a TenantName instead of the TenantId. |
| 190 | // Some require both. Your provider's authentication policies will determine |
| 191 | // how these fields influence authentication. |
| 192 | TenantID string `json:"tenantId,omitempty"` |
| 193 | TenantName string `json:"tenantName,omitempty"` |
| 194 | |
| 195 | // TokenCredentials allows users to authenticate (possibly as another user) with an |
| 196 | // authentication token ID. |
| 197 | TokenCredentials `json:"token,omitempty" xor:"PasswordCredentials"` |
| 198 | |
| 199 | OrFields orFields `json:"or_fields,omitempty"` |
| 200 | } |
| 201 | |
| 202 | var successCases = []struct { |
| 203 | opts AuthOptions |
| 204 | expected map[string]interface{} |
| 205 | }{ |
| 206 | { |
| 207 | AuthOptions{ |
| 208 | PasswordCredentials: PasswordCredentials{ |
| 209 | Username: "me", |
| 210 | Password: "swordfish", |
| 211 | }, |
| 212 | }, |
| 213 | map[string]interface{}{ |
| 214 | "auth": map[string]interface{}{ |
| 215 | "passwordCredentials": map[string]interface{}{ |
| 216 | "password": "swordfish", |
| 217 | "username": "me", |
| 218 | }, |
| 219 | }, |
| 220 | }, |
| 221 | }, |
| 222 | { |
| 223 | AuthOptions{ |
| 224 | TokenCredentials: TokenCredentials{ |
| 225 | ID: "1234567", |
| 226 | }, |
| 227 | }, |
| 228 | map[string]interface{}{ |
| 229 | "auth": map[string]interface{}{ |
| 230 | "token": map[string]interface{}{ |
| 231 | "id": "1234567", |
| 232 | }, |
| 233 | }, |
| 234 | }, |
| 235 | }, |
| 236 | } |
| 237 | |
| 238 | for _, successCase := range successCases { |
| 239 | actual, err := BuildRequestBody(successCase.opts, "auth") |
| 240 | th.AssertNoErr(t, err) |
| 241 | th.AssertDeepEquals(t, successCase.expected, actual) |
| 242 | } |
| 243 | |
| 244 | var failCases = []struct { |
| 245 | opts AuthOptions |
| 246 | expected error |
| 247 | }{ |
| 248 | { |
| 249 | AuthOptions{ |
| 250 | TenantID: "987654321", |
| 251 | TenantName: "me", |
| 252 | }, |
| 253 | ErrMissingInput{}, |
| 254 | }, |
| 255 | { |
| 256 | AuthOptions{ |
| 257 | TokenCredentials: TokenCredentials{ |
| 258 | ID: "1234567", |
| 259 | }, |
| 260 | PasswordCredentials: PasswordCredentials{ |
| 261 | Username: "me", |
| 262 | Password: "swordfish", |
| 263 | }, |
| 264 | }, |
| 265 | ErrMissingInput{}, |
| 266 | }, |
| 267 | { |
| 268 | AuthOptions{ |
| 269 | PasswordCredentials: PasswordCredentials{ |
| 270 | Password: "swordfish", |
| 271 | }, |
| 272 | }, |
| 273 | ErrMissingInput{}, |
| 274 | }, |
| 275 | { |
| 276 | AuthOptions{ |
| 277 | PasswordCredentials: PasswordCredentials{ |
| 278 | Username: "me", |
| 279 | Password: "swordfish", |
| 280 | }, |
| 281 | OrFields: orFields{ |
| 282 | Filler: 2, |
| 283 | }, |
| 284 | }, |
| 285 | ErrMissingInput{}, |
| 286 | }, |
| 287 | } |
| 288 | |
| 289 | for _, failCase := range failCases { |
| 290 | _, err := BuildRequestBody(failCase.opts, "auth") |
| 291 | th.AssertDeepEquals(t, reflect.TypeOf(failCase.expected), reflect.TypeOf(err)) |
| 292 | } |
| 293 | } |