Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 1 | // +build fixtures |
| 2 | |
| 3 | package containers |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "testing" |
| 9 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 10 | th "github.com/gophercloud/gophercloud/testhelper" |
| 11 | fake "github.com/gophercloud/gophercloud/testhelper/client" |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 12 | ) |
| 13 | |
| 14 | // ExpectedListInfo is the result expected from a call to `List` when full |
| 15 | // info is requested. |
| 16 | var ExpectedListInfo = []Container{ |
| 17 | Container{ |
| 18 | Count: 0, |
| 19 | Bytes: 0, |
| 20 | Name: "janeausten", |
| 21 | }, |
| 22 | Container{ |
| 23 | Count: 1, |
| 24 | Bytes: 14, |
| 25 | Name: "marktwain", |
| 26 | }, |
| 27 | } |
| 28 | |
| 29 | // ExpectedListNames is the result expected from a call to `List` when just |
| 30 | // container names are requested. |
| 31 | var ExpectedListNames = []string{"janeausten", "marktwain"} |
| 32 | |
| 33 | // HandleListContainerInfoSuccessfully creates an HTTP handler at `/` on the test handler mux that |
| 34 | // responds with a `List` response when full info is requested. |
| 35 | func HandleListContainerInfoSuccessfully(t *testing.T) { |
| 36 | th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 37 | th.TestMethod(t, r, "GET") |
| 38 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 39 | th.TestHeader(t, r, "Accept", "application/json") |
| 40 | |
| 41 | w.Header().Set("Content-Type", "application/json") |
| 42 | r.ParseForm() |
| 43 | marker := r.Form.Get("marker") |
| 44 | switch marker { |
| 45 | case "": |
| 46 | fmt.Fprintf(w, `[ |
| 47 | { |
| 48 | "count": 0, |
| 49 | "bytes": 0, |
| 50 | "name": "janeausten" |
| 51 | }, |
| 52 | { |
| 53 | "count": 1, |
| 54 | "bytes": 14, |
| 55 | "name": "marktwain" |
| 56 | } |
| 57 | ]`) |
Jon Perritt | fe5e735 | 2015-02-18 13:51:01 -0700 | [diff] [blame] | 58 | case "janeausten": |
| 59 | fmt.Fprintf(w, `[ |
| 60 | { |
| 61 | "count": 1, |
| 62 | "bytes": 14, |
| 63 | "name": "marktwain" |
| 64 | } |
| 65 | ]`) |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 66 | case "marktwain": |
| 67 | fmt.Fprintf(w, `[]`) |
| 68 | default: |
| 69 | t.Fatalf("Unexpected marker: [%s]", marker) |
| 70 | } |
| 71 | }) |
| 72 | } |
| 73 | |
| 74 | // HandleListContainerNamesSuccessfully creates an HTTP handler at `/` on the test handler mux that |
| 75 | // responds with a `ListNames` response when only container names are requested. |
| 76 | func HandleListContainerNamesSuccessfully(t *testing.T) { |
| 77 | th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 78 | th.TestMethod(t, r, "GET") |
| 79 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 80 | th.TestHeader(t, r, "Accept", "text/plain") |
| 81 | |
| 82 | w.Header().Set("Content-Type", "text/plain") |
| 83 | r.ParseForm() |
| 84 | marker := r.Form.Get("marker") |
| 85 | switch marker { |
| 86 | case "": |
| 87 | fmt.Fprintf(w, "janeausten\nmarktwain\n") |
Jon Perritt | fe5e735 | 2015-02-18 13:51:01 -0700 | [diff] [blame] | 88 | case "janeausten": |
| 89 | fmt.Fprintf(w, "marktwain\n") |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 90 | case "marktwain": |
| 91 | fmt.Fprintf(w, ``) |
| 92 | default: |
| 93 | t.Fatalf("Unexpected marker: [%s]", marker) |
| 94 | } |
| 95 | }) |
| 96 | } |
| 97 | |
| 98 | // HandleCreateContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that |
| 99 | // responds with a `Create` response. |
| 100 | func HandleCreateContainerSuccessfully(t *testing.T) { |
| 101 | th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 102 | th.TestMethod(t, r, "PUT") |
| 103 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 104 | th.TestHeader(t, r, "Accept", "application/json") |
| 105 | |
| 106 | w.Header().Add("X-Container-Meta-Foo", "bar") |
Jon Perritt | 59b0ea4 | 2015-01-31 20:23:33 -0700 | [diff] [blame] | 107 | w.Header().Add("X-Trans-Id", "1234567") |
Jon Perritt | 457f8ca | 2014-10-15 00:28:23 -0500 | [diff] [blame] | 108 | w.WriteHeader(http.StatusNoContent) |
| 109 | }) |
| 110 | } |
| 111 | |
| 112 | // HandleDeleteContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that |
| 113 | // responds with a `Delete` response. |
| 114 | func HandleDeleteContainerSuccessfully(t *testing.T) { |
| 115 | th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 116 | th.TestMethod(t, r, "DELETE") |
| 117 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 118 | th.TestHeader(t, r, "Accept", "application/json") |
| 119 | w.WriteHeader(http.StatusNoContent) |
| 120 | }) |
| 121 | } |
| 122 | |
| 123 | // HandleUpdateContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that |
| 124 | // responds with a `Update` response. |
| 125 | func HandleUpdateContainerSuccessfully(t *testing.T) { |
| 126 | th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 127 | th.TestMethod(t, r, "POST") |
| 128 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 129 | th.TestHeader(t, r, "Accept", "application/json") |
| 130 | w.WriteHeader(http.StatusNoContent) |
| 131 | }) |
| 132 | } |
| 133 | |
| 134 | // HandleGetContainerSuccessfully creates an HTTP handler at `/testContainer` on the test handler mux that |
| 135 | // responds with a `Get` response. |
| 136 | func HandleGetContainerSuccessfully(t *testing.T) { |
| 137 | th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 138 | th.TestMethod(t, r, "HEAD") |
| 139 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 140 | th.TestHeader(t, r, "Accept", "application/json") |
| 141 | w.WriteHeader(http.StatusNoContent) |
| 142 | }) |
| 143 | } |