github.com/gophercloud/gophercloud@v1.11.0/openstack/messaging/v2/claims/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/claims" 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 var ClaimID = "51db7067821e727dc24df754" 17 18 // CreateClaimResponse is a sample response to a create claim 19 const CreateClaimResponse = ` 20 { 21 "messages": [ 22 { 23 "body": {"event": "BackupStarted"}, 24 "href": "/v2/queues/FakeTestQueue/messages/51db6f78c508f17ddc924357?claim_id=51db7067821e727dc24df754", 25 "age": 57, 26 "ttl": 300 27 } 28 ] 29 }` 30 31 // GetClaimResponse is a sample response to a get claim 32 const GetClaimResponse = ` 33 { 34 "age": 50, 35 "href": "/v2/queues/demoqueue/claims/51db7067821e727dc24df754", 36 "messages": [ 37 { 38 "body": {"event": "BackupStarted"}, 39 "href": "/v2/queues/FakeTestQueue/messages/51db6f78c508f17ddc924357?claim_id=51db7067821e727dc24df754", 40 "age": 57, 41 "ttl": 300 42 } 43 ], 44 "ttl": 50 45 }` 46 47 // CreateClaimRequest is a sample request to create a claim. 48 const CreateClaimRequest = ` 49 { 50 "ttl": 3600, 51 "grace": 3600 52 }` 53 54 // UpdateClaimRequest is a sample request to update a claim. 55 const UpdateClaimRequest = ` 56 { 57 "ttl": 1200, 58 "grace": 1600 59 }` 60 61 // CreatedClaim is the result of a create request. 62 var CreatedClaim = []claims.Messages{ 63 { 64 Age: 57, 65 Href: fmt.Sprintf("/v2/queues/%s/messages/51db6f78c508f17ddc924357?claim_id=%s", QueueName, ClaimID), 66 TTL: 300, 67 Body: map[string]interface{}{"event": "BackupStarted"}, 68 }, 69 } 70 71 // FirstClaim is the result of a get claim. 72 var FirstClaim = claims.Claim{ 73 Age: 50, 74 Href: "/v2/queues/demoqueue/claims/51db7067821e727dc24df754", 75 Messages: []claims.Messages{ 76 { 77 Age: 57, 78 Href: fmt.Sprintf("/v2/queues/%s/messages/51db6f78c508f17ddc924357?claim_id=%s", QueueName, ClaimID), 79 TTL: 300, 80 Body: map[string]interface{}{"event": "BackupStarted"}, 81 }, 82 }, 83 TTL: 50, 84 } 85 86 // HandleCreateSuccessfully configures the test server to respond to a Create request. 87 func HandleCreateSuccessfully(t *testing.T) { 88 th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s/claims", QueueName), 89 func(w http.ResponseWriter, r *http.Request) { 90 th.TestMethod(t, r, "POST") 91 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 92 th.TestJSONRequest(t, r, CreateClaimRequest) 93 94 w.WriteHeader(http.StatusCreated) 95 w.Header().Add("Content-Type", "application/json") 96 fmt.Fprintf(w, CreateClaimResponse) 97 }) 98 } 99 100 // HandleCreateNoContent configures the test server to respond to a Create request with no content. 101 func HandleCreateNoContent(t *testing.T) { 102 th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s/claims", QueueName), 103 func(w http.ResponseWriter, r *http.Request) { 104 th.TestMethod(t, r, "POST") 105 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 106 th.TestJSONRequest(t, r, CreateClaimRequest) 107 108 w.WriteHeader(http.StatusNoContent) 109 }) 110 } 111 112 // HandleGetSuccessfully configures the test server to respond to a Get request. 113 func HandleGetSuccessfully(t *testing.T) { 114 th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s/claims/%s", QueueName, ClaimID), 115 func(w http.ResponseWriter, r *http.Request) { 116 th.TestMethod(t, r, "GET") 117 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 118 119 w.Header().Add("Content-Type", "application/json") 120 fmt.Fprintf(w, GetClaimResponse) 121 }) 122 } 123 124 // HandleUpdateSuccessfully configures the test server to respond to a Update request. 125 func HandleUpdateSuccessfully(t *testing.T) { 126 th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s/claims/%s", QueueName, ClaimID), 127 func(w http.ResponseWriter, r *http.Request) { 128 th.TestMethod(t, r, "PATCH") 129 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 130 th.TestJSONRequest(t, r, UpdateClaimRequest) 131 132 w.WriteHeader(http.StatusNoContent) 133 }) 134 } 135 136 // HandleDeleteSuccessfully configures the test server to respond to an Delete request. 137 func HandleDeleteSuccessfully(t *testing.T) { 138 th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s/claims/%s", QueueName, ClaimID), 139 func(w http.ResponseWriter, r *http.Request) { 140 th.TestMethod(t, r, "DELETE") 141 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 142 143 w.WriteHeader(http.StatusNoContent) 144 }) 145 }