github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/orchestration/v1/apiversions/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "context" 5 "fmt" 6 "net/http" 7 "testing" 8 9 "github.com/vnpaycloud-console/gophercloud/v2" 10 "github.com/vnpaycloud-console/gophercloud/v2/openstack/orchestration/v1/apiversions" 11 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 12 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 13 fake "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client" 14 ) 15 16 func TestListVersions(t *testing.T) { 17 th.SetupHTTP() 18 defer th.TeardownHTTP() 19 20 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 21 th.TestMethod(t, r, "GET") 22 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 23 24 w.Header().Add("Content-Type", "application/json") 25 w.WriteHeader(http.StatusOK) 26 27 fmt.Fprint(w, ` 28 { 29 "versions": [ 30 { 31 "status": "CURRENT", 32 "id": "v1.0", 33 "links": [ 34 { 35 "href": "http://23.253.228.211:8000/v1", 36 "rel": "self" 37 } 38 ] 39 } 40 ] 41 }`) 42 }) 43 44 count := 0 45 46 err := apiversions.ListVersions(fake.ServiceClient()).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { 47 count++ 48 actual, err := apiversions.ExtractAPIVersions(page) 49 if err != nil { 50 t.Errorf("Failed to extract API versions: %v", err) 51 return false, err 52 } 53 54 expected := []apiversions.APIVersion{ 55 { 56 Status: "CURRENT", 57 ID: "v1.0", 58 Links: []gophercloud.Link{ 59 { 60 Href: "http://23.253.228.211:8000/v1", 61 Rel: "self", 62 }, 63 }, 64 }, 65 } 66 67 th.AssertDeepEquals(t, expected, actual) 68 69 return true, nil 70 }) 71 th.AssertNoErr(t, err) 72 73 if count != 1 { 74 t.Errorf("Expected 1 page, got %d", count) 75 } 76 } 77 78 func TestNonJSONCannotBeExtractedIntoAPIVersions(t *testing.T) { 79 th.SetupHTTP() 80 defer th.TeardownHTTP() 81 82 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 83 w.WriteHeader(http.StatusOK) 84 }) 85 86 err := apiversions.ListVersions(fake.ServiceClient()).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { 87 if _, err := apiversions.ExtractAPIVersions(page); err == nil { 88 t.Fatalf("Expected error, got nil") 89 } 90 return true, nil 91 }) 92 th.AssertErr(t, err) 93 }