Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 1 | package extensions |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
Jamie Hannaford | f0c615b | 2014-09-17 10:56:52 +0200 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 9 | th "github.com/rackspace/gophercloud/testhelper" |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame^] | 10 | fake "github.com/rackspace/gophercloud/testhelper/client" |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 11 | ) |
| 12 | |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 13 | func TestList(t *testing.T) { |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 14 | th.SetupHTTP() |
| 15 | defer th.TeardownHTTP() |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 16 | |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 17 | th.Mux.HandleFunc("/v2.0/extensions", func(w http.ResponseWriter, r *http.Request) { |
| 18 | th.TestMethod(t, r, "GET") |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame^] | 19 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 20 | |
| 21 | w.Header().Add("Content-Type", "application/json") |
| 22 | |
| 23 | fmt.Fprintf(w, ` |
| 24 | { |
| 25 | "extensions": [ |
| 26 | { |
| 27 | "updated": "2013-01-20T00:00:00-00:00", |
| 28 | "name": "Neutron Service Type Management", |
| 29 | "links": [], |
| 30 | "namespace": "http://docs.openstack.org/ext/neutron/service-type/api/v1.0", |
| 31 | "alias": "service-type", |
| 32 | "description": "API for retrieving service providers for Neutron advanced services" |
| 33 | } |
| 34 | ] |
| 35 | } |
| 36 | `) |
| 37 | }) |
| 38 | |
| 39 | count := 0 |
| 40 | |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame^] | 41 | List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 42 | count++ |
| 43 | actual, err := ExtractExtensions(page) |
| 44 | if err != nil { |
| 45 | t.Errorf("Failed to extract extensions: %v", err) |
| 46 | } |
| 47 | |
| 48 | expected := []Extension{ |
| 49 | Extension{ |
| 50 | Updated: "2013-01-20T00:00:00-00:00", |
| 51 | Name: "Neutron Service Type Management", |
| 52 | Links: []interface{}{}, |
| 53 | Namespace: "http://docs.openstack.org/ext/neutron/service-type/api/v1.0", |
| 54 | Alias: "service-type", |
| 55 | Description: "API for retrieving service providers for Neutron advanced services", |
| 56 | }, |
| 57 | } |
| 58 | |
| 59 | th.AssertDeepEquals(t, expected, actual) |
| 60 | |
| 61 | return true, nil |
| 62 | }) |
| 63 | |
| 64 | if count != 1 { |
| 65 | t.Errorf("Expected 1 page, got %d", count) |
| 66 | } |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | func TestGet(t *testing.T) { |
| 70 | th.SetupHTTP() |
| 71 | defer th.TeardownHTTP() |
| 72 | |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 73 | th.Mux.HandleFunc("/v2.0/extensions/agent", func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 74 | th.TestMethod(t, r, "GET") |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame^] | 75 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 76 | |
| 77 | w.Header().Add("Content-Type", "application/json") |
| 78 | w.WriteHeader(http.StatusOK) |
| 79 | |
| 80 | fmt.Fprintf(w, ` |
| 81 | { |
| 82 | "extension": { |
| 83 | "updated": "2013-02-03T10:00:00-00:00", |
| 84 | "name": "agent", |
| 85 | "links": [], |
| 86 | "namespace": "http://docs.openstack.org/ext/agent/api/v2.0", |
| 87 | "alias": "agent", |
| 88 | "description": "The agent management extension." |
| 89 | } |
| 90 | } |
| 91 | `) |
| 92 | |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame^] | 93 | ext, err := Get(fake.ServiceClient(), "agent").Extract() |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 94 | th.AssertNoErr(t, err) |
| 95 | |
| 96 | th.AssertEquals(t, ext.Updated, "2013-02-03T10:00:00-00:00") |
| 97 | th.AssertEquals(t, ext.Name, "agent") |
| 98 | th.AssertEquals(t, ext.Namespace, "http://docs.openstack.org/ext/agent/api/v2.0") |
| 99 | th.AssertEquals(t, ext.Alias, "agent") |
| 100 | th.AssertEquals(t, ext.Description, "The agent management extension.") |
| 101 | }) |
| 102 | } |