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