blob: a2fc980d3588920c361184ebe199e731fe1e4479 [file] [log] [blame]
Jon Perrittdf38cca2014-12-04 10:59:04 -07001package apiversions
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
Jon Perritt21b3eee2015-02-11 12:23:08 -07008 "github.com/rackspace/gophercloud"
Jon Perrittdf38cca2014-12-04 10:59:04 -07009 "github.com/rackspace/gophercloud/pagination"
10 th "github.com/rackspace/gophercloud/testhelper"
11 fake "github.com/rackspace/gophercloud/testhelper/client"
12)
13
14func 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 Perritt21b3eee2015-02-11 12:23:08 -070056 Links: []gophercloud.Link{
57 gophercloud.Link{
58 Href: "http://23.253.228.211:8000/v1",
Jon Perrittdf38cca2014-12-04 10:59:04 -070059 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
75func 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}