blob: 9552696232cdf59815d111f468bc583cc816196d [file] [log] [blame]
Ash Wilsonc4360202014-08-29 14:14:24 -04001package utils
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
8 "github.com/rackspace/gophercloud/testhelper"
9)
10
Ash Wilsona0c4c842014-09-09 11:30:58 -040011func setupVersionHandler() {
12 testhelper.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
13 fmt.Fprintf(w, `
14 {
15 "versions": {
16 "values": [
17 {
18 "status": "stable",
19 "id": "v3.0",
20 "links": [
21 { "href": "%s/v3.0", "rel": "self" }
22 ]
23 },
24 {
25 "status": "stable",
26 "id": "v2.0",
27 "links": [
28 { "href": "%s/v2.0", "rel": "self" }
29 ]
30 }
31 ]
32 }
33 }
34 `, testhelper.Server.URL, testhelper.Server.URL)
35 })
36}
37
Ash Wilsonc4360202014-08-29 14:14:24 -040038func TestChooseVersion(t *testing.T) {
39 testhelper.SetupHTTP()
40 defer testhelper.TeardownHTTP()
Ash Wilsona0c4c842014-09-09 11:30:58 -040041 setupVersionHandler()
Ash Wilsonc4360202014-08-29 14:14:24 -040042
Ash Wilson09694b92014-09-09 14:08:27 -040043 v2 := &Version{ID: "v2.0", Priority: 2, Suffix: "blarg"}
44 v3 := &Version{ID: "v3.0", Priority: 3, Suffix: "hargl"}
Ash Wilsonc4360202014-08-29 14:14:24 -040045
Ash Wilson09694b92014-09-09 14:08:27 -040046 v, endpoint, err := ChooseVersion(testhelper.Endpoint(), "", []*Version{v2, v3})
Ash Wilsonc4360202014-08-29 14:14:24 -040047
48 if err != nil {
49 t.Fatalf("Unexpected error from ChooseVersion: %v", err)
50 }
51
52 if v != v3 {
53 t.Errorf("Expected %#v to win, but %#v did instead", v3, v)
54 }
55
Ash Wilsone7da01c2014-09-09 12:31:06 -040056 expected := testhelper.Endpoint() + "v3.0/"
Ash Wilsona0c4c842014-09-09 11:30:58 -040057 if endpoint != expected {
58 t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint)
59 }
60}
61
62func TestChooseVersionOpinionatedLink(t *testing.T) {
63 testhelper.SetupHTTP()
64 defer testhelper.TeardownHTTP()
65 setupVersionHandler()
66
Ash Wilson09694b92014-09-09 14:08:27 -040067 v2 := &Version{ID: "v2.0", Priority: 2, Suffix: "nope"}
68 v3 := &Version{ID: "v3.0", Priority: 3, Suffix: "northis"}
Ash Wilsona0c4c842014-09-09 11:30:58 -040069
Ash Wilson09694b92014-09-09 14:08:27 -040070 v, endpoint, err := ChooseVersion(testhelper.Endpoint(), testhelper.Endpoint()+"v2.0/", []*Version{v2, v3})
71 if err != nil {
72 t.Fatalf("Unexpected error from ChooseVersion: %v", err)
73 }
74
75 if v != v2 {
76 t.Errorf("Expected %#v to win, but %#v did instead", v2, v)
77 }
78
79 expected := testhelper.Endpoint() + "v2.0/"
80 if endpoint != expected {
81 t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint)
82 }
83}
84
85func TestChooseVersionFromSuffix(t *testing.T) {
86 testhelper.SetupHTTP()
87 defer testhelper.TeardownHTTP()
88
89 v2 := &Version{ID: "v2.0", Priority: 2, Suffix: "/v2.0/"}
90 v3 := &Version{ID: "v3.0", Priority: 3, Suffix: "/v3.0/"}
91
92 v, endpoint, err := ChooseVersion(testhelper.Endpoint(), testhelper.Endpoint()+"v2.0/", []*Version{v2, v3})
Ash Wilsona0c4c842014-09-09 11:30:58 -040093 if err != nil {
94 t.Fatalf("Unexpected error from ChooseVersion: %v", err)
95 }
96
97 if v != v2 {
98 t.Errorf("Expected %#v to win, but %#v did instead", v2, v)
99 }
100
Ash Wilsone7da01c2014-09-09 12:31:06 -0400101 expected := testhelper.Endpoint() + "v2.0/"
Ash Wilsonc4360202014-08-29 14:14:24 -0400102 if endpoint != expected {
103 t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint)
104 }
105}