Sreekanth Pothanis | 07400f3 | 2015-09-08 00:26:14 -0700 | [diff] [blame] | 1 | package testing |
Jamie Hannaford | 449c4ac | 2014-10-21 09:58:02 +0200 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame^] | 8 | th "github.com/gophercloud/gophercloud/testhelper" |
| 9 | fake "github.com/gophercloud/gophercloud/testhelper/client" |
Jamie Hannaford | 449c4ac | 2014-10-21 09:58:02 +0200 | [diff] [blame] | 10 | ) |
| 11 | |
| 12 | func MockListResponse(t *testing.T) { |
| 13 | th.Mux.HandleFunc("/volumes", 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 | "volumes": [ |
| 23 | { |
| 24 | "id": "289da7f8-6440-407c-9fb4-7db01ec49164", |
| 25 | "display_name": "vol-001" |
| 26 | }, |
| 27 | { |
| 28 | "id": "96c3bda7-c82a-4f50-be73-ca7621794835", |
| 29 | "display_name": "vol-002" |
| 30 | } |
| 31 | ] |
| 32 | } |
| 33 | `) |
| 34 | }) |
| 35 | } |
| 36 | |
| 37 | func MockGetResponse(t *testing.T) { |
| 38 | th.Mux.HandleFunc("/volumes/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 | "volume": { |
| 47 | "display_name": "vol-001", |
Joe Topjian | b75567d | 2015-02-06 04:23:16 +0000 | [diff] [blame] | 48 | "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22", |
| 49 | "attachments": [ |
| 50 | { |
| 51 | "device": "/dev/vde", |
| 52 | "server_id": "a740d24b-dc5b-4d59-ac75-53971c2920ba", |
| 53 | "id": "d6da11e5-2ed3-413e-88d8-b772ba62193d", |
| 54 | "volume_id": "d6da11e5-2ed3-413e-88d8-b772ba62193d" |
| 55 | } |
| 56 | ] |
| 57 | } |
Jamie Hannaford | 449c4ac | 2014-10-21 09:58:02 +0200 | [diff] [blame] | 58 | } |
| 59 | `) |
| 60 | }) |
| 61 | } |
| 62 | |
| 63 | func MockCreateResponse(t *testing.T) { |
| 64 | th.Mux.HandleFunc("/volumes", func(w http.ResponseWriter, r *http.Request) { |
| 65 | th.TestMethod(t, r, "POST") |
| 66 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 67 | th.TestHeader(t, r, "Content-Type", "application/json") |
| 68 | th.TestHeader(t, r, "Accept", "application/json") |
| 69 | th.TestJSONRequest(t, r, ` |
| 70 | { |
| 71 | "volume": { |
Jamie Hannaford | 59f2207 | 2014-10-23 17:00:59 +0200 | [diff] [blame] | 72 | "size": 75 |
Jamie Hannaford | 449c4ac | 2014-10-21 09:58:02 +0200 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | `) |
| 76 | |
| 77 | w.Header().Add("Content-Type", "application/json") |
| 78 | w.WriteHeader(http.StatusCreated) |
| 79 | |
| 80 | fmt.Fprintf(w, ` |
| 81 | { |
| 82 | "volume": { |
| 83 | "size": 4, |
| 84 | "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22" |
| 85 | } |
| 86 | } |
| 87 | `) |
| 88 | }) |
| 89 | } |
| 90 | |
| 91 | func MockDeleteResponse(t *testing.T) { |
| 92 | th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { |
| 93 | th.TestMethod(t, r, "DELETE") |
| 94 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 95 | w.WriteHeader(http.StatusNoContent) |
| 96 | }) |
| 97 | } |
| 98 | |
| 99 | func MockUpdateResponse(t *testing.T) { |
| 100 | th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { |
| 101 | th.TestMethod(t, r, "PUT") |
| 102 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 103 | w.WriteHeader(http.StatusOK) |
| 104 | fmt.Fprintf(w, ` |
| 105 | { |
| 106 | "volume": { |
| 107 | "display_name": "vol-002", |
| 108 | "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22" |
| 109 | } |
| 110 | } |
| 111 | `) |
| 112 | }) |
| 113 | } |