blob: f314a9eea9992baf8b7a5832afe03dfa38976017 [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) {
37 opts := struct {
Jon Perrittdc561902015-02-08 15:22:02 -070038 J int `q:"j"`
39 R string `q:"r,required"`
40 C bool `q:"c"`
41 S []string `q:"s"`
Jon Perrittb5d13ad2014-10-06 16:39:27 -050042 }{
43 J: 2,
44 R: "red",
45 C: true,
Jon Perrittdc561902015-02-08 15:22:02 -070046 S: []string{"one", "two", "three"},
Jon Perritt255b6f82014-09-30 16:07:50 -050047 }
Jon Perrittdc561902015-02-08 15:22:02 -070048 expected := &url.URL{RawQuery: "c=true&j=2&r=red&s=one&s=two&s=three"}
Jon Perrittdb00ad12014-09-30 16:29:50 -050049 actual, err := BuildQueryString(&opts)
Jon Perritt255b6f82014-09-30 16:07:50 -050050 if err != nil {
51 t.Errorf("Error building query string: %v", err)
52 }
Jon Perrittc3e04b62014-10-06 16:17:49 -050053 th.CheckDeepEquals(t, expected, actual)
Jon Perrittdb00ad12014-09-30 16:29:50 -050054
Jon Perrittb5d13ad2014-10-06 16:39:27 -050055 opts = struct {
Jon Perrittdc561902015-02-08 15:22:02 -070056 J int `q:"j"`
57 R string `q:"r,required"`
58 C bool `q:"c"`
59 S []string `q:"s"`
Jon Perrittb5d13ad2014-10-06 16:39:27 -050060 }{
61 J: 2,
62 C: true,
Jon Perrittdb00ad12014-09-30 16:29:50 -050063 }
Jon Perrittb5d13ad2014-10-06 16:39:27 -050064 _, err = BuildQueryString(&opts)
Jon Perrittdb00ad12014-09-30 16:29:50 -050065 if err == nil {
Jon Perrittb5d13ad2014-10-06 16:39:27 -050066 t.Errorf("Expected error: 'Required field not set'")
Jon Perrittdb00ad12014-09-30 16:29:50 -050067 }
Jon Perrittb5d13ad2014-10-06 16:39:27 -050068 th.CheckDeepEquals(t, expected, actual)
Jon Perrittdb00ad12014-09-30 16:29:50 -050069
Jon Perrittb5d13ad2014-10-06 16:39:27 -050070 _, err = BuildQueryString(map[string]interface{}{"Number": 4})
71 if err == nil {
72 t.Errorf("Expected error: 'Options type is not a struct'")
73 }
Jon Perrittdb00ad12014-09-30 16:29:50 -050074}
Jon Perrittc3e04b62014-10-06 16:17:49 -050075
76func TestBuildHeaders(t *testing.T) {
77 testStruct := struct {
78 Accept string `h:"Accept"`
Jon Perrittdfeb33b2014-10-06 16:28:58 -050079 Num int `h:"Number,required"`
Jon Perrittc3e04b62014-10-06 16:17:49 -050080 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 Perrittdfeb33b2014-10-06 16:28:58 -050090
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 Perrittc3e04b62014-10-06 16:17:49 -0500101}
102
103func 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 Hannafordf68c3e42014-11-18 13:02:09 +0100144}
Jon Perrittc3e04b62014-10-06 16:17:49 -0500145
Jamie Hannafordf68c3e42014-11-18 13:02:09 +0100146func 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 Perrittc3e04b62014-10-06 16:17:49 -0500158}