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