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 | |
| 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 Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame^] | 56 | expected := testhelper.Endpoint() + "v3.0" |
| 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 | |
| 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 Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 80 | if endpoint != expected { |
| 81 | t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint) |
| 82 | } |
| 83 | } |