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 | |
| 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/pagination" |
| 10 | th "github.com/rackspace/gophercloud/testhelper" |
| 11 | ) |
| 12 | |
| 13 | const TokenID = "123" |
| 14 | |
| 15 | func ServiceClient() *gophercloud.ServiceClient { |
| 16 | return &gophercloud.ServiceClient{ |
| 17 | Provider: &gophercloud.ProviderClient{ |
| 18 | TokenID: TokenID, |
| 19 | }, |
| 20 | Endpoint: th.Endpoint(), |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func TestListVersions(t *testing.T) { |
| 25 | th.SetupHTTP() |
| 26 | defer th.TeardownHTTP() |
| 27 | |
| 28 | th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 29 | th.TestMethod(t, r, "GET") |
| 30 | th.TestHeader(t, r, "X-Auth-Token", TokenID) |
| 31 | |
| 32 | w.Header().Add("Content-Type", "application/json") |
| 33 | w.WriteHeader(http.StatusOK) |
| 34 | |
| 35 | fmt.Fprintf(w, `{ |
| 36 | "versions": [ |
| 37 | { |
| 38 | "status": "CURRENT", |
| 39 | "updated": "2012-01-04T11:33:21Z", |
| 40 | "id": "v1.0", |
| 41 | "links": [ |
| 42 | { |
| 43 | "href": "http://23.253.228.211:8776/v1/", |
| 44 | "rel": "self" |
| 45 | } |
| 46 | ] |
| 47 | }, |
| 48 | { |
| 49 | "status": "CURRENT", |
| 50 | "updated": "2012-11-21T11:33:21Z", |
| 51 | "id": "v2.0", |
| 52 | "links": [ |
| 53 | { |
| 54 | "href": "http://23.253.228.211:8776/v2/", |
| 55 | "rel": "self" |
| 56 | } |
| 57 | ] |
| 58 | } |
| 59 | ] |
| 60 | }`) |
| 61 | }) |
| 62 | |
| 63 | count := 0 |
| 64 | |
| 65 | List(ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
| 66 | count++ |
| 67 | actual, err := ExtractAPIVersions(page) |
| 68 | if err != nil { |
| 69 | t.Errorf("Failed to extract API versions: %v", err) |
| 70 | return false, err |
| 71 | } |
| 72 | |
| 73 | expected := []APIVersion{ |
| 74 | APIVersion{ |
| 75 | ID: "v1.0", |
| 76 | Status: "CURRENT", |
| 77 | Updated: "2012-01-04T11:33:21Z", |
| 78 | }, |
| 79 | APIVersion{ |
| 80 | ID: "v2.0", |
| 81 | Status: "CURRENT", |
| 82 | Updated: "2012-11-21T11:33:21Z", |
| 83 | }, |
| 84 | } |
| 85 | |
| 86 | th.AssertDeepEquals(t, expected, actual) |
| 87 | |
| 88 | return true, nil |
| 89 | }) |
| 90 | |
| 91 | if count != 1 { |
| 92 | t.Errorf("Expected 1 page, got %d", count) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func TestAPIInfo(t *testing.T) { |
| 97 | th.SetupHTTP() |
| 98 | defer th.TeardownHTTP() |
| 99 | |
| 100 | th.Mux.HandleFunc("/v1/", func(w http.ResponseWriter, r *http.Request) { |
| 101 | th.TestMethod(t, r, "GET") |
| 102 | th.TestHeader(t, r, "X-Auth-Token", TokenID) |
| 103 | |
| 104 | w.Header().Add("Content-Type", "application/json") |
| 105 | w.WriteHeader(http.StatusOK) |
| 106 | |
| 107 | fmt.Fprintf(w, `{ |
| 108 | "version": { |
| 109 | "status": "CURRENT", |
| 110 | "updated": "2012-01-04T11:33:21Z", |
| 111 | "media-types": [ |
| 112 | { |
| 113 | "base": "application/xml", |
| 114 | "type": "application/vnd.openstack.volume+xml;version=1" |
| 115 | }, |
| 116 | { |
| 117 | "base": "application/json", |
| 118 | "type": "application/vnd.openstack.volume+json;version=1" |
| 119 | } |
| 120 | ], |
| 121 | "id": "v1.0", |
| 122 | "links": [ |
| 123 | { |
| 124 | "href": "http://23.253.228.211:8776/v1/", |
| 125 | "rel": "self" |
| 126 | }, |
| 127 | { |
| 128 | "href": "http://jorgew.github.com/block-storage-api/content/os-block-storage-1.0.pdf", |
| 129 | "type": "application/pdf", |
| 130 | "rel": "describedby" |
| 131 | }, |
| 132 | { |
| 133 | "href": "http://docs.rackspacecloud.com/servers/api/v1.1/application.wadl", |
| 134 | "type": "application/vnd.sun.wadl+xml", |
| 135 | "rel": "describedby" |
| 136 | } |
| 137 | ] |
| 138 | } |
| 139 | }`) |
| 140 | }) |
| 141 | |
| 142 | actual, err := Get(ServiceClient(), "v1").Extract() |
| 143 | if err != nil { |
| 144 | t.Errorf("Failed to extract version: %v", err) |
| 145 | } |
| 146 | |
| 147 | expected := APIVersion{ |
| 148 | ID: "v1.0", |
| 149 | Status: "CURRENT", |
| 150 | Updated: "2012-01-04T11:33:21Z", |
| 151 | } |
| 152 | |
| 153 | th.AssertEquals(t, actual.ID, expected.ID) |
| 154 | th.AssertEquals(t, actual.Status, expected.Status) |
| 155 | th.AssertEquals(t, actual.Updated, expected.Updated) |
| 156 | } |