forked from gophercloud/gophercloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixtures.go
122 lines (106 loc) · 3.27 KB
/
fixtures.go
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
package testing
import (
"fmt"
"net/http"
"testing"
"github.com/chjlangzi/gophercloud/openstack/messaging/v2/claims"
th "github.com/chjlangzi/gophercloud/testhelper"
fake "github.com/chjlangzi/gophercloud/testhelper/client"
)
// QueueName is the name of the queue
var QueueName = "FakeTestQueue"
var ClaimID = "51db7067821e727dc24df754"
// CreateClaimResponse is a sample response to a create claim
const CreateClaimResponse = `
{
"messages": [
{
"body": {"event": "BackupStarted"},
"href": "/v2/queues/FakeTestQueue/messages/51db6f78c508f17ddc924357?claim_id=51db7067821e727dc24df754",
"age": 57,
"ttl": 300
}
]
}`
// GetClaimResponse is a sample response to a get claim
const GetClaimResponse = `
{
"age": 50,
"href": "/v2/queues/demoqueue/claims/51db7067821e727dc24df754",
"messages": [
{
"body": {"event": "BackupStarted"},
"href": "/v2/queues/FakeTestQueue/messages/51db6f78c508f17ddc924357?claim_id=51db7067821e727dc24df754",
"age": 57,
"ttl": 300
}
],
"ttl": 50
}`
// CreateClaimRequest is a sample request to create a claim.
const CreateClaimRequest = `
{
"ttl": 3600,
"grace": 3600
}`
// UpdateClaimRequest is a sample request to update a claim.
const UpdateClaimRequest = `
{
"ttl": 1200,
"grace": 1600
}`
// CreatedClaim is the result of a create request.
var CreatedClaim = []claims.Messages{
{
Age: 57,
Href: fmt.Sprintf("/v2/queues/%s/messages/51db6f78c508f17ddc924357?claim_id=%s", QueueName, ClaimID),
TTL: 300,
Body: map[string]interface{}{"event": "BackupStarted"},
},
}
// FirstClaim is the result of a get claim.
var FirstClaim = claims.Claim{
Age: 50,
Href: "/v2/queues/demoqueue/claims/51db7067821e727dc24df754",
Messages: []claims.Messages{
{
Age: 57,
Href: fmt.Sprintf("/v2/queues/%s/messages/51db6f78c508f17ddc924357?claim_id=%s", QueueName, ClaimID),
TTL: 300,
Body: map[string]interface{}{"event": "BackupStarted"},
},
},
TTL: 50,
}
// HandleCreateSuccessfully configures the test server to respond to a Create request.
func HandleCreateSuccessfully(t *testing.T) {
th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s/claims", 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, CreateClaimRequest)
w.WriteHeader(http.StatusCreated)
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, CreateClaimResponse)
})
}
// HandleGetSuccessfully configures the test server to respond to a Get request.
func HandleGetSuccessfully(t *testing.T) {
th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s/claims/%s", QueueName, ClaimID),
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, GetClaimResponse)
})
}
// HandleUpdateSuccessfully configures the test server to respond to a Update request.
func HandleUpdateSuccessfully(t *testing.T) {
th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s/claims/%s", QueueName, ClaimID),
func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PATCH")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestJSONRequest(t, r, UpdateClaimRequest)
w.WriteHeader(http.StatusNoContent)
})
}