jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 1 | package testing |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 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" |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 9 | "github.com/gophercloud/gophercloud/openstack/utils" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 10 | "github.com/gophercloud/gophercloud/testhelper" |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 11 | ) |
| 12 | |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 13 | func setupVersionHandler() { |
| 14 | testhelper.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 15 | fmt.Fprintf(w, ` |
| 16 | { |
| 17 | "versions": { |
| 18 | "values": [ |
| 19 | { |
| 20 | "status": "stable", |
| 21 | "id": "v3.0", |
| 22 | "links": [ |
| 23 | { "href": "%s/v3.0", "rel": "self" } |
| 24 | ] |
| 25 | }, |
| 26 | { |
| 27 | "status": "stable", |
| 28 | "id": "v2.0", |
| 29 | "links": [ |
| 30 | { "href": "%s/v2.0", "rel": "self" } |
| 31 | ] |
| 32 | } |
| 33 | ] |
| 34 | } |
| 35 | } |
| 36 | `, testhelper.Server.URL, testhelper.Server.URL) |
| 37 | }) |
| 38 | } |
| 39 | |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 40 | func TestChooseVersion(t *testing.T) { |
| 41 | testhelper.SetupHTTP() |
| 42 | defer testhelper.TeardownHTTP() |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 43 | setupVersionHandler() |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 44 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 45 | v2 := &utils.Version{ID: "v2.0", Priority: 2, Suffix: "blarg"} |
| 46 | v3 := &utils.Version{ID: "v3.0", Priority: 3, Suffix: "hargl"} |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 47 | |
Ash Wilson | 2491b4c | 2015-02-12 16:13:39 -0500 | [diff] [blame] | 48 | c := &gophercloud.ProviderClient{ |
| 49 | IdentityBase: testhelper.Endpoint(), |
| 50 | IdentityEndpoint: "", |
| 51 | } |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 52 | v, endpoint, err := utils.ChooseVersion(c, []*utils.Version{v2, v3}) |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 53 | |
| 54 | if err != nil { |
| 55 | t.Fatalf("Unexpected error from ChooseVersion: %v", err) |
| 56 | } |
| 57 | |
| 58 | if v != v3 { |
| 59 | t.Errorf("Expected %#v to win, but %#v did instead", v3, v) |
| 60 | } |
| 61 | |
Ash Wilson | e7da01c | 2014-09-09 12:31:06 -0400 | [diff] [blame] | 62 | expected := testhelper.Endpoint() + "v3.0/" |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 63 | if endpoint != expected { |
| 64 | t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestChooseVersionOpinionatedLink(t *testing.T) { |
| 69 | testhelper.SetupHTTP() |
| 70 | defer testhelper.TeardownHTTP() |
| 71 | setupVersionHandler() |
| 72 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 73 | v2 := &utils.Version{ID: "v2.0", Priority: 2, Suffix: "nope"} |
| 74 | v3 := &utils.Version{ID: "v3.0", Priority: 3, Suffix: "northis"} |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 75 | |
Ash Wilson | 2491b4c | 2015-02-12 16:13:39 -0500 | [diff] [blame] | 76 | c := &gophercloud.ProviderClient{ |
| 77 | IdentityBase: testhelper.Endpoint(), |
| 78 | IdentityEndpoint: testhelper.Endpoint() + "v2.0/", |
| 79 | } |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 80 | v, endpoint, err := utils.ChooseVersion(c, []*utils.Version{v2, v3}) |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 81 | if err != nil { |
| 82 | t.Fatalf("Unexpected error from ChooseVersion: %v", err) |
| 83 | } |
| 84 | |
| 85 | if v != v2 { |
| 86 | t.Errorf("Expected %#v to win, but %#v did instead", v2, v) |
| 87 | } |
| 88 | |
| 89 | expected := testhelper.Endpoint() + "v2.0/" |
| 90 | if endpoint != expected { |
| 91 | t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func TestChooseVersionFromSuffix(t *testing.T) { |
| 96 | testhelper.SetupHTTP() |
| 97 | defer testhelper.TeardownHTTP() |
| 98 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 99 | v2 := &utils.Version{ID: "v2.0", Priority: 2, Suffix: "/v2.0/"} |
| 100 | v3 := &utils.Version{ID: "v3.0", Priority: 3, Suffix: "/v3.0/"} |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 101 | |
Ash Wilson | 2491b4c | 2015-02-12 16:13:39 -0500 | [diff] [blame] | 102 | c := &gophercloud.ProviderClient{ |
| 103 | IdentityBase: testhelper.Endpoint(), |
| 104 | IdentityEndpoint: testhelper.Endpoint() + "v2.0/", |
| 105 | } |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 106 | v, endpoint, err := utils.ChooseVersion(c, []*utils.Version{v2, v3}) |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 107 | if err != nil { |
| 108 | t.Fatalf("Unexpected error from ChooseVersion: %v", err) |
| 109 | } |
| 110 | |
| 111 | if v != v2 { |
| 112 | t.Errorf("Expected %#v to win, but %#v did instead", v2, v) |
| 113 | } |
| 114 | |
Ash Wilson | e7da01c | 2014-09-09 12:31:06 -0400 | [diff] [blame] | 115 | expected := testhelper.Endpoint() + "v2.0/" |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 116 | if endpoint != expected { |
| 117 | t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint) |
| 118 | } |
| 119 | } |