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