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 | |
| 9 | th "github.com/rackspace/gophercloud/testhelper" |
| 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) { |
| 37 | opts := struct { |
Jon Perritt | dc56190 | 2015-02-08 15:22:02 -0700 | [diff] [blame] | 38 | J int `q:"j"` |
| 39 | R string `q:"r,required"` |
| 40 | C bool `q:"c"` |
| 41 | S []string `q:"s"` |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 42 | }{ |
| 43 | J: 2, |
| 44 | R: "red", |
| 45 | C: true, |
Jon Perritt | dc56190 | 2015-02-08 15:22:02 -0700 | [diff] [blame] | 46 | S: []string{"one", "two", "three"}, |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 47 | } |
Jon Perritt | dc56190 | 2015-02-08 15:22:02 -0700 | [diff] [blame] | 48 | expected := &url.URL{RawQuery: "c=true&j=2&r=red&s=one&s=two&s=three"} |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 49 | actual, err := BuildQueryString(&opts) |
Jon Perritt | 255b6f8 | 2014-09-30 16:07:50 -0500 | [diff] [blame] | 50 | if err != nil { |
| 51 | t.Errorf("Error building query string: %v", err) |
| 52 | } |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 53 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 54 | |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 55 | opts = struct { |
Jon Perritt | dc56190 | 2015-02-08 15:22:02 -0700 | [diff] [blame] | 56 | J int `q:"j"` |
| 57 | R string `q:"r,required"` |
| 58 | C bool `q:"c"` |
| 59 | S []string `q:"s"` |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 60 | }{ |
| 61 | J: 2, |
| 62 | C: true, |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 63 | } |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 64 | _, err = BuildQueryString(&opts) |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 65 | if err == nil { |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 66 | t.Errorf("Expected error: 'Required field not set'") |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 67 | } |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 68 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 69 | |
Jon Perritt | b5d13ad | 2014-10-06 16:39:27 -0500 | [diff] [blame] | 70 | _, err = BuildQueryString(map[string]interface{}{"Number": 4}) |
| 71 | if err == nil { |
| 72 | t.Errorf("Expected error: 'Options type is not a struct'") |
| 73 | } |
Jon Perritt | db00ad1 | 2014-09-30 16:29:50 -0500 | [diff] [blame] | 74 | } |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 75 | |
| 76 | func TestBuildHeaders(t *testing.T) { |
| 77 | testStruct := struct { |
| 78 | Accept string `h:"Accept"` |
Jon Perritt | dfeb33b | 2014-10-06 16:28:58 -0500 | [diff] [blame] | 79 | Num int `h:"Number,required"` |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 80 | Style bool `h:"Style"` |
| 81 | }{ |
| 82 | Accept: "application/json", |
| 83 | Num: 4, |
| 84 | Style: true, |
| 85 | } |
| 86 | expected := map[string]string{"Accept": "application/json", "Number": "4", "Style": "true"} |
| 87 | actual, err := BuildHeaders(&testStruct) |
| 88 | th.CheckNoErr(t, err) |
| 89 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | dfeb33b | 2014-10-06 16:28:58 -0500 | [diff] [blame] | 90 | |
| 91 | testStruct.Num = 0 |
| 92 | _, err = BuildHeaders(&testStruct) |
| 93 | if err == nil { |
| 94 | t.Errorf("Expected error: 'Required header not set'") |
| 95 | } |
| 96 | |
| 97 | _, err = BuildHeaders(map[string]interface{}{"Number": 4}) |
| 98 | if err == nil { |
| 99 | t.Errorf("Expected error: 'Options type is not a struct'") |
| 100 | } |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | func TestIsZero(t *testing.T) { |
| 104 | var testMap map[string]interface{} |
| 105 | testMapValue := reflect.ValueOf(testMap) |
| 106 | expected := true |
| 107 | actual := isZero(testMapValue) |
| 108 | th.CheckEquals(t, expected, actual) |
| 109 | testMap = map[string]interface{}{"empty": false} |
| 110 | testMapValue = reflect.ValueOf(testMap) |
| 111 | expected = false |
| 112 | actual = isZero(testMapValue) |
| 113 | th.CheckEquals(t, expected, actual) |
| 114 | |
| 115 | var testArray [2]string |
| 116 | testArrayValue := reflect.ValueOf(testArray) |
| 117 | expected = true |
| 118 | actual = isZero(testArrayValue) |
| 119 | th.CheckEquals(t, expected, actual) |
| 120 | testArray = [2]string{"one", "two"} |
| 121 | testArrayValue = reflect.ValueOf(testArray) |
| 122 | expected = false |
| 123 | actual = isZero(testArrayValue) |
| 124 | th.CheckEquals(t, expected, actual) |
| 125 | |
| 126 | var testStruct struct { |
| 127 | A string |
| 128 | B time.Time |
| 129 | } |
| 130 | testStructValue := reflect.ValueOf(testStruct) |
| 131 | expected = true |
| 132 | actual = isZero(testStructValue) |
| 133 | th.CheckEquals(t, expected, actual) |
| 134 | testStruct = struct { |
| 135 | A string |
| 136 | B time.Time |
| 137 | }{ |
| 138 | B: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), |
| 139 | } |
| 140 | testStructValue = reflect.ValueOf(testStruct) |
| 141 | expected = false |
| 142 | actual = isZero(testStructValue) |
| 143 | th.CheckEquals(t, expected, actual) |
Jamie Hannaford | f68c3e4 | 2014-11-18 13:02:09 +0100 | [diff] [blame] | 144 | } |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 145 | |
Jamie Hannaford | f68c3e4 | 2014-11-18 13:02:09 +0100 | [diff] [blame] | 146 | func TestQueriesAreEscaped(t *testing.T) { |
| 147 | type foo struct { |
| 148 | Name string `q:"something"` |
| 149 | Shape string `q:"else"` |
| 150 | } |
| 151 | |
| 152 | expected := &url.URL{RawQuery: "else=Triangl+e&something=blah%2B%3F%21%21foo"} |
| 153 | |
| 154 | actual, err := BuildQueryString(foo{Name: "blah+?!!foo", Shape: "Triangl e"}) |
| 155 | th.AssertNoErr(t, err) |
| 156 | |
| 157 | th.AssertDeepEquals(t, expected, actual) |
Jon Perritt | c3e04b6 | 2014-10-06 16:17:49 -0500 | [diff] [blame] | 158 | } |