blob: 7c713e13ea8e0bbccc220d805531987f8f9f6cdc [file] [log] [blame]
Jamie Hannaford4721abc2014-09-16 16:29:04 +02001package apiversions
Jamie Hannaford1ce30f22014-09-16 11:23:34 +02002
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
8 "github.com/rackspace/gophercloud"
Jamie Hannafordf0c615b2014-09-17 10:56:52 +02009 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020010 th "github.com/rackspace/gophercloud/testhelper"
11)
12
13const TokenID = "123"
14
15func ServiceClient() *gophercloud.ServiceClient {
16 return &gophercloud.ServiceClient{
17 Provider: &gophercloud.ProviderClient{
18 TokenID: TokenID,
19 },
20 Endpoint: th.Endpoint(),
21 }
22}
23
Jamie Hannaford4721abc2014-09-16 16:29:04 +020024func TestListVersions(t *testing.T) {
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020025 th.SetupHTTP()
26 defer th.TeardownHTTP()
27
28 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
29 th.TestMethod(t, r, "GET")
30 th.TestHeader(t, r, "X-Auth-Token", TokenID)
31
32 w.Header().Add("Content-Type", "application/json")
33 w.WriteHeader(http.StatusOK)
34
35 fmt.Fprintf(w, `
36{
37 "versions": [
38 {
39 "status": "CURRENT",
40 "id": "v2.0",
41 "links": [
42 {
43 "href": "http://23.253.228.211:9696/v2.0",
44 "rel": "self"
45 }
46 ]
47 }
48 ]
49}`)
50 })
51
Jamie Hannaford4721abc2014-09-16 16:29:04 +020052 count := 0
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020053
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020054 ListVersions(ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020055 count++
56 actual, err := ExtractAPIVersions(page)
57 if err != nil {
58 t.Errorf("Failed to extract API versions: %v", err)
59 return false, err
60 }
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020061
Jamie Hannaford4721abc2014-09-16 16:29:04 +020062 expected := []APIVersion{
63 APIVersion{
64 Status: "CURRENT",
65 ID: "v2.0",
66 },
67 }
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020068
Jamie Hannaford4721abc2014-09-16 16:29:04 +020069 th.AssertDeepEquals(t, expected, actual)
70
71 return true, nil
72 })
73
74 if count != 1 {
75 t.Errorf("Expected 1 page, got %d", count)
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020076 }
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020077}
78
79func TestAPIInfo(t *testing.T) {
80 th.SetupHTTP()
81 defer th.TeardownHTTP()
82
83 th.Mux.HandleFunc("/v2.0/", func(w http.ResponseWriter, r *http.Request) {
84 th.TestMethod(t, r, "GET")
85 th.TestHeader(t, r, "X-Auth-Token", TokenID)
86
87 w.Header().Add("Content-Type", "application/json")
88 w.WriteHeader(http.StatusOK)
89
90 fmt.Fprintf(w, `
91{
92 "resources": [
93 {
94 "links": [
95 {
96 "href": "http://23.253.228.211:9696/v2.0/subnets",
97 "rel": "self"
98 }
99 ],
100 "name": "subnet",
101 "collection": "subnets"
102 },
103 {
104 "links": [
105 {
106 "href": "http://23.253.228.211:9696/v2.0/networks",
107 "rel": "self"
108 }
109 ],
110 "name": "network",
111 "collection": "networks"
112 },
113 {
114 "links": [
115 {
116 "href": "http://23.253.228.211:9696/v2.0/ports",
117 "rel": "self"
118 }
119 ],
120 "name": "port",
121 "collection": "ports"
122 }
123 ]
124}
125 `)
126 })
127
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200128 count := 0
Jamie Hannaford1ce30f22014-09-16 11:23:34 +0200129
Jamie Hannafordf0c615b2014-09-17 10:56:52 +0200130 ListVersionResources(ServiceClient(), "v2.0").EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200131 count++
132 actual, err := ExtractVersionResources(page)
133 if err != nil {
134 t.Errorf("Failed to extract version resources: %v", err)
135 return false, err
136 }
Jamie Hannaford1ce30f22014-09-16 11:23:34 +0200137
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200138 expected := []APIVersionResource{
139 APIVersionResource{
140 Name: "subnet",
141 Collection: "subnets",
142 },
143 APIVersionResource{
144 Name: "network",
145 Collection: "networks",
146 },
147 APIVersionResource{
148 Name: "port",
149 Collection: "ports",
150 },
151 }
152
153 th.AssertDeepEquals(t, expected, actual)
154
155 return true, nil
156 })
157
158 if count != 1 {
159 t.Errorf("Expected 1 page, got %d", count)
Jamie Hannaford1ce30f22014-09-16 11:23:34 +0200160 }
Jamie Hannaford1ce30f22014-09-16 11:23:34 +0200161}