Jon Perritt | fd53bba | 2014-10-03 00:41:22 -0500 | [diff] [blame] | 1 | package apiversions |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
Jon Perritt | fd53bba | 2014-10-03 00:41:22 -0500 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/pagination" |
| 9 | th "github.com/rackspace/gophercloud/testhelper" |
Ash Wilson | ff899c1 | 2014-10-22 09:14:30 -0400 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud/testhelper/client" |
Jon Perritt | fd53bba | 2014-10-03 00:41:22 -0500 | [diff] [blame] | 11 | ) |
| 12 | |
Jon Perritt | fd53bba | 2014-10-03 00:41:22 -0500 | [diff] [blame] | 13 | func 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 Wilson | ff899c1 | 2014-10-22 09:14:30 -0400 | [diff] [blame] | 19 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
Jon Perritt | fd53bba | 2014-10-03 00:41:22 -0500 | [diff] [blame] | 20 | |
| 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 Wilson | ff899c1 | 2014-10-22 09:14:30 -0400 | [diff] [blame] | 54 | List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | fd53bba | 2014-10-03 00:41:22 -0500 | [diff] [blame] | 55 | 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 | |
| 85 | func 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 Wilson | ff899c1 | 2014-10-22 09:14:30 -0400 | [diff] [blame] | 91 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
Jon Perritt | fd53bba | 2014-10-03 00:41:22 -0500 | [diff] [blame] | 92 | |
| 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 Wilson | ff899c1 | 2014-10-22 09:14:30 -0400 | [diff] [blame] | 131 | actual, err := Get(client.ServiceClient(), "v1").Extract() |
Jon Perritt | fd53bba | 2014-10-03 00:41:22 -0500 | [diff] [blame] | 132 | 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 | } |