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 | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 8 | "github.com/gophercloud/gophercloud/pagination" |
| 9 | th "github.com/gophercloud/gophercloud/testhelper" |
| 10 | "github.com/gophercloud/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) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 57 | th.AssertNoErr(t, err) |
Jon Perritt | fd53bba | 2014-10-03 00:41:22 -0500 | [diff] [blame] | 58 | |
| 59 | expected := []APIVersion{ |
| 60 | APIVersion{ |
| 61 | ID: "v1.0", |
| 62 | Status: "CURRENT", |
| 63 | Updated: "2012-01-04T11:33:21Z", |
| 64 | }, |
| 65 | APIVersion{ |
| 66 | ID: "v2.0", |
| 67 | Status: "CURRENT", |
| 68 | Updated: "2012-11-21T11:33:21Z", |
| 69 | }, |
| 70 | } |
| 71 | |
| 72 | th.AssertDeepEquals(t, expected, actual) |
| 73 | |
| 74 | return true, nil |
| 75 | }) |
| 76 | |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 77 | th.AssertEquals(t, 1, count) |
Jon Perritt | fd53bba | 2014-10-03 00:41:22 -0500 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | func TestAPIInfo(t *testing.T) { |
| 81 | th.SetupHTTP() |
| 82 | defer th.TeardownHTTP() |
| 83 | |
| 84 | th.Mux.HandleFunc("/v1/", func(w http.ResponseWriter, r *http.Request) { |
| 85 | th.TestMethod(t, r, "GET") |
Ash Wilson | ff899c1 | 2014-10-22 09:14:30 -0400 | [diff] [blame] | 86 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
Jon Perritt | fd53bba | 2014-10-03 00:41:22 -0500 | [diff] [blame] | 87 | |
| 88 | w.Header().Add("Content-Type", "application/json") |
| 89 | w.WriteHeader(http.StatusOK) |
| 90 | |
| 91 | fmt.Fprintf(w, `{ |
| 92 | "version": { |
| 93 | "status": "CURRENT", |
| 94 | "updated": "2012-01-04T11:33:21Z", |
| 95 | "media-types": [ |
| 96 | { |
| 97 | "base": "application/xml", |
| 98 | "type": "application/vnd.openstack.volume+xml;version=1" |
| 99 | }, |
| 100 | { |
| 101 | "base": "application/json", |
| 102 | "type": "application/vnd.openstack.volume+json;version=1" |
| 103 | } |
| 104 | ], |
| 105 | "id": "v1.0", |
| 106 | "links": [ |
| 107 | { |
| 108 | "href": "http://23.253.228.211:8776/v1/", |
| 109 | "rel": "self" |
| 110 | }, |
| 111 | { |
| 112 | "href": "http://jorgew.github.com/block-storage-api/content/os-block-storage-1.0.pdf", |
| 113 | "type": "application/pdf", |
| 114 | "rel": "describedby" |
| 115 | }, |
| 116 | { |
| 117 | "href": "http://docs.rackspacecloud.com/servers/api/v1.1/application.wadl", |
| 118 | "type": "application/vnd.sun.wadl+xml", |
| 119 | "rel": "describedby" |
| 120 | } |
| 121 | ] |
| 122 | } |
| 123 | }`) |
| 124 | }) |
| 125 | |
Ash Wilson | ff899c1 | 2014-10-22 09:14:30 -0400 | [diff] [blame] | 126 | actual, err := Get(client.ServiceClient(), "v1").Extract() |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 127 | th.AssertNoErr(t, err) |
Jon Perritt | fd53bba | 2014-10-03 00:41:22 -0500 | [diff] [blame] | 128 | |
| 129 | expected := APIVersion{ |
| 130 | ID: "v1.0", |
| 131 | Status: "CURRENT", |
| 132 | Updated: "2012-01-04T11:33:21Z", |
| 133 | } |
| 134 | |
| 135 | th.AssertEquals(t, actual.ID, expected.ID) |
| 136 | th.AssertEquals(t, actual.Status, expected.Status) |
| 137 | th.AssertEquals(t, actual.Updated, expected.Updated) |
| 138 | } |