blob: cec6a22aebeedf1e9f77dbc418af181adcd8b30b [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
Jamie Hannafordf0c615b2014-09-17 10:56:52 +02008 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford1ce30f22014-09-16 11:23:34 +02009 th "github.com/rackspace/gophercloud/testhelper"
10)
11
Jamie Hannaford4721abc2014-09-16 16:29:04 +020012func TestListVersions(t *testing.T) {
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020013 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 Hannaford09cc0a72014-10-02 17:28:25 +020018 th.TestHeader(t, r, "X-Auth-Token", th.TokenID)
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020019
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 Hannaford4721abc2014-09-16 16:29:04 +020040 count := 0
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020041
Jamie Hannaford09cc0a72014-10-02 17:28:25 +020042 ListVersions(th.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020043 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 Hannaford1ce30f22014-09-16 11:23:34 +020049
Jamie Hannaford4721abc2014-09-16 16:29:04 +020050 expected := []APIVersion{
51 APIVersion{
52 Status: "CURRENT",
53 ID: "v2.0",
54 },
55 }
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020056
Jamie Hannaford4721abc2014-09-16 16:29:04 +020057 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 Hannaford1ce30f22014-09-16 11:23:34 +020064 }
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020065}
66
67func 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 Hannaford09cc0a72014-10-02 17:28:25 +020073 th.TestHeader(t, r, "X-Auth-Token", th.TokenID)
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020074
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 Hannaford4721abc2014-09-16 16:29:04 +0200116 count := 0
Jamie Hannaford1ce30f22014-09-16 11:23:34 +0200117
Jamie Hannaford09cc0a72014-10-02 17:28:25 +0200118 ListVersionResources(th.ServiceClient(), "v2.0").EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200119 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 Hannaford1ce30f22014-09-16 11:23:34 +0200125
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200126 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 Hannaford1ce30f22014-09-16 11:23:34 +0200148 }
Jamie Hannaford1ce30f22014-09-16 11:23:34 +0200149}