blob: c49b509833ce2bf13f7bc3397ca0d04abeebb6ed [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"
Jamie Hannaford58b008f2014-10-06 10:07:47 +020010 fake "github.com/rackspace/gophercloud/testhelper/client"
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020011)
12
Jamie Hannaford4721abc2014-09-16 16:29:04 +020013func TestListVersions(t *testing.T) {
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020014 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 Hannaford58b008f2014-10-06 10:07:47 +020019 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020020
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 Hannaford4721abc2014-09-16 16:29:04 +020041 count := 0
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020042
Jamie Hannaford58b008f2014-10-06 10:07:47 +020043 ListVersions(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020044 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 Hannaford1ce30f22014-09-16 11:23:34 +020050
Jamie Hannaford4721abc2014-09-16 16:29:04 +020051 expected := []APIVersion{
52 APIVersion{
53 Status: "CURRENT",
54 ID: "v2.0",
55 },
56 }
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020057
Jamie Hannaford4721abc2014-09-16 16:29:04 +020058 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 Hannaford1ce30f22014-09-16 11:23:34 +020065 }
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020066}
67
68func 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 Hannaford58b008f2014-10-06 10:07:47 +020074 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020075
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 Hannaford4721abc2014-09-16 16:29:04 +0200117 count := 0
Jamie Hannaford1ce30f22014-09-16 11:23:34 +0200118
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200119 ListVersionResources(fake.ServiceClient(), "v2.0").EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200120 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 Hannaford1ce30f22014-09-16 11:23:34 +0200126
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200127 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 Hannaford1ce30f22014-09-16 11:23:34 +0200149 }
Jamie Hannaford1ce30f22014-09-16 11:23:34 +0200150}