blob: 2f40eec812228fc5c2951763814d319460d62fc8 [file] [log] [blame]
Jon Perritt255b6f82014-09-30 16:07:50 -05001package gophercloud
2
3import (
4 "net/url"
Jon Perrittc3e04b62014-10-06 16:17:49 -05005 "reflect"
Jon Perritt255b6f82014-09-30 16:07:50 -05006 "testing"
Jon Perrittc3e04b62014-10-06 16:17:49 -05007 "time"
Jon Perritt255b6f82014-09-30 16:07:50 -05008
9 th "github.com/rackspace/gophercloud/testhelper"
10)
11
Jon Perritt1e17aec2014-10-06 14:33:34 -050012func TestMaybeString(t *testing.T) {
13 testString := ""
14 var expected *string
15 actual := MaybeString(testString)
Jon Perrittc3e04b62014-10-06 16:17:49 -050016 th.CheckDeepEquals(t, expected, actual)
Jon Perritt1e17aec2014-10-06 14:33:34 -050017
18 testString = "carol"
19 expected = &testString
20 actual = MaybeString(testString)
Jon Perrittc3e04b62014-10-06 16:17:49 -050021 th.CheckDeepEquals(t, expected, actual)
Jon Perritt255b6f82014-09-30 16:07:50 -050022}
23
Jon Perritt1e17aec2014-10-06 14:33:34 -050024func TestMaybeInt(t *testing.T) {
25 testInt := 0
26 var expected *int
27 actual := MaybeInt(testInt)
Jon Perrittc3e04b62014-10-06 16:17:49 -050028 th.CheckDeepEquals(t, expected, actual)
Jon Perritt1e17aec2014-10-06 14:33:34 -050029
30 testInt = 4
31 expected = &testInt
32 actual = MaybeInt(testInt)
Jon Perrittc3e04b62014-10-06 16:17:49 -050033 th.CheckDeepEquals(t, expected, actual)
Jon Perritt255b6f82014-09-30 16:07:50 -050034}
35
Jon Perrittb5d13ad2014-10-06 16:39:27 -050036func TestBuildQueryString(t *testing.T) {
Jon Perritte43f3de2015-02-12 11:45:34 -070037 type testVar string
Jon Perrittb5d13ad2014-10-06 16:39:27 -050038 opts := struct {
Jon Perritte43f3de2015-02-12 11:45:34 -070039 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 Perrittb5d13ad2014-10-06 16:39:27 -050045 }{
Jon Perritte43f3de2015-02-12 11:45:34 -070046 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 Perritt255b6f82014-09-30 16:07:50 -050052 }
Jon Perritte43f3de2015-02-12 11:45:34 -070053 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 Perrittdb00ad12014-09-30 16:29:50 -050054 actual, err := BuildQueryString(&opts)
Jon Perritt255b6f82014-09-30 16:07:50 -050055 if err != nil {
56 t.Errorf("Error building query string: %v", err)
57 }
Jon Perrittc3e04b62014-10-06 16:17:49 -050058 th.CheckDeepEquals(t, expected, actual)
Jon Perrittdb00ad12014-09-30 16:29:50 -050059
Jon Perrittb5d13ad2014-10-06 16:39:27 -050060 opts = struct {
Jon Perritte43f3de2015-02-12 11:45:34 -070061 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 Perrittb5d13ad2014-10-06 16:39:27 -050067 }{
68 J: 2,
69 C: true,
Jon Perrittdb00ad12014-09-30 16:29:50 -050070 }
Jon Perrittb5d13ad2014-10-06 16:39:27 -050071 _, err = BuildQueryString(&opts)
Jon Perrittdb00ad12014-09-30 16:29:50 -050072 if err == nil {
Jon Perrittb5d13ad2014-10-06 16:39:27 -050073 t.Errorf("Expected error: 'Required field not set'")
Jon Perrittdb00ad12014-09-30 16:29:50 -050074 }
Jon Perrittb5d13ad2014-10-06 16:39:27 -050075 th.CheckDeepEquals(t, expected, actual)
Jon Perrittdb00ad12014-09-30 16:29:50 -050076
Jon Perrittb5d13ad2014-10-06 16:39:27 -050077 _, err = BuildQueryString(map[string]interface{}{"Number": 4})
78 if err == nil {
79 t.Errorf("Expected error: 'Options type is not a struct'")
80 }
Jon Perrittdb00ad12014-09-30 16:29:50 -050081}
Jon Perrittc3e04b62014-10-06 16:17:49 -050082
83func TestBuildHeaders(t *testing.T) {
84 testStruct := struct {
85 Accept string `h:"Accept"`
Jon Perrittdfeb33b2014-10-06 16:28:58 -050086 Num int `h:"Number,required"`
Jon Perrittc3e04b62014-10-06 16:17:49 -050087 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 Perrittdfeb33b2014-10-06 16:28:58 -050097
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 Perrittc3e04b62014-10-06 16:17:49 -0500108}
109
110func 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 Hannafordf68c3e42014-11-18 13:02:09 +0100151}
Jon Perrittc3e04b62014-10-06 16:17:49 -0500152
Jamie Hannafordf68c3e42014-11-18 13:02:09 +0100153func 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 Perrittc3e04b62014-10-06 16:17:49 -0500165}