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