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, ` |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 45 | { |
| 46 | "volume": { |
| 47 | "id": "521752a6-acf6-4b2d-bc7a-119f9148cd8c", |
| 48 | "display_name": "vol-001", |
| 49 | "display_description": "Another volume.", |
| 50 | "status": "active", |
| 51 | "size": 30, |
| 52 | "volume_type": "289da7f8-6440-407c-9fb4-7db01ec49164", |
| 53 | "metadata": { |
| 54 | "contents": "junk" |
| 55 | }, |
| 56 | "availability_zone": "us-east1", |
| 57 | "bootable": "false", |
| 58 | "snapshot_id": null, |
| 59 | "attachments": [ |
| 60 | { |
| 61 | "attachment_id": "03987cd1-0ad5-40d1-9b2a-7cc48295d4fa", |
| 62 | "id": "47e9ecc5-4045-4ee3-9a4b-d859d546a0cf", |
| 63 | "volume_id": "6c80f8ac-e3e2-480c-8e6e-f1db92fe4bfe", |
| 64 | "server_id": "d1c4788b-9435-42e2-9b81-29f3be1cd01f", |
| 65 | "host_name": "mitaka", |
| 66 | "device": "/" |
| 67 | } |
| 68 | ], |
| 69 | "created_at": "2012-02-14T20:53:07Z" |
| 70 | } |
| 71 | } |
Jamie Hannaford | 449c4ac | 2014-10-21 09:58:02 +0200 | [diff] [blame] | 72 | `) |
| 73 | }) |
| 74 | } |
| 75 | |
| 76 | func MockCreateResponse(t *testing.T) { |
| 77 | th.Mux.HandleFunc("/volumes", func(w http.ResponseWriter, r *http.Request) { |
| 78 | th.TestMethod(t, r, "POST") |
| 79 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 80 | th.TestHeader(t, r, "Content-Type", "application/json") |
| 81 | th.TestHeader(t, r, "Accept", "application/json") |
| 82 | th.TestJSONRequest(t, r, ` |
| 83 | { |
| 84 | "volume": { |
Jamie Hannaford | 59f2207 | 2014-10-23 17:00:59 +0200 | [diff] [blame] | 85 | "size": 75 |
Jamie Hannaford | 449c4ac | 2014-10-21 09:58:02 +0200 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | `) |
| 89 | |
| 90 | w.Header().Add("Content-Type", "application/json") |
| 91 | w.WriteHeader(http.StatusCreated) |
| 92 | |
| 93 | fmt.Fprintf(w, ` |
| 94 | { |
| 95 | "volume": { |
| 96 | "size": 4, |
| 97 | "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22" |
| 98 | } |
| 99 | } |
| 100 | `) |
| 101 | }) |
| 102 | } |
| 103 | |
| 104 | func MockDeleteResponse(t *testing.T) { |
| 105 | th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { |
| 106 | th.TestMethod(t, r, "DELETE") |
| 107 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 108 | w.WriteHeader(http.StatusNoContent) |
| 109 | }) |
| 110 | } |
| 111 | |
| 112 | func MockUpdateResponse(t *testing.T) { |
| 113 | th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { |
| 114 | th.TestMethod(t, r, "PUT") |
| 115 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 116 | w.WriteHeader(http.StatusOK) |
| 117 | fmt.Fprintf(w, ` |
| 118 | { |
| 119 | "volume": { |
| 120 | "display_name": "vol-002", |
| 121 | "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22" |
| 122 | } |
| 123 | } |
| 124 | `) |
| 125 | }) |
| 126 | } |