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 | |
| 11 | func TestChooseVersion(t *testing.T) { |
| 12 | testhelper.SetupHTTP() |
| 13 | defer testhelper.TeardownHTTP() |
| 14 | |
| 15 | testhelper.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 16 | fmt.Fprintf(w, ` |
| 17 | { |
| 18 | "versions": { |
| 19 | "values": [ |
| 20 | { |
| 21 | "status": "stable", |
| 22 | "id": "v3.0", |
| 23 | "links": [ |
| 24 | { "href": "https://example.com:1000/", "rel": "self" } |
| 25 | ] |
| 26 | }, |
| 27 | { |
| 28 | "status": "stable", |
| 29 | "id": "v2.0", |
| 30 | "links": [ |
| 31 | { "href": "https://example.com:2000/", "rel": "self" } |
| 32 | ] |
| 33 | } |
| 34 | ] |
| 35 | } |
| 36 | } |
| 37 | `) |
| 38 | }) |
| 39 | |
| 40 | v2 := &Version{ID: "v2.0", Priority: 2} |
| 41 | v3 := &Version{ID: "v3.0", Priority: 3} |
| 42 | |
| 43 | v, endpoint, err := ChooseVersion(testhelper.Endpoint(), []*Version{v2, v3}) |
| 44 | |
| 45 | if err != nil { |
| 46 | t.Fatalf("Unexpected error from ChooseVersion: %v", err) |
| 47 | } |
| 48 | |
| 49 | if v != v3 { |
| 50 | t.Errorf("Expected %#v to win, but %#v did instead", v3, v) |
| 51 | } |
| 52 | |
| 53 | expected := "https://example.com:1000/" |
| 54 | if endpoint != expected { |
| 55 | t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint) |
| 56 | } |
| 57 | } |