github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/apiversions/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/huaweicloud/golangsdk/openstack/networking/v2/apiversions" 9 "github.com/huaweicloud/golangsdk/pagination" 10 th "github.com/huaweicloud/golangsdk/testhelper" 11 fake "github.com/huaweicloud/golangsdk/testhelper/client" 12 ) 13 14 func TestListVersions(t *testing.T) { 15 th.SetupHTTP() 16 defer th.TeardownHTTP() 17 18 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 19 th.TestMethod(t, r, "GET") 20 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 21 22 w.Header().Add("Content-Type", "application/json") 23 w.WriteHeader(http.StatusOK) 24 25 fmt.Fprintf(w, ` 26 { 27 "versions": [ 28 { 29 "status": "CURRENT", 30 "id": "v2.0", 31 "links": [ 32 { 33 "href": "http://23.253.228.211:9696/v2.0", 34 "rel": "self" 35 } 36 ] 37 } 38 ] 39 }`) 40 }) 41 42 count := 0 43 44 apiversions.ListVersions(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { 45 count++ 46 actual, err := apiversions.ExtractAPIVersions(page) 47 if err != nil { 48 t.Errorf("Failed to extract API versions: %v", err) 49 return false, err 50 } 51 52 expected := []apiversions.APIVersion{ 53 { 54 Status: "CURRENT", 55 ID: "v2.0", 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 } 67 } 68 69 func TestNonJSONCannotBeExtractedIntoAPIVersions(t *testing.T) { 70 th.SetupHTTP() 71 defer th.TeardownHTTP() 72 73 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 74 w.WriteHeader(http.StatusOK) 75 }) 76 77 apiversions.ListVersions(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { 78 if _, err := apiversions.ExtractAPIVersions(page); err == nil { 79 t.Fatalf("Expected error, got nil") 80 } 81 return true, nil 82 }) 83 } 84 85 func TestAPIInfo(t *testing.T) { 86 th.SetupHTTP() 87 defer th.TeardownHTTP() 88 89 th.Mux.HandleFunc("/v2.0/", func(w http.ResponseWriter, r *http.Request) { 90 th.TestMethod(t, r, "GET") 91 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 92 93 w.Header().Add("Content-Type", "application/json") 94 w.WriteHeader(http.StatusOK) 95 96 fmt.Fprintf(w, ` 97 { 98 "resources": [ 99 { 100 "links": [ 101 { 102 "href": "http://23.253.228.211:9696/v2.0/subnets", 103 "rel": "self" 104 } 105 ], 106 "name": "subnet", 107 "collection": "subnets" 108 }, 109 { 110 "links": [ 111 { 112 "href": "http://23.253.228.211:9696/v2.0/networks", 113 "rel": "self" 114 } 115 ], 116 "name": "network", 117 "collection": "networks" 118 }, 119 { 120 "links": [ 121 { 122 "href": "http://23.253.228.211:9696/v2.0/ports", 123 "rel": "self" 124 } 125 ], 126 "name": "port", 127 "collection": "ports" 128 } 129 ] 130 } 131 `) 132 }) 133 134 count := 0 135 136 apiversions.ListVersionResources(fake.ServiceClient(), "v2.0").EachPage(func(page pagination.Page) (bool, error) { 137 count++ 138 actual, err := apiversions.ExtractVersionResources(page) 139 if err != nil { 140 t.Errorf("Failed to extract version resources: %v", err) 141 return false, err 142 } 143 144 expected := []apiversions.APIVersionResource{ 145 { 146 Name: "subnet", 147 Collection: "subnets", 148 }, 149 { 150 Name: "network", 151 Collection: "networks", 152 }, 153 { 154 Name: "port", 155 Collection: "ports", 156 }, 157 } 158 159 th.AssertDeepEquals(t, expected, actual) 160 161 return true, nil 162 }) 163 164 if count != 1 { 165 t.Errorf("Expected 1 page, got %d", count) 166 } 167 } 168 169 func TestNonJSONCannotBeExtractedIntoAPIVersionResources(t *testing.T) { 170 th.SetupHTTP() 171 defer th.TeardownHTTP() 172 173 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 174 w.WriteHeader(http.StatusOK) 175 }) 176 177 apiversions.ListVersionResources(fake.ServiceClient(), "v2.0").EachPage(func(page pagination.Page) (bool, error) { 178 if _, err := apiversions.ExtractVersionResources(page); err == nil { 179 t.Fatalf("Expected error, got nil") 180 } 181 return true, nil 182 }) 183 }