| Jon Perritt | df38cca | 2014-12-04 10:59:04 -0700 | [diff] [blame] | 1 | package apiversions | 
 | 2 |  | 
 | 3 | import ( | 
 | 4 | 	"fmt" | 
 | 5 | 	"net/http" | 
 | 6 | 	"testing" | 
 | 7 |  | 
| Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 8 | 	"github.com/gophercloud/gophercloud" | 
 | 9 | 	"github.com/gophercloud/gophercloud/pagination" | 
 | 10 | 	th "github.com/gophercloud/gophercloud/testhelper" | 
 | 11 | 	fake "github.com/gophercloud/gophercloud/testhelper/client" | 
| Jon Perritt | df38cca | 2014-12-04 10:59:04 -0700 | [diff] [blame] | 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": "v1.0", | 
 | 31 |             "links": [ | 
 | 32 |                 { | 
 | 33 |                     "href": "http://23.253.228.211:8000/v1", | 
 | 34 |                     "rel": "self" | 
 | 35 |                 } | 
 | 36 |             ] | 
 | 37 |         } | 
 | 38 |     ] | 
 | 39 | }`) | 
 | 40 | 	}) | 
 | 41 |  | 
 | 42 | 	count := 0 | 
 | 43 |  | 
 | 44 | 	ListVersions(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { | 
 | 45 | 		count++ | 
 | 46 | 		actual, err := ExtractAPIVersions(page) | 
 | 47 | 		if err != nil { | 
 | 48 | 			t.Errorf("Failed to extract API versions: %v", err) | 
 | 49 | 			return false, err | 
 | 50 | 		} | 
 | 51 |  | 
 | 52 | 		expected := []APIVersion{ | 
 | 53 | 			APIVersion{ | 
 | 54 | 				Status: "CURRENT", | 
 | 55 | 				ID:     "v1.0", | 
| Jon Perritt | 21b3eee | 2015-02-11 12:23:08 -0700 | [diff] [blame] | 56 | 				Links: []gophercloud.Link{ | 
 | 57 | 					gophercloud.Link{ | 
 | 58 | 						Href: "http://23.253.228.211:8000/v1", | 
| Jon Perritt | df38cca | 2014-12-04 10:59:04 -0700 | [diff] [blame] | 59 | 						Rel:  "self", | 
 | 60 | 					}, | 
 | 61 | 				}, | 
 | 62 | 			}, | 
 | 63 | 		} | 
 | 64 |  | 
 | 65 | 		th.AssertDeepEquals(t, expected, actual) | 
 | 66 |  | 
 | 67 | 		return true, nil | 
 | 68 | 	}) | 
 | 69 |  | 
 | 70 | 	if count != 1 { | 
 | 71 | 		t.Errorf("Expected 1 page, got %d", count) | 
 | 72 | 	} | 
 | 73 | } | 
 | 74 |  | 
 | 75 | func TestNonJSONCannotBeExtractedIntoAPIVersions(t *testing.T) { | 
 | 76 | 	th.SetupHTTP() | 
 | 77 | 	defer th.TeardownHTTP() | 
 | 78 |  | 
 | 79 | 	th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | 
 | 80 | 		w.WriteHeader(http.StatusOK) | 
 | 81 | 	}) | 
 | 82 |  | 
 | 83 | 	ListVersions(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { | 
 | 84 | 		if _, err := ExtractAPIVersions(page); err == nil { | 
 | 85 | 			t.Fatalf("Expected error, got nil") | 
 | 86 | 		} | 
 | 87 | 		return true, nil | 
 | 88 | 	}) | 
 | 89 | } |