blob: 0922318f802af23b361cdee47d81ff6e994e28a0 [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"
9 th "github.com/rackspace/gophercloud/testhelper"
10)
11
12const TokenID = "123"
13
14func ServiceClient() *gophercloud.ServiceClient {
15 return &gophercloud.ServiceClient{
16 Provider: &gophercloud.ProviderClient{
17 TokenID: TokenID,
18 },
19 Endpoint: th.Endpoint(),
20 }
21}
22
Jamie Hannaford4721abc2014-09-16 16:29:04 +020023func TestListVersions(t *testing.T) {
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020024 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 Hannaford4721abc2014-09-16 16:29:04 +020051 count := 0
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020052
Jamie Hannaford4721abc2014-09-16 16:29:04 +020053 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 Hannaford1ce30f22014-09-16 11:23:34 +020060
Jamie Hannaford4721abc2014-09-16 16:29:04 +020061 expected := []APIVersion{
62 APIVersion{
63 Status: "CURRENT",
64 ID: "v2.0",
65 },
66 }
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020067
Jamie Hannaford4721abc2014-09-16 16:29:04 +020068 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 Hannaford1ce30f22014-09-16 11:23:34 +020075 }
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020076}
77
78func 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 Hannaford4721abc2014-09-16 16:29:04 +0200127 count := 0
Jamie Hannaford1ce30f22014-09-16 11:23:34 +0200128
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200129 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 Hannaford1ce30f22014-09-16 11:23:34 +0200136
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200137 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 Hannaford1ce30f22014-09-16 11:23:34 +0200159 }
Jamie Hannaford1ce30f22014-09-16 11:23:34 +0200160}