Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 1 | package apiversions |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 9 | th "github.com/rackspace/gophercloud/testhelper" |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 10 | fake "github.com/rackspace/gophercloud/testhelper/client" |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 11 | ) |
| 12 | |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 13 | func TestListVersions(t *testing.T) { |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 14 | th.SetupHTTP() |
| 15 | defer th.TeardownHTTP() |
| 16 | |
| 17 | th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 18 | th.TestMethod(t, r, "GET") |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 19 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 20 | |
| 21 | w.Header().Add("Content-Type", "application/json") |
| 22 | w.WriteHeader(http.StatusOK) |
| 23 | |
| 24 | fmt.Fprintf(w, ` |
| 25 | { |
| 26 | "versions": [ |
| 27 | { |
| 28 | "status": "CURRENT", |
| 29 | "id": "v2.0", |
| 30 | "links": [ |
| 31 | { |
| 32 | "href": "http://23.253.228.211:9696/v2.0", |
| 33 | "rel": "self" |
| 34 | } |
| 35 | ] |
| 36 | } |
| 37 | ] |
| 38 | }`) |
| 39 | }) |
| 40 | |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 41 | count := 0 |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 42 | |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 43 | ListVersions(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 44 | count++ |
| 45 | actual, err := ExtractAPIVersions(page) |
| 46 | if err != nil { |
| 47 | t.Errorf("Failed to extract API versions: %v", err) |
| 48 | return false, err |
| 49 | } |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 50 | |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 51 | expected := []APIVersion{ |
| 52 | APIVersion{ |
| 53 | Status: "CURRENT", |
| 54 | ID: "v2.0", |
| 55 | }, |
| 56 | } |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 57 | |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 58 | th.AssertDeepEquals(t, expected, actual) |
| 59 | |
| 60 | return true, nil |
| 61 | }) |
| 62 | |
| 63 | if count != 1 { |
| 64 | t.Errorf("Expected 1 page, got %d", count) |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 65 | } |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Jamie Hannaford | 47ea9c2 | 2014-10-08 12:01:38 +0200 | [diff] [blame] | 68 | func TestNonJSONCannotBeExtractedIntoAPIVersions(t *testing.T) { |
| 69 | th.SetupHTTP() |
| 70 | defer th.TeardownHTTP() |
| 71 | |
| 72 | th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 73 | w.WriteHeader(http.StatusOK) |
| 74 | }) |
| 75 | |
| 76 | ListVersions(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
| 77 | if _, err := ExtractAPIVersions(page); err == nil { |
| 78 | t.Fatalf("Expected error, got nil") |
| 79 | } |
| 80 | return true, nil |
| 81 | }) |
| 82 | } |
| 83 | |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 84 | func TestAPIInfo(t *testing.T) { |
| 85 | th.SetupHTTP() |
| 86 | defer th.TeardownHTTP() |
| 87 | |
| 88 | th.Mux.HandleFunc("/v2.0/", func(w http.ResponseWriter, r *http.Request) { |
| 89 | th.TestMethod(t, r, "GET") |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 90 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 91 | |
| 92 | w.Header().Add("Content-Type", "application/json") |
| 93 | w.WriteHeader(http.StatusOK) |
| 94 | |
| 95 | fmt.Fprintf(w, ` |
| 96 | { |
| 97 | "resources": [ |
| 98 | { |
| 99 | "links": [ |
| 100 | { |
| 101 | "href": "http://23.253.228.211:9696/v2.0/subnets", |
| 102 | "rel": "self" |
| 103 | } |
| 104 | ], |
| 105 | "name": "subnet", |
| 106 | "collection": "subnets" |
| 107 | }, |
| 108 | { |
| 109 | "links": [ |
| 110 | { |
| 111 | "href": "http://23.253.228.211:9696/v2.0/networks", |
| 112 | "rel": "self" |
| 113 | } |
| 114 | ], |
| 115 | "name": "network", |
| 116 | "collection": "networks" |
| 117 | }, |
| 118 | { |
| 119 | "links": [ |
| 120 | { |
| 121 | "href": "http://23.253.228.211:9696/v2.0/ports", |
| 122 | "rel": "self" |
| 123 | } |
| 124 | ], |
| 125 | "name": "port", |
| 126 | "collection": "ports" |
| 127 | } |
| 128 | ] |
| 129 | } |
| 130 | `) |
| 131 | }) |
| 132 | |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 133 | count := 0 |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 134 | |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 135 | ListVersionResources(fake.ServiceClient(), "v2.0").EachPage(func(page pagination.Page) (bool, error) { |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 136 | count++ |
| 137 | actual, err := ExtractVersionResources(page) |
| 138 | if err != nil { |
| 139 | t.Errorf("Failed to extract version resources: %v", err) |
| 140 | return false, err |
| 141 | } |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 142 | |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 143 | expected := []APIVersionResource{ |
| 144 | APIVersionResource{ |
| 145 | Name: "subnet", |
| 146 | Collection: "subnets", |
| 147 | }, |
| 148 | APIVersionResource{ |
| 149 | Name: "network", |
| 150 | Collection: "networks", |
| 151 | }, |
| 152 | APIVersionResource{ |
| 153 | Name: "port", |
| 154 | Collection: "ports", |
| 155 | }, |
| 156 | } |
| 157 | |
| 158 | th.AssertDeepEquals(t, expected, actual) |
| 159 | |
| 160 | return true, nil |
| 161 | }) |
| 162 | |
| 163 | if count != 1 { |
| 164 | t.Errorf("Expected 1 page, got %d", count) |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 165 | } |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 166 | } |
Jamie Hannaford | 47ea9c2 | 2014-10-08 12:01:38 +0200 | [diff] [blame] | 167 | |
| 168 | func TestNonJSONCannotBeExtractedIntoAPIVersionResources(t *testing.T) { |
| 169 | th.SetupHTTP() |
| 170 | defer th.TeardownHTTP() |
| 171 | |
| 172 | th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 173 | w.WriteHeader(http.StatusOK) |
| 174 | }) |
| 175 | |
| 176 | ListVersionResources(fake.ServiceClient(), "v2.0").EachPage(func(page pagination.Page) (bool, error) { |
| 177 | if _, err := ExtractVersionResources(page); err == nil { |
| 178 | t.Fatalf("Expected error, got nil") |
| 179 | } |
| 180 | return true, nil |
| 181 | }) |
| 182 | } |