blob: 9f2f3639d420e5930703e9cb11da0164abd122d6 [file] [log] [blame]
Ash Wilsonc4360202014-08-29 14:14:24 -04001package utils
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
Jon Perritt27249f42016-02-18 10:35:59 -06008 "github.com/gophercloud/gophercloud"
9 "github.com/gophercloud/gophercloud/testhelper"
Ash Wilsonc4360202014-08-29 14:14:24 -040010)
11
Ash Wilsona0c4c842014-09-09 11:30:58 -040012func setupVersionHandler() {
13 testhelper.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
14 fmt.Fprintf(w, `
15 {
16 "versions": {
17 "values": [
18 {
19 "status": "stable",
20 "id": "v3.0",
21 "links": [
22 { "href": "%s/v3.0", "rel": "self" }
23 ]
24 },
25 {
26 "status": "stable",
27 "id": "v2.0",
28 "links": [
29 { "href": "%s/v2.0", "rel": "self" }
30 ]
31 }
32 ]
33 }
34 }
35 `, testhelper.Server.URL, testhelper.Server.URL)
36 })
37}
38
Ash Wilsonc4360202014-08-29 14:14:24 -040039func TestChooseVersion(t *testing.T) {
40 testhelper.SetupHTTP()
41 defer testhelper.TeardownHTTP()
Ash Wilsona0c4c842014-09-09 11:30:58 -040042 setupVersionHandler()
Ash Wilsonc4360202014-08-29 14:14:24 -040043
Ash Wilson09694b92014-09-09 14:08:27 -040044 v2 := &Version{ID: "v2.0", Priority: 2, Suffix: "blarg"}
45 v3 := &Version{ID: "v3.0", Priority: 3, Suffix: "hargl"}
Ash Wilsonc4360202014-08-29 14:14:24 -040046
Ash Wilson2491b4c2015-02-12 16:13:39 -050047 c := &gophercloud.ProviderClient{
48 IdentityBase: testhelper.Endpoint(),
49 IdentityEndpoint: "",
50 }
51 v, endpoint, err := ChooseVersion(c, []*Version{v2, v3})
Ash Wilsonc4360202014-08-29 14:14:24 -040052
53 if err != nil {
54 t.Fatalf("Unexpected error from ChooseVersion: %v", err)
55 }
56
57 if v != v3 {
58 t.Errorf("Expected %#v to win, but %#v did instead", v3, v)
59 }
60
Ash Wilsone7da01c2014-09-09 12:31:06 -040061 expected := testhelper.Endpoint() + "v3.0/"
Ash Wilsona0c4c842014-09-09 11:30:58 -040062 if endpoint != expected {
63 t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint)
64 }
65}
66
67func TestChooseVersionOpinionatedLink(t *testing.T) {
68 testhelper.SetupHTTP()
69 defer testhelper.TeardownHTTP()
70 setupVersionHandler()
71
Ash Wilson09694b92014-09-09 14:08:27 -040072 v2 := &Version{ID: "v2.0", Priority: 2, Suffix: "nope"}
73 v3 := &Version{ID: "v3.0", Priority: 3, Suffix: "northis"}
Ash Wilsona0c4c842014-09-09 11:30:58 -040074
Ash Wilson2491b4c2015-02-12 16:13:39 -050075 c := &gophercloud.ProviderClient{
76 IdentityBase: testhelper.Endpoint(),
77 IdentityEndpoint: testhelper.Endpoint() + "v2.0/",
78 }
79 v, endpoint, err := ChooseVersion(c, []*Version{v2, v3})
Ash Wilson09694b92014-09-09 14:08:27 -040080 if err != nil {
81 t.Fatalf("Unexpected error from ChooseVersion: %v", err)
82 }
83
84 if v != v2 {
85 t.Errorf("Expected %#v to win, but %#v did instead", v2, v)
86 }
87
88 expected := testhelper.Endpoint() + "v2.0/"
89 if endpoint != expected {
90 t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint)
91 }
92}
93
94func TestChooseVersionFromSuffix(t *testing.T) {
95 testhelper.SetupHTTP()
96 defer testhelper.TeardownHTTP()
97
98 v2 := &Version{ID: "v2.0", Priority: 2, Suffix: "/v2.0/"}
99 v3 := &Version{ID: "v3.0", Priority: 3, Suffix: "/v3.0/"}
100
Ash Wilson2491b4c2015-02-12 16:13:39 -0500101 c := &gophercloud.ProviderClient{
102 IdentityBase: testhelper.Endpoint(),
103 IdentityEndpoint: testhelper.Endpoint() + "v2.0/",
104 }
105 v, endpoint, err := ChooseVersion(c, []*Version{v2, v3})
Ash Wilsona0c4c842014-09-09 11:30:58 -0400106 if err != nil {
107 t.Fatalf("Unexpected error from ChooseVersion: %v", err)
108 }
109
110 if v != v2 {
111 t.Errorf("Expected %#v to win, but %#v did instead", v2, v)
112 }
113
Ash Wilsone7da01c2014-09-09 12:31:06 -0400114 expected := testhelper.Endpoint() + "v2.0/"
Ash Wilsonc4360202014-08-29 14:14:24 -0400115 if endpoint != expected {
116 t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint)
117 }
118}