Skip to content

Commit

Permalink
Fix webhook content type for custom templates (TykTechnologies#2393)
Browse files Browse the repository at this point in the history
  • Loading branch information
buger authored Jul 10, 2019
1 parent 36848bf commit 1d3d3f5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
14 changes: 9 additions & 5 deletions gateway/event_handler_webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ type WebHookHandler struct {
template *template.Template // non-nil if Init is run without error
store storage.Handler

useDefaultTemplate bool
dashboardService DashboardServiceSender
contentType string
dashboardService DashboardServiceSender
}

// createConfigObject by default tyk will provide a map[string]interface{} type as a conf, converting it
Expand Down Expand Up @@ -82,6 +82,10 @@ func (w *WebHookHandler) Init(handlerConf interface{}) error {
"target": w.conf.TargetPath,
}).Warning("Custom template load failure, using default: ", err)
}

if strings.HasSuffix(w.conf.TemplatePath, ".json") {
w.contentType = "application/json"
}
}

// We use the default if TemplatePath was empty or if we failed
Expand All @@ -99,7 +103,7 @@ func (w *WebHookHandler) Init(handlerConf interface{}) error {
}).Error("Could not load the default template: ", err)
return err
}
w.useDefaultTemplate = true
w.contentType = "application/json"
}

log.WithFields(logrus.Fields{
Expand Down Expand Up @@ -190,8 +194,8 @@ func (w *WebHookHandler) BuildRequest(reqBody string) (*http.Request, error) {
req.Header.Set(key, val)
}

if req.Header.Get("Content-Type") == "" && w.useDefaultTemplate {
req.Header.Set("Content-Type", "application/json")
if req.Header.Get("Content-Type") == "" {
req.Header.Set("Content-Type", w.contentType)
}

return req, nil
Expand Down
6 changes: 3 additions & 3 deletions gateway/event_handler_webhooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func createGetHandler() *WebHookHandler {
TargetPath: TestHttpGet,
Method: "GET",
EventTimeout: 10,
TemplatePath: "templates/default_webhook.json",
TemplatePath: "../templates/default_webhook.json",
HeaderList: map[string]string{"x-tyk-test": "TEST"},
}
ev := &WebHookHandler{}
Expand All @@ -28,7 +28,7 @@ func TestNewValid(t *testing.T) {
err := h.Init(map[string]interface{}{
"method": "POST",
"target_path": testHttpPost,
"template_path": "templates/default_webhook.json",
"template_path": "../templates/default_webhook.json",
"header_map": map[string]string{"X-Tyk-Test-Header": "Tyk v1.BANANA"},
"event_timeout": 10,
})
Expand All @@ -42,7 +42,7 @@ func TestNewInvalid(t *testing.T) {
err := h.Init(map[string]interface{}{
"method": 123,
"target_path": testHttpPost,
"template_path": "templates/default_webhook.json",
"template_path": "../templates/default_webhook.json",
"header_map": map[string]string{"X-Tyk-Test-Header": "Tyk v1.BANANA"},
"event_timeout": 10,
})
Expand Down

0 comments on commit 1d3d3f5

Please sign in to comment.