Skip to content

Commit

Permalink
Add test cases to check webhook Content-Type headers
Browse files Browse the repository at this point in the history
  • Loading branch information
komalsukhani committed Jul 5, 2019
1 parent f2acf68 commit 41d7933
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions gateway/event_handler_webhooks_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gateway

import (
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -219,3 +220,46 @@ func TestNewCustomTemplate(t *testing.T) {
})
}
}

func TestWebhookContentTypeHeader(t *testing.T) {
globalConf := config.Global()
templatePath := globalConf.TemplatePath

tests := []struct {
Name string
TemplatePath string
InputHeaders map[string]string
ExpectedContentType string
}{
{"MissingTemplatePath", "", nil, "application/json"},
{"MissingTemplatePath/CustomHeaders", "", map[string]string{"Content-Type": "application/xml"}, "application/xml"},
{"InvalidTemplatePath", "randomPath", nil, "application/json"},
{"InvalidTemplatePath/CustomHeaders", "randomPath", map[string]string{"Content-Type": "application/xml"}, "application/xml"},
{"CustomTemplate", filepath.Join(templatePath, "breaker_webhook.json"), nil, ""},
{"CustomTemplate/CustomHeaders", filepath.Join(templatePath, "breaker_webhook.json"), map[string]string{"Content-Type": "application/json"}, "application/json"},
}

for _, ts := range tests {
t.Run(ts.Name, func(t *testing.T) {
conf := config.WebHookHandlerConf{
TemplatePath: ts.TemplatePath,
HeaderList: ts.InputHeaders,
}

hook := &WebHookHandler{}
if err := hook.Init(conf); err != nil {
t.Fatal("Webhook Init failed with err ", err)
}

req, err := hook.BuildRequest("")
if err != nil {
t.Fatal("Failed to build request with error ", err)
}

if req.Header.Get("Content-Type") != ts.ExpectedContentType {
t.Fatalf("Expect Content-Type %s. Got %s", ts.ExpectedContentType, req.Header.Get("Content-Type"))
}
})
}

}

0 comments on commit 41d7933

Please sign in to comment.