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