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