Jamie Hannaford | 96c666d | 2014-10-20 16:09:10 +0200 | [diff] [blame] | 1 | package snapshots |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
| 8 | th "github.com/rackspace/gophercloud/testhelper" |
| 9 | fake "github.com/rackspace/gophercloud/testhelper/client" |
| 10 | ) |
| 11 | |
| 12 | func MockListResponse(t *testing.T) { |
| 13 | th.Mux.HandleFunc("/snapshots", func(w http.ResponseWriter, r *http.Request) { |
| 14 | th.TestMethod(t, r, "GET") |
| 15 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 16 | |
| 17 | w.Header().Add("Content-Type", "application/json") |
| 18 | w.WriteHeader(http.StatusOK) |
| 19 | |
| 20 | fmt.Fprintf(w, ` |
| 21 | { |
| 22 | "snapshots": [ |
| 23 | { |
| 24 | "id": "289da7f8-6440-407c-9fb4-7db01ec49164", |
| 25 | "display_name": "snapshot-001" |
| 26 | }, |
| 27 | { |
| 28 | "id": "96c3bda7-c82a-4f50-be73-ca7621794835", |
| 29 | "display_name": "snapshot-002" |
| 30 | } |
| 31 | ] |
| 32 | } |
| 33 | `) |
| 34 | }) |
| 35 | } |
| 36 | |
| 37 | func MockGetResponse(t *testing.T) { |
| 38 | th.Mux.HandleFunc("/snapshots/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { |
| 39 | th.TestMethod(t, r, "GET") |
| 40 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 41 | |
| 42 | w.Header().Add("Content-Type", "application/json") |
| 43 | w.WriteHeader(http.StatusOK) |
| 44 | fmt.Fprintf(w, ` |
| 45 | { |
| 46 | "snapshot": { |
| 47 | "display_name": "snapshot-001", |
| 48 | "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22" |
| 49 | } |
| 50 | } |
| 51 | `) |
| 52 | }) |
| 53 | } |
| 54 | |
| 55 | func MockCreateResponse(t *testing.T) { |
| 56 | th.Mux.HandleFunc("/snapshots", func(w http.ResponseWriter, r *http.Request) { |
| 57 | th.TestMethod(t, r, "POST") |
| 58 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 59 | th.TestHeader(t, r, "Content-Type", "application/json") |
| 60 | th.TestHeader(t, r, "Accept", "application/json") |
| 61 | th.TestJSONRequest(t, r, ` |
| 62 | { |
| 63 | "snapshot": { |
| 64 | "volume_id": "1234", |
| 65 | "display_name": "snapshot-001" |
| 66 | } |
| 67 | } |
| 68 | `) |
| 69 | |
| 70 | w.Header().Add("Content-Type", "application/json") |
| 71 | w.WriteHeader(http.StatusCreated) |
| 72 | |
| 73 | fmt.Fprintf(w, ` |
| 74 | { |
| 75 | "snapshot": { |
| 76 | "volume_id": "1234", |
| 77 | "display_name": "snapshot-001", |
| 78 | "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22" |
| 79 | } |
| 80 | } |
| 81 | `) |
| 82 | }) |
| 83 | } |
| 84 | |
| 85 | func MockUpdateMetadataResponse(t *testing.T) { |
| 86 | th.Mux.HandleFunc("/snapshots/123/metadata", func(w http.ResponseWriter, r *http.Request) { |
| 87 | th.TestMethod(t, r, "PUT") |
| 88 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 89 | th.TestHeader(t, r, "Content-Type", "application/json") |
| 90 | th.TestJSONRequest(t, r, ` |
| 91 | { |
| 92 | "metadata": { |
| 93 | "key": "v1" |
| 94 | } |
| 95 | } |
| 96 | `) |
| 97 | |
| 98 | fmt.Fprintf(w, ` |
| 99 | { |
| 100 | "metadata": { |
| 101 | "key": "v1" |
| 102 | } |
| 103 | } |
| 104 | `) |
| 105 | }) |
| 106 | } |
| 107 | |
| 108 | func MockDeleteResponse(t *testing.T) { |
| 109 | th.Mux.HandleFunc("/snapshots/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { |
| 110 | th.TestMethod(t, r, "DELETE") |
| 111 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 112 | w.WriteHeader(http.StatusNoContent) |
| 113 | }) |
| 114 | } |