Ash Wilson | 61dcb02 | 2014-10-03 08:15:47 -0400 | [diff] [blame^] | 1 | package extensions |
| 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 TestList(t *testing.T) { |
| 25 | th.SetupHTTP() |
| 26 | defer th.TeardownHTTP() |
| 27 | |
| 28 | th.Mux.HandleFunc("/v2.0/extensions", 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 | |
| 34 | fmt.Fprintf(w, ` |
| 35 | { |
| 36 | "extensions": [ |
| 37 | { |
| 38 | "updated": "2013-01-20T00:00:00-00:00", |
| 39 | "name": "Neutron Service Type Management", |
| 40 | "links": [], |
| 41 | "namespace": "http://docs.openstack.org/ext/neutron/service-type/api/v1.0", |
| 42 | "alias": "service-type", |
| 43 | "description": "API for retrieving service providers for Neutron advanced services" |
| 44 | } |
| 45 | ] |
| 46 | } |
| 47 | `) |
| 48 | }) |
| 49 | |
| 50 | count := 0 |
| 51 | |
| 52 | List(ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
| 53 | count++ |
| 54 | actual, err := ExtractExtensions(page) |
| 55 | if err != nil { |
| 56 | t.Errorf("Failed to extract extensions: %v", err) |
| 57 | } |
| 58 | |
| 59 | expected := []Extension{ |
| 60 | Extension{ |
| 61 | Updated: "2013-01-20T00:00:00-00:00", |
| 62 | Name: "Neutron Service Type Management", |
| 63 | Links: []interface{}{}, |
| 64 | Namespace: "http://docs.openstack.org/ext/neutron/service-type/api/v1.0", |
| 65 | Alias: "service-type", |
| 66 | Description: "API for retrieving service providers for Neutron advanced services", |
| 67 | }, |
| 68 | } |
| 69 | |
| 70 | th.AssertDeepEquals(t, expected, actual) |
| 71 | |
| 72 | return true, nil |
| 73 | }) |
| 74 | |
| 75 | if count != 1 { |
| 76 | t.Errorf("Expected 1 page, got %d", count) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func TestGet(t *testing.T) { |
| 81 | th.SetupHTTP() |
| 82 | defer th.TeardownHTTP() |
| 83 | |
| 84 | th.Mux.HandleFunc("/v2.0/extensions/agent", func(w http.ResponseWriter, r *http.Request) { |
| 85 | th.TestMethod(t, r, "GET") |
| 86 | th.TestHeader(t, r, "X-Auth-Token", TokenID) |
| 87 | |
| 88 | w.Header().Add("Content-Type", "application/json") |
| 89 | w.WriteHeader(http.StatusOK) |
| 90 | |
| 91 | fmt.Fprintf(w, ` |
| 92 | { |
| 93 | "extension": { |
| 94 | "updated": "2013-02-03T10:00:00-00:00", |
| 95 | "name": "agent", |
| 96 | "links": [], |
| 97 | "namespace": "http://docs.openstack.org/ext/agent/api/v2.0", |
| 98 | "alias": "agent", |
| 99 | "description": "The agent management extension." |
| 100 | } |
| 101 | } |
| 102 | `) |
| 103 | |
| 104 | ext, err := Get(ServiceClient(), "agent").Extract() |
| 105 | th.AssertNoErr(t, err) |
| 106 | |
| 107 | th.AssertEquals(t, ext.Updated, "2013-02-03T10:00:00-00:00") |
| 108 | th.AssertEquals(t, ext.Name, "agent") |
| 109 | th.AssertEquals(t, ext.Namespace, "http://docs.openstack.org/ext/agent/api/v2.0") |
| 110 | th.AssertEquals(t, ext.Alias, "agent") |
| 111 | th.AssertEquals(t, ext.Description, "The agent management extension.") |
| 112 | }) |
| 113 | } |