blob: db21772e64f34cb1dacc2f369c636d595d206422 [file] [log] [blame]
Jamie Hannaford4721abc2014-09-16 16:29:04 +02001package extensions
Jamie Hannaford1ce30f22014-09-16 11:23:34 +02002
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
Jamie Hannafordf0c615b2014-09-17 10:56:52 +02008 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford1ce30f22014-09-16 11:23:34 +02009 th "github.com/rackspace/gophercloud/testhelper"
Jamie Hannaford58b008f2014-10-06 10:07:47 +020010 fake "github.com/rackspace/gophercloud/testhelper/client"
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020011)
12
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020013func TestList(t *testing.T) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020014 th.SetupHTTP()
15 defer th.TeardownHTTP()
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020016
Jamie Hannaford4721abc2014-09-16 16:29:04 +020017 th.Mux.HandleFunc("/v2.0/extensions", func(w http.ResponseWriter, r *http.Request) {
18 th.TestMethod(t, r, "GET")
Jamie Hannaford58b008f2014-10-06 10:07:47 +020019 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannaford4721abc2014-09-16 16:29:04 +020020
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 Hannaford58b008f2014-10-06 10:07:47 +020041 List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020042 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 Hannaford1ce30f22014-09-16 11:23:34 +020067}
68
69func TestGet(t *testing.T) {
70 th.SetupHTTP()
71 defer th.TeardownHTTP()
72
Jamie Hannaford4721abc2014-09-16 16:29:04 +020073 th.Mux.HandleFunc("/v2.0/extensions/agent", func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020074 th.TestMethod(t, r, "GET")
Jamie Hannaford58b008f2014-10-06 10:07:47 +020075 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020076
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 Hannaford58b008f2014-10-06 10:07:47 +020093 ext, err := Get(fake.ServiceClient(), "agent").Extract()
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020094 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}