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 | |
| 8 | "github.com/rackspace/gophercloud/testhelper" |
| 9 | ) |
| 10 | |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 11 | func 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 Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 38 | func TestChooseVersion(t *testing.T) { |
| 39 | testhelper.SetupHTTP() |
| 40 | defer testhelper.TeardownHTTP() |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 41 | setupVersionHandler() |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 42 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 43 | v2 := &Version{ID: "v2.0", Priority: 2, Suffix: "blarg"} |
| 44 | v3 := &Version{ID: "v3.0", Priority: 3, Suffix: "hargl"} |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 45 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 46 | v, endpoint, err := ChooseVersion(testhelper.Endpoint(), "", []*Version{v2, v3}) |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 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 Wilson | e7da01c | 2014-09-09 12:31:06 -0400 | [diff] [blame] | 56 | expected := testhelper.Endpoint() + "v3.0/" |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 57 | if endpoint != expected { |
| 58 | t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func TestChooseVersionOpinionatedLink(t *testing.T) { |
| 63 | testhelper.SetupHTTP() |
| 64 | defer testhelper.TeardownHTTP() |
| 65 | setupVersionHandler() |
| 66 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 67 | v2 := &Version{ID: "v2.0", Priority: 2, Suffix: "nope"} |
| 68 | v3 := &Version{ID: "v3.0", Priority: 3, Suffix: "northis"} |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 69 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 70 | 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 | |
| 85 | func 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 Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 93 | 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 Wilson | e7da01c | 2014-09-09 12:31:06 -0400 | [diff] [blame] | 101 | expected := testhelper.Endpoint() + "v2.0/" |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 102 | if endpoint != expected { |
| 103 | t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint) |
| 104 | } |
| 105 | } |