blob: d35af9f0c6b294193a6128af7a43f6330b369d12 [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
Jamie Hannaford47ea9c22014-10-08 12:01:38 +020068func TestNonJSONCannotBeExtractedIntoAPIVersions(t *testing.T) {
69 th.SetupHTTP()
70 defer th.TeardownHTTP()
71
72 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
73 w.WriteHeader(http.StatusOK)
74 })
75
76 ListVersions(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
77 if _, err := ExtractAPIVersions(page); err == nil {
78 t.Fatalf("Expected error, got nil")
79 }
80 return true, nil
81 })
82}
83
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020084func TestAPIInfo(t *testing.T) {
85 th.SetupHTTP()
86 defer th.TeardownHTTP()
87
88 th.Mux.HandleFunc("/v2.0/", func(w http.ResponseWriter, r *http.Request) {
89 th.TestMethod(t, r, "GET")
Jamie Hannaford58b008f2014-10-06 10:07:47 +020090 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020091
92 w.Header().Add("Content-Type", "application/json")
93 w.WriteHeader(http.StatusOK)
94
95 fmt.Fprintf(w, `
96{
97 "resources": [
98 {
99 "links": [
100 {
101 "href": "http://23.253.228.211:9696/v2.0/subnets",
102 "rel": "self"
103 }
104 ],
105 "name": "subnet",
106 "collection": "subnets"
107 },
108 {
109 "links": [
110 {
111 "href": "http://23.253.228.211:9696/v2.0/networks",
112 "rel": "self"
113 }
114 ],
115 "name": "network",
116 "collection": "networks"
117 },
118 {
119 "links": [
120 {
121 "href": "http://23.253.228.211:9696/v2.0/ports",
122 "rel": "self"
123 }
124 ],
125 "name": "port",
126 "collection": "ports"
127 }
128 ]
129}
130 `)
131 })
132
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200133 count := 0
Jamie Hannaford1ce30f22014-09-16 11:23:34 +0200134
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200135 ListVersionResources(fake.ServiceClient(), "v2.0").EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200136 count++
137 actual, err := ExtractVersionResources(page)
138 if err != nil {
139 t.Errorf("Failed to extract version resources: %v", err)
140 return false, err
141 }
Jamie Hannaford1ce30f22014-09-16 11:23:34 +0200142
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200143 expected := []APIVersionResource{
144 APIVersionResource{
145 Name: "subnet",
146 Collection: "subnets",
147 },
148 APIVersionResource{
149 Name: "network",
150 Collection: "networks",
151 },
152 APIVersionResource{
153 Name: "port",
154 Collection: "ports",
155 },
156 }
157
158 th.AssertDeepEquals(t, expected, actual)
159
160 return true, nil
161 })
162
163 if count != 1 {
164 t.Errorf("Expected 1 page, got %d", count)
Jamie Hannaford1ce30f22014-09-16 11:23:34 +0200165 }
Jamie Hannaford1ce30f22014-09-16 11:23:34 +0200166}
Jamie Hannaford47ea9c22014-10-08 12:01:38 +0200167
168func TestNonJSONCannotBeExtractedIntoAPIVersionResources(t *testing.T) {
169 th.SetupHTTP()
170 defer th.TeardownHTTP()
171
172 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
173 w.WriteHeader(http.StatusOK)
174 })
175
176 ListVersionResources(fake.ServiceClient(), "v2.0").EachPage(func(page pagination.Page) (bool, error) {
177 if _, err := ExtractVersionResources(page); err == nil {
178 t.Fatalf("Expected error, got nil")
179 }
180 return true, nil
181 })
182}