blob: d2aec8adc14d853b050d9f225cbfc41266e72033 [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
43 v2 := &Version{ID: "v2.0", Priority: 2}
44 v3 := &Version{ID: "v3.0", Priority: 3}
45
46 v, endpoint, err := ChooseVersion(testhelper.Endpoint(), []*Version{v2, v3})
47
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 Wilsona0c4c842014-09-09 11:30:58 -040056 expected := testhelper.Endpoint() + "v3.0"
57 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
67 v2 := &Version{ID: "v2.0", Priority: 2}
68 v3 := &Version{ID: "v3.0", Priority: 3}
69
70 v, endpoint, err := ChooseVersion(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"
Ash Wilsonc4360202014-08-29 14:14:24 -040080 if endpoint != expected {
81 t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint)
82 }
83}