Brad Ison | 366a7a0 | 2016-03-27 17:06:21 -0400 | [diff] [blame] | 1 | // +build fixtures |
| 2 | |
| 3 | package webhooks |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/rackspace/gophercloud" |
| 11 | th "github.com/rackspace/gophercloud/testhelper" |
| 12 | "github.com/rackspace/gophercloud/testhelper/client" |
| 13 | ) |
| 14 | |
| 15 | // WebhookListBody contains the canned body of a webhooks.List response. |
| 16 | const WebhookListBody = ` |
| 17 | { |
| 18 | "webhooks": [ |
| 19 | { |
| 20 | "id": "2bd1822c-58c5-49fd-8b3d-ed44781a58d1", |
| 21 | "name": "first hook", |
| 22 | "links": [ |
| 23 | { |
| 24 | "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks/2bd1822c-58c5-49fd-8b3d-ed44781a58d1/", |
| 25 | "rel": "self" |
| 26 | }, |
| 27 | { |
| 28 | "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/714c1c17c5e6ea5ef1e710d5ccc62e492575bab5216184d4c27dc0164db1bc06/", |
| 29 | "rel": "capability" |
| 30 | } |
| 31 | ], |
| 32 | "metadata": {} |
| 33 | }, |
| 34 | { |
| 35 | "id": "76711c36-dfbe-4f5e-bea6-cded99690515", |
| 36 | "name": "second hook", |
| 37 | "links": [ |
| 38 | { |
| 39 | "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks/76711c36-dfbe-4f5e-bea6-cded99690515/", |
| 40 | "rel": "self" |
| 41 | }, |
| 42 | { |
| 43 | "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/982e24858723f9e8bc2afea42a73a3c357c8f518857735400a7f7d8b3f14ccdb/", |
| 44 | "rel": "capability" |
| 45 | } |
| 46 | ], |
| 47 | "metadata": { |
| 48 | "notes": "a note about this webhook" |
| 49 | } |
| 50 | } |
| 51 | ], |
| 52 | "webhooks_links": [] |
| 53 | } |
| 54 | ` |
| 55 | |
Brad Ison | e6e0ec1 | 2016-03-27 21:26:46 -0400 | [diff] [blame] | 56 | // WebhookCreateBody contains the canned body of a webhooks.Create response. |
| 57 | const WebhookCreateBody = WebhookListBody |
| 58 | |
| 59 | // WebhookCreateRequest contains the canned body of a webhooks.Create request. |
| 60 | const WebhookCreateRequest = ` |
| 61 | [ |
| 62 | { |
| 63 | "name": "first hook" |
| 64 | }, |
| 65 | { |
| 66 | "name": "second hook", |
| 67 | "metadata": { |
| 68 | "notes": "a note about this webhook" |
| 69 | } |
| 70 | } |
| 71 | ] |
| 72 | ` |
| 73 | |
Brad Ison | 20644be | 2016-03-28 19:13:05 -0400 | [diff] [blame^] | 74 | // WebhookGetBody contains the canned body of a webhooks.Get response. |
| 75 | const WebhookGetBody = ` |
| 76 | { |
| 77 | "webhook": { |
| 78 | "id": "2bd1822c-58c5-49fd-8b3d-ed44781a58d1", |
| 79 | "name": "first hook", |
| 80 | "links": [ |
| 81 | { |
| 82 | "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks/2bd1822c-58c5-49fd-8b3d-ed44781a58d1/", |
| 83 | "rel": "self" |
| 84 | }, |
| 85 | { |
| 86 | "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/714c1c17c5e6ea5ef1e710d5ccc62e492575bab5216184d4c27dc0164db1bc06/", |
| 87 | "rel": "capability" |
| 88 | } |
| 89 | ], |
| 90 | "metadata": {} |
| 91 | } |
| 92 | } |
| 93 | ` |
| 94 | |
Brad Ison | 366a7a0 | 2016-03-27 17:06:21 -0400 | [diff] [blame] | 95 | var ( |
| 96 | // FirstWebhook is a Webhook corresponding to the first result in WebhookListBody. |
| 97 | FirstWebhook = Webhook{ |
| 98 | ID: "2bd1822c-58c5-49fd-8b3d-ed44781a58d1", |
| 99 | Name: "first hook", |
| 100 | Links: []gophercloud.Link{ |
| 101 | { |
| 102 | Href: "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks/2bd1822c-58c5-49fd-8b3d-ed44781a58d1/", |
| 103 | Rel: "self", |
| 104 | }, |
| 105 | { |
| 106 | Href: "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/714c1c17c5e6ea5ef1e710d5ccc62e492575bab5216184d4c27dc0164db1bc06/", |
| 107 | Rel: "capability", |
| 108 | }, |
| 109 | }, |
| 110 | Metadata: map[string]string{}, |
| 111 | } |
| 112 | |
| 113 | // SecondWebhook is a Webhook corresponding to the second result in WebhookListBody. |
| 114 | SecondWebhook = Webhook{ |
| 115 | ID: "76711c36-dfbe-4f5e-bea6-cded99690515", |
| 116 | Name: "second hook", |
| 117 | Links: []gophercloud.Link{ |
| 118 | { |
| 119 | Href: "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks/76711c36-dfbe-4f5e-bea6-cded99690515/", |
| 120 | Rel: "self", |
| 121 | }, |
| 122 | { |
| 123 | Href: "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/982e24858723f9e8bc2afea42a73a3c357c8f518857735400a7f7d8b3f14ccdb/", |
| 124 | Rel: "capability", |
| 125 | }, |
| 126 | }, |
| 127 | Metadata: map[string]string{ |
| 128 | "notes": "a note about this webhook", |
| 129 | }, |
| 130 | } |
| 131 | ) |
| 132 | |
| 133 | // HandleWebhookListSuccessfully sets up the test server to respond to a webhooks List request. |
| 134 | func HandleWebhookListSuccessfully(t *testing.T) { |
| 135 | path := "/groups/10eb3219-1b12-4b34-b1e4-e10ee4f24c65/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks" |
| 136 | |
| 137 | th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { |
| 138 | th.TestMethod(t, r, "GET") |
| 139 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 140 | |
| 141 | w.Header().Add("Content-Type", "application/json") |
| 142 | |
| 143 | fmt.Fprintf(w, WebhookListBody) |
| 144 | }) |
| 145 | } |
Brad Ison | e6e0ec1 | 2016-03-27 21:26:46 -0400 | [diff] [blame] | 146 | |
| 147 | // HandleWebhookCreateSuccessfully sets up the test server to respond to a webhooks Create request. |
| 148 | func HandleWebhookCreateSuccessfully(t *testing.T) { |
| 149 | path := "/groups/10eb3219-1b12-4b34-b1e4-e10ee4f24c65/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks" |
| 150 | |
| 151 | th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { |
| 152 | th.TestMethod(t, r, "POST") |
| 153 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 154 | th.TestHeader(t, r, "Content-Type", "application/json") |
| 155 | th.TestHeader(t, r, "Accept", "application/json") |
| 156 | |
| 157 | th.TestJSONRequest(t, r, WebhookCreateRequest) |
| 158 | |
| 159 | w.Header().Add("Content-Type", "application/json") |
| 160 | w.WriteHeader(http.StatusCreated) |
| 161 | |
| 162 | fmt.Fprintf(w, WebhookCreateBody) |
| 163 | }) |
| 164 | } |
Brad Ison | 20644be | 2016-03-28 19:13:05 -0400 | [diff] [blame^] | 165 | |
| 166 | // HandleWebhookGetSuccessfully sets up the test server to respond to a webhooks Get request. |
| 167 | func HandleWebhookGetSuccessfully(t *testing.T) { |
| 168 | groupID := "10eb3219-1b12-4b34-b1e4-e10ee4f24c65" |
| 169 | policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649" |
| 170 | webhookID := "2bd1822c-58c5-49fd-8b3d-ed44781a58d1" |
| 171 | |
| 172 | path := fmt.Sprintf("/groups/%s/policies/%s/webhooks/%s", groupID, policyID, webhookID) |
| 173 | |
| 174 | th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { |
| 175 | th.TestMethod(t, r, "GET") |
| 176 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 177 | |
| 178 | w.Header().Add("Content-Type", "application/json") |
| 179 | |
| 180 | fmt.Fprintf(w, WebhookGetBody) |
| 181 | }) |
| 182 | } |