blob: 56b5e4fc72bd768714eedc73e1c7b78a9390b95b [file] [log] [blame]
Jon Perrittfd53bba2014-10-03 00:41:22 -05001package apiversions
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
Jon Perrittfd53bba2014-10-03 00:41:22 -05008 "github.com/rackspace/gophercloud/pagination"
9 th "github.com/rackspace/gophercloud/testhelper"
Ash Wilsonff899c12014-10-22 09:14:30 -040010 "github.com/rackspace/gophercloud/testhelper/client"
Jon Perrittfd53bba2014-10-03 00:41:22 -050011)
12
Jon Perrittfd53bba2014-10-03 00:41:22 -050013func TestListVersions(t *testing.T) {
14 th.SetupHTTP()
15 defer th.TeardownHTTP()
16
17 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
18 th.TestMethod(t, r, "GET")
Ash Wilsonff899c12014-10-22 09:14:30 -040019 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Jon Perrittfd53bba2014-10-03 00:41:22 -050020
21 w.Header().Add("Content-Type", "application/json")
22 w.WriteHeader(http.StatusOK)
23
24 fmt.Fprintf(w, `{
25 "versions": [
26 {
27 "status": "CURRENT",
28 "updated": "2012-01-04T11:33:21Z",
29 "id": "v1.0",
30 "links": [
31 {
32 "href": "http://23.253.228.211:8776/v1/",
33 "rel": "self"
34 }
35 ]
36 },
37 {
38 "status": "CURRENT",
39 "updated": "2012-11-21T11:33:21Z",
40 "id": "v2.0",
41 "links": [
42 {
43 "href": "http://23.253.228.211:8776/v2/",
44 "rel": "self"
45 }
46 ]
47 }
48 ]
49 }`)
50 })
51
52 count := 0
53
Ash Wilsonff899c12014-10-22 09:14:30 -040054 List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittfd53bba2014-10-03 00:41:22 -050055 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 }
61
62 expected := []APIVersion{
63 APIVersion{
64 ID: "v1.0",
65 Status: "CURRENT",
66 Updated: "2012-01-04T11:33:21Z",
67 },
68 APIVersion{
69 ID: "v2.0",
70 Status: "CURRENT",
71 Updated: "2012-11-21T11:33:21Z",
72 },
73 }
74
75 th.AssertDeepEquals(t, expected, actual)
76
77 return true, nil
78 })
79
80 if count != 1 {
81 t.Errorf("Expected 1 page, got %d", count)
82 }
83}
84
85func TestAPIInfo(t *testing.T) {
86 th.SetupHTTP()
87 defer th.TeardownHTTP()
88
89 th.Mux.HandleFunc("/v1/", func(w http.ResponseWriter, r *http.Request) {
90 th.TestMethod(t, r, "GET")
Ash Wilsonff899c12014-10-22 09:14:30 -040091 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Jon Perrittfd53bba2014-10-03 00:41:22 -050092
93 w.Header().Add("Content-Type", "application/json")
94 w.WriteHeader(http.StatusOK)
95
96 fmt.Fprintf(w, `{
97 "version": {
98 "status": "CURRENT",
99 "updated": "2012-01-04T11:33:21Z",
100 "media-types": [
101 {
102 "base": "application/xml",
103 "type": "application/vnd.openstack.volume+xml;version=1"
104 },
105 {
106 "base": "application/json",
107 "type": "application/vnd.openstack.volume+json;version=1"
108 }
109 ],
110 "id": "v1.0",
111 "links": [
112 {
113 "href": "http://23.253.228.211:8776/v1/",
114 "rel": "self"
115 },
116 {
117 "href": "http://jorgew.github.com/block-storage-api/content/os-block-storage-1.0.pdf",
118 "type": "application/pdf",
119 "rel": "describedby"
120 },
121 {
122 "href": "http://docs.rackspacecloud.com/servers/api/v1.1/application.wadl",
123 "type": "application/vnd.sun.wadl+xml",
124 "rel": "describedby"
125 }
126 ]
127 }
128 }`)
129 })
130
Ash Wilsonff899c12014-10-22 09:14:30 -0400131 actual, err := Get(client.ServiceClient(), "v1").Extract()
Jon Perrittfd53bba2014-10-03 00:41:22 -0500132 if err != nil {
133 t.Errorf("Failed to extract version: %v", err)
134 }
135
136 expected := APIVersion{
137 ID: "v1.0",
138 Status: "CURRENT",
139 Updated: "2012-01-04T11:33:21Z",
140 }
141
142 th.AssertEquals(t, actual.ID, expected.ID)
143 th.AssertEquals(t, actual.Status, expected.Status)
144 th.AssertEquals(t, actual.Updated, expected.Updated)
145}