github.com/gophercloud/gophercloud@v1.11.0/openstack/messaging/v2/messages/testing/fixtures_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/gophercloud/gophercloud/openstack/messaging/v2/messages" 9 th "github.com/gophercloud/gophercloud/testhelper" 10 fake "github.com/gophercloud/gophercloud/testhelper/client" 11 ) 12 13 // QueueName is the name of the queue 14 var QueueName = "FakeTestQueue" 15 16 // MessageID is the id of the message 17 var MessageID = "9988776655" 18 19 // CreateMessageResponse is a sample response to a Create message. 20 const CreateMessageResponse = ` 21 { 22 "resources": [ 23 "/v2/queues/demoqueue/messages/51db6f78c508f17ddc924357", 24 "/v2/queues/demoqueue/messages/51db6f78c508f17ddc924358" 25 ] 26 }` 27 28 // CreateMessageRequest is a sample request to create a message. 29 const CreateMessageRequest = ` 30 { 31 "messages": [ 32 { 33 "body": { 34 "backup_id": "c378813c-3f0b-11e2-ad92-7823d2b0f3ce", 35 "event": "BackupStarted" 36 }, 37 "delay": 20, 38 "ttl": 300 39 }, 40 { 41 "body": { 42 "current_bytes": "0", 43 "event": "BackupProgress", 44 "total_bytes": "99614720" 45 } 46 } 47 ] 48 }` 49 50 // ListMessagesResponse is a sample response to list messages. 51 const ListMessagesResponse1 = ` 52 { 53 "messages": [ 54 { 55 "body": { 56 "current_bytes": "0", 57 "event": "BackupProgress", 58 "total_bytes": "99614720" 59 }, 60 "age": 482, 61 "href": "/v2/queues/FakeTestQueue/messages/578edfe6508f153f256f717b", 62 "id": "578edfe6508f153f256f717b", 63 "ttl": 3600, 64 "checksum": "MD5:abf7213555626e29c3cb3e5dc58b3515" 65 } 66 ], 67 "links": [ 68 { 69 "href": "/v2/queues/FakeTestQueue/messages?marker=1", 70 "rel": "next" 71 } 72 ] 73 }` 74 75 // ListMessagesResponse is a sample response to list messages. 76 const ListMessagesResponse2 = ` 77 { 78 "messages": [ 79 { 80 "body": { 81 "current_bytes": "0", 82 "event": "BackupProgress", 83 "total_bytes": "99614720" 84 }, 85 "age": 456, 86 "href": "/v2/queues/FakeTestQueue/messages/578ee000508f153f256f717d", 87 "id": "578ee000508f153f256f717d", 88 "ttl": 3600, 89 "checksum": "MD5:abf7213555626e29c3cb3e5dc58b3515" 90 } 91 ], 92 "links": [ 93 { 94 "href": "/v2/queues/FakeTestQueue/messages?marker=2", 95 "rel": "next" 96 } 97 ] 98 99 }` 100 101 // GetMessagesResponse is a sample response to GetMessages. 102 const GetMessagesResponse = ` 103 { 104 "messages": [ 105 { 106 "body": { 107 "current_bytes": "0", 108 "event": "BackupProgress", 109 "total_bytes": "99614720" 110 }, 111 "age": 443, 112 "href": "/v2/queues/beijing/messages/578f0055508f153f256f717f", 113 "id": "578f0055508f153f256f717f", 114 "ttl": 3600 115 } 116 ] 117 }` 118 119 // GetMessageResponse is a sample response to Get. 120 const GetMessageResponse = ` 121 { 122 "body": { 123 "current_bytes": "0", 124 "event": "BackupProgress", 125 "total_bytes": "99614720" 126 }, 127 "age": 482, 128 "href": "/v2/queues/FakeTestQueue/messages/578edfe6508f153f256f717b", 129 "id": "578edfe6508f153f256f717b", 130 "ttl": 3600, 131 "checksum": "MD5:abf7213555626e29c3cb3e5dc58b3515" 132 }` 133 134 // PopMessageResponse is a sample reponse to pop messages 135 const PopMessageResponse = ` 136 { 137 "messages": [ 138 { 139 "body": { 140 "current_bytes": "0", 141 "event": "BackupProgress", 142 "total_bytes": "99614720" 143 }, 144 "age": 20, 145 "ttl": 120, 146 "claim_count": 55, 147 "claim_id": "123456", 148 "id": "5ae7972599352b436763aee7" 149 } 150 ] 151 }` 152 153 // ExpectedResources is the expected result in Create 154 var ExpectedResources = messages.ResourceList{ 155 Resources: []string{ 156 "/v2/queues/demoqueue/messages/51db6f78c508f17ddc924357", 157 "/v2/queues/demoqueue/messages/51db6f78c508f17ddc924358", 158 }, 159 } 160 161 // FirstMessage is the first result in a List. 162 var FirstMessage = messages.Message{ 163 Body: map[string]interface{}{ 164 "current_bytes": "0", 165 "event": "BackupProgress", 166 "total_bytes": "99614720", 167 }, 168 Age: 482, 169 Href: fmt.Sprintf("/v2/queues/%s/messages/578edfe6508f153f256f717b", QueueName), 170 ID: "578edfe6508f153f256f717b", 171 TTL: 3600, 172 Checksum: "MD5:abf7213555626e29c3cb3e5dc58b3515", 173 } 174 175 // SecondMessage is the second result in a List. 176 var SecondMessage = messages.Message{ 177 Body: map[string]interface{}{ 178 "current_bytes": "0", 179 "event": "BackupProgress", 180 "total_bytes": "99614720", 181 }, 182 Age: 456, 183 Href: fmt.Sprintf("/v2/queues/%s/messages/578ee000508f153f256f717d", QueueName), 184 ID: "578ee000508f153f256f717d", 185 TTL: 3600, 186 Checksum: "MD5:abf7213555626e29c3cb3e5dc58b3515", 187 } 188 189 // ExpectedMessagesSlice is the expected result in a List. 190 var ExpectedMessagesSlice = [][]messages.Message{{FirstMessage}, {SecondMessage}} 191 192 // ExpectedMessagesSet is the expected result in GetMessages 193 var ExpectedMessagesSet = []messages.Message{ 194 { 195 Body: map[string]interface{}{ 196 "total_bytes": "99614720", 197 "current_bytes": "0", 198 "event": "BackupProgress", 199 }, 200 Age: 443, 201 Href: "/v2/queues/beijing/messages/578f0055508f153f256f717f", 202 ID: "578f0055508f153f256f717f", 203 TTL: 3600, 204 Checksum: "", 205 }, 206 } 207 208 // ExpectedPopMessage is the expected result of a Pop. 209 var ExpectedPopMessage = []messages.PopMessage{{ 210 Body: map[string]interface{}{ 211 "current_bytes": "0", 212 "event": "BackupProgress", 213 "total_bytes": "99614720", 214 }, 215 Age: 20, 216 TTL: 120, 217 ClaimID: "123456", 218 ClaimCount: 55, 219 ID: "5ae7972599352b436763aee7", 220 }} 221 222 // HandleCreateSuccessfully configures the test server to respond to a Create request. 223 func HandleCreateSuccessfully(t *testing.T) { 224 th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s/messages", QueueName), 225 func(w http.ResponseWriter, r *http.Request) { 226 th.TestMethod(t, r, "POST") 227 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 228 th.TestJSONRequest(t, r, CreateMessageRequest) 229 230 w.Header().Add("Content-Type", "application/json") 231 w.WriteHeader(http.StatusCreated) 232 fmt.Fprintf(w, CreateMessageResponse) 233 }) 234 } 235 236 // HandleListSuccessfully configures the test server to respond to a List request. 237 func HandleListSuccessfully(t *testing.T) { 238 th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s/messages", QueueName), 239 func(w http.ResponseWriter, r *http.Request) { 240 th.TestMethod(t, r, "GET") 241 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 242 243 w.Header().Add("Content-Type", "application/json") 244 next := r.RequestURI 245 246 switch next { 247 case fmt.Sprintf("/v2/queues/%s/messages?limit=1", QueueName): 248 fmt.Fprintf(w, ListMessagesResponse1) 249 case fmt.Sprintf("/v2/queues/%s/messages?marker=1", QueueName): 250 fmt.Fprint(w, ListMessagesResponse2) 251 case fmt.Sprintf("/v2/queues/%s/messages?marker=2", QueueName): 252 fmt.Fprint(w, `{ "messages": [] }`) 253 } 254 }) 255 } 256 257 // HandleGetMessagesSuccessfully configures the test server to respond to a GetMessages request. 258 func HandleGetMessagesSuccessfully(t *testing.T) { 259 th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s/messages", QueueName), 260 func(w http.ResponseWriter, r *http.Request) { 261 th.TestMethod(t, r, "GET") 262 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 263 264 w.Header().Add("Content-Type", "application/json") 265 fmt.Fprintf(w, GetMessagesResponse) 266 }) 267 } 268 269 // HandleGetSuccessfully configures the test server to respond to a Get request. 270 func HandleGetSuccessfully(t *testing.T) { 271 th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s/messages/%s", QueueName, MessageID), 272 func(w http.ResponseWriter, r *http.Request) { 273 th.TestMethod(t, r, "GET") 274 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 275 276 w.Header().Add("Content-Type", "application/json") 277 fmt.Fprintf(w, GetMessageResponse) 278 }) 279 } 280 281 // HandleDeleteMessagesSuccessfully configures the test server to respond to a Delete request. 282 func HandleDeleteMessagesSuccessfully(t *testing.T) { 283 th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s/messages", QueueName), 284 func(w http.ResponseWriter, r *http.Request) { 285 th.TestMethod(t, r, "DELETE") 286 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 287 288 w.Header().Add("Content-Type", "application/json") 289 w.WriteHeader(http.StatusNoContent) 290 }) 291 } 292 293 // HandlePopSuccessfully configures the test server to respond to a Pop request. 294 func HandlePopSuccessfully(t *testing.T) { 295 th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s/messages", QueueName), 296 func(w http.ResponseWriter, r *http.Request) { 297 th.TestMethod(t, r, "DELETE") 298 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 299 300 w.Header().Add("Content-Type", "application/json") 301 w.WriteHeader(http.StatusOK) 302 fmt.Fprintf(w, PopMessageResponse) 303 }) 304 } 305 306 // HandleGetSuccessfully configures the test server to respond to a Get request. 307 func HandleDeleteSuccessfully(t *testing.T) { 308 th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s/messages/%s", QueueName, MessageID), 309 func(w http.ResponseWriter, r *http.Request) { 310 th.TestMethod(t, r, "DELETE") 311 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 312 313 w.Header().Add("Content-Type", "application/json") 314 w.WriteHeader(http.StatusNoContent) 315 }) 316 }