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