Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 1 | package utils |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 8 | "github.com/gophercloud/gophercloud" |
| 9 | "github.com/gophercloud/gophercloud/testhelper" |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 10 | ) |
| 11 | |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 12 | func 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 Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 39 | func TestChooseVersion(t *testing.T) { |
| 40 | testhelper.SetupHTTP() |
| 41 | defer testhelper.TeardownHTTP() |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 42 | setupVersionHandler() |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 43 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 44 | v2 := &Version{ID: "v2.0", Priority: 2, Suffix: "blarg"} |
| 45 | v3 := &Version{ID: "v3.0", Priority: 3, Suffix: "hargl"} |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 46 | |
Ash Wilson | 2491b4c | 2015-02-12 16:13:39 -0500 | [diff] [blame] | 47 | c := &gophercloud.ProviderClient{ |
| 48 | IdentityBase: testhelper.Endpoint(), |
| 49 | IdentityEndpoint: "", |
| 50 | } |
| 51 | v, endpoint, err := ChooseVersion(c, []*Version{v2, v3}) |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 52 | |
| 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 Wilson | e7da01c | 2014-09-09 12:31:06 -0400 | [diff] [blame] | 61 | expected := testhelper.Endpoint() + "v3.0/" |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 62 | if endpoint != expected { |
| 63 | t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func TestChooseVersionOpinionatedLink(t *testing.T) { |
| 68 | testhelper.SetupHTTP() |
| 69 | defer testhelper.TeardownHTTP() |
| 70 | setupVersionHandler() |
| 71 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 72 | v2 := &Version{ID: "v2.0", Priority: 2, Suffix: "nope"} |
| 73 | v3 := &Version{ID: "v3.0", Priority: 3, Suffix: "northis"} |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 74 | |
Ash Wilson | 2491b4c | 2015-02-12 16:13:39 -0500 | [diff] [blame] | 75 | c := &gophercloud.ProviderClient{ |
| 76 | IdentityBase: testhelper.Endpoint(), |
| 77 | IdentityEndpoint: testhelper.Endpoint() + "v2.0/", |
| 78 | } |
| 79 | v, endpoint, err := ChooseVersion(c, []*Version{v2, v3}) |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 80 | 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 | |
| 94 | func 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 Wilson | 2491b4c | 2015-02-12 16:13:39 -0500 | [diff] [blame] | 101 | c := &gophercloud.ProviderClient{ |
| 102 | IdentityBase: testhelper.Endpoint(), |
| 103 | IdentityEndpoint: testhelper.Endpoint() + "v2.0/", |
| 104 | } |
| 105 | v, endpoint, err := ChooseVersion(c, []*Version{v2, v3}) |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 106 | 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 Wilson | e7da01c | 2014-09-09 12:31:06 -0400 | [diff] [blame] | 114 | expected := testhelper.Endpoint() + "v2.0/" |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 115 | if endpoint != expected { |
| 116 | t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint) |
| 117 | } |
| 118 | } |