blob: f243289e4e9a685fc267904bceb545cb1c50bdbb [file] [log] [blame]
Brad Ison366a7a02016-03-27 17:06:21 -04001// +build fixtures
2
3package webhooks
4
5import (
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.
16const 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 Isone6e0ec12016-03-27 21:26:46 -040056// WebhookCreateBody contains the canned body of a webhooks.Create response.
57const WebhookCreateBody = WebhookListBody
58
59// WebhookCreateRequest contains the canned body of a webhooks.Create request.
60const 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 Ison366a7a02016-03-27 17:06:21 -040074var (
75 // FirstWebhook is a Webhook corresponding to the first result in WebhookListBody.
76 FirstWebhook = Webhook{
77 ID: "2bd1822c-58c5-49fd-8b3d-ed44781a58d1",
78 Name: "first hook",
79 Links: []gophercloud.Link{
80 {
81 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/",
82 Rel: "self",
83 },
84 {
85 Href: "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/714c1c17c5e6ea5ef1e710d5ccc62e492575bab5216184d4c27dc0164db1bc06/",
86 Rel: "capability",
87 },
88 },
89 Metadata: map[string]string{},
90 }
91
92 // SecondWebhook is a Webhook corresponding to the second result in WebhookListBody.
93 SecondWebhook = Webhook{
94 ID: "76711c36-dfbe-4f5e-bea6-cded99690515",
95 Name: "second hook",
96 Links: []gophercloud.Link{
97 {
98 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/",
99 Rel: "self",
100 },
101 {
102 Href: "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/982e24858723f9e8bc2afea42a73a3c357c8f518857735400a7f7d8b3f14ccdb/",
103 Rel: "capability",
104 },
105 },
106 Metadata: map[string]string{
107 "notes": "a note about this webhook",
108 },
109 }
110)
111
112// HandleWebhookListSuccessfully sets up the test server to respond to a webhooks List request.
113func HandleWebhookListSuccessfully(t *testing.T) {
114 path := "/groups/10eb3219-1b12-4b34-b1e4-e10ee4f24c65/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks"
115
116 th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
117 th.TestMethod(t, r, "GET")
118 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
119
120 w.Header().Add("Content-Type", "application/json")
121
122 fmt.Fprintf(w, WebhookListBody)
123 })
124}
Brad Isone6e0ec12016-03-27 21:26:46 -0400125
126// HandleWebhookCreateSuccessfully sets up the test server to respond to a webhooks Create request.
127func HandleWebhookCreateSuccessfully(t *testing.T) {
128 path := "/groups/10eb3219-1b12-4b34-b1e4-e10ee4f24c65/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks"
129
130 th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
131 th.TestMethod(t, r, "POST")
132 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
133 th.TestHeader(t, r, "Content-Type", "application/json")
134 th.TestHeader(t, r, "Accept", "application/json")
135
136 th.TestJSONRequest(t, r, WebhookCreateRequest)
137
138 w.Header().Add("Content-Type", "application/json")
139 w.WriteHeader(http.StatusCreated)
140
141 fmt.Fprintf(w, WebhookCreateBody)
142 })
143}