github.com/gophercloud/gophercloud@v1.11.0/openstack/clustering/v1/webhooks/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 "testing" 8 9 "github.com/gophercloud/gophercloud/openstack/clustering/v1/webhooks" 10 th "github.com/gophercloud/gophercloud/testhelper" 11 fake "github.com/gophercloud/gophercloud/testhelper/client" 12 ) 13 14 func TestWebhookTrigger(t *testing.T) { 15 th.SetupHTTP() 16 defer th.TeardownHTTP() 17 18 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 19 th.TestMethod(t, r, "POST") 20 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 21 22 w.Header().Add("Content-Type", "application/json") 23 w.WriteHeader(http.StatusOK) 24 25 fmt.Fprintf(w, ` 26 { 27 "action": "290c44fa-c60f-4d75-a0eb-87433ba982a3" 28 }`) 29 }) 30 31 triggerOpts := webhooks.TriggerOpts{ 32 V: "1", 33 Params: map[string]string{ 34 "foo": "bar", 35 "bar": "baz", 36 }, 37 } 38 result, err := webhooks.Trigger(fake.ServiceClient(), "f93f83f6-762b-41b6-b757-80507834d394", triggerOpts).Extract() 39 th.AssertNoErr(t, err) 40 th.AssertEquals(t, result, "290c44fa-c60f-4d75-a0eb-87433ba982a3") 41 } 42 43 // Test webhook with params that generates query strings 44 func TestWebhookParams(t *testing.T) { 45 triggerOpts := webhooks.TriggerOpts{ 46 V: "1", 47 Params: map[string]string{ 48 "foo": "bar", 49 "bar": "baz", 50 }, 51 } 52 expected := "?V=1&bar=baz&foo=bar" 53 actual, err := triggerOpts.ToWebhookTriggerQuery() 54 th.AssertNoErr(t, err) 55 th.AssertEquals(t, actual, expected) 56 } 57 58 // Nagative test case for returning invalid type (integer) for action id 59 func TestWebhooksInvalidAction(t *testing.T) { 60 th.SetupHTTP() 61 defer th.TeardownHTTP() 62 63 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 64 th.TestMethod(t, r, "POST") 65 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 66 67 w.Header().Add("Content-Type", "application/json") 68 w.WriteHeader(http.StatusOK) 69 70 fmt.Fprintf(w, ` 71 { 72 "action": 123 73 }`) 74 }) 75 76 triggerOpts := webhooks.TriggerOpts{ 77 V: "1", 78 Params: map[string]string{ 79 "foo": "bar", 80 "bar": "baz", 81 }, 82 } 83 _, err := webhooks.Trigger(fake.ServiceClient(), "f93f83f6-762b-41b6-b757-80507834d394", triggerOpts).Extract() 84 isValid := err.(*json.UnmarshalTypeError) == nil 85 th.AssertEquals(t, false, isValid) 86 } 87 88 // Negative test case for passing empty TriggerOpt 89 func TestWebhookTriggerInvalidEmptyOpt(t *testing.T) { 90 th.SetupHTTP() 91 defer th.TeardownHTTP() 92 93 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 94 th.TestMethod(t, r, "POST") 95 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 96 97 w.Header().Add("Content-Type", "application/json") 98 w.WriteHeader(http.StatusOK) 99 100 fmt.Fprintf(w, ` 101 { 102 "action": "290c44fa-c60f-4d75-a0eb-87433ba982a3" 103 }`) 104 }) 105 106 _, err := webhooks.Trigger(fake.ServiceClient(), "f93f83f6-762b-41b6-b757-80507834d394", webhooks.TriggerOpts{}).Extract() 107 if err == nil { 108 t.Errorf("Expected error without V param") 109 } 110 } 111 112 // Negative test case for passing in nil for TriggerOpt 113 func TestWebhookTriggerInvalidNilOpt(t *testing.T) { 114 th.SetupHTTP() 115 defer th.TeardownHTTP() 116 117 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 118 th.TestMethod(t, r, "POST") 119 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 120 121 w.Header().Add("Content-Type", "application/json") 122 w.WriteHeader(http.StatusOK) 123 124 fmt.Fprintf(w, ` 125 { 126 "action": "290c44fa-c60f-4d75-a0eb-87433ba982a3" 127 }`) 128 }) 129 130 _, err := webhooks.Trigger(fake.ServiceClient(), "f93f83f6-762b-41b6-b757-80507834d394", nil).Extract() 131 132 if err == nil { 133 t.Errorf("Expected error with nil param") 134 } 135 }