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 | |
| 68 | func TestAPIInfo(t *testing.T) { |
| 69 | th.SetupHTTP() |
| 70 | defer th.TeardownHTTP() |
| 71 | |
| 72 | th.Mux.HandleFunc("/v2.0/", func(w http.ResponseWriter, r *http.Request) { |
| 73 | th.TestMethod(t, r, "GET") |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 74 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 75 | |
| 76 | w.Header().Add("Content-Type", "application/json") |
| 77 | w.WriteHeader(http.StatusOK) |
| 78 | |
| 79 | fmt.Fprintf(w, ` |
| 80 | { |
| 81 | "resources": [ |
| 82 | { |
| 83 | "links": [ |
| 84 | { |
| 85 | "href": "http://23.253.228.211:9696/v2.0/subnets", |
| 86 | "rel": "self" |
| 87 | } |
| 88 | ], |
| 89 | "name": "subnet", |
| 90 | "collection": "subnets" |
| 91 | }, |
| 92 | { |
| 93 | "links": [ |
| 94 | { |
| 95 | "href": "http://23.253.228.211:9696/v2.0/networks", |
| 96 | "rel": "self" |
| 97 | } |
| 98 | ], |
| 99 | "name": "network", |
| 100 | "collection": "networks" |
| 101 | }, |
| 102 | { |
| 103 | "links": [ |
| 104 | { |
| 105 | "href": "http://23.253.228.211:9696/v2.0/ports", |
| 106 | "rel": "self" |
| 107 | } |
| 108 | ], |
| 109 | "name": "port", |
| 110 | "collection": "ports" |
| 111 | } |
| 112 | ] |
| 113 | } |
| 114 | `) |
| 115 | }) |
| 116 | |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 117 | count := 0 |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 118 | |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 119 | ListVersionResources(fake.ServiceClient(), "v2.0").EachPage(func(page pagination.Page) (bool, error) { |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 120 | count++ |
| 121 | actual, err := ExtractVersionResources(page) |
| 122 | if err != nil { |
| 123 | t.Errorf("Failed to extract version resources: %v", err) |
| 124 | return false, err |
| 125 | } |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 126 | |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 127 | expected := []APIVersionResource{ |
| 128 | APIVersionResource{ |
| 129 | Name: "subnet", |
| 130 | Collection: "subnets", |
| 131 | }, |
| 132 | APIVersionResource{ |
| 133 | Name: "network", |
| 134 | Collection: "networks", |
| 135 | }, |
| 136 | APIVersionResource{ |
| 137 | Name: "port", |
| 138 | Collection: "ports", |
| 139 | }, |
| 140 | } |
| 141 | |
| 142 | th.AssertDeepEquals(t, expected, actual) |
| 143 | |
| 144 | return true, nil |
| 145 | }) |
| 146 | |
| 147 | if count != 1 { |
| 148 | t.Errorf("Expected 1 page, got %d", count) |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 149 | } |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 150 | } |