Skip to content

Commit

Permalink
Merge pull request #390 from vixns/templated_url2
Browse files Browse the repository at this point in the history
parse http notifier open-url and close-url as templates.
  • Loading branch information
bai committed Aug 14, 2019
2 parents a449cc4 + 8d3ec19 commit e685792
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
14 changes: 13 additions & 1 deletion core/internal/notifier/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,20 @@ func (module *HTTPNotifier) Notify(status *protocol.ConsumerGroupStatus, eventID
return
}

urlTmpl, err := template.New("url").Parse(url)
if err != nil {
logger.Error("failed to parse url", zap.Error(err))
return
}

urlToSend, err := executeTemplate(urlTmpl, module.extras, status, eventID, startTime)
if err != nil {
logger.Error("failed to assemble url", zap.Error(err))
return
}

// Send request to HTTP endpoint
req, err := http.NewRequest(method, url, bytesToSend)
req, err := http.NewRequest(method, urlToSend.String(), bytesToSend)
if err != nil {
logger.Error("failed to create request", zap.Error(err))
return
Expand Down
8 changes: 6 additions & 2 deletions core/internal/notifier/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ func TestHttpNotifier_Notify_Open(t *testing.T) {
assert.True(t, ok, "Expected to receive Token header")
assert.Equalf(t, "testtoken", tokenHeaders[0], "Expected Token header to be 'testtoken', not '%v'", tokenHeaders[0])

assert.Equalf(t, "id=testidstring", r.URL.RawQuery, "Expected URL querystring to be id=testidstring, not %v", r.URL)

decoder := json.NewDecoder(r.Body)
var req HTTPRequest
err := decoder.Decode(&req)
Expand All @@ -126,7 +128,7 @@ func TestHttpNotifier_Notify_Open(t *testing.T) {
defer ts.Close()

module := fixtureHTTPNotifier()
viper.Set("notifier.test.url-open", ts.URL)
viper.Set("notifier.test.url-open", fmt.Sprintf("%s?id={{.ID}}", ts.URL))

// Template sends the ID, cluster, and group
module.templateOpen, _ = template.New("test").Parse("{\"template\":\"template_open\",\"id\":\"{{.ID}}\",\"cluster\":\"{{.Cluster}}\",\"group\":\"{{.Group}}\"}")
Expand Down Expand Up @@ -155,6 +157,8 @@ func TestHttpNotifier_Notify_Close(t *testing.T) {
assert.True(t, ok, "Expected to receive Token header")
assert.Equalf(t, "testtoken", tokenHeaders[0], "Expected Token header to be 'testtoken', not '%v'", tokenHeaders[0])

assert.Equalf(t, "id=testidstring", r.URL.RawQuery, "Expected URL querystring to be id=testidstring, not %v", r.URL)

decoder := json.NewDecoder(r.Body)
var req HTTPRequest
err := decoder.Decode(&req)
Expand All @@ -178,7 +182,7 @@ func TestHttpNotifier_Notify_Close(t *testing.T) {

module := fixtureHTTPNotifier()
viper.Set("notifier.test.send-close", true)
viper.Set("notifier.test.url-close", ts.URL)
viper.Set("notifier.test.url-close", fmt.Sprintf("%s?id={{.ID}}", ts.URL))

// Template sends the ID, cluster, and group
module.templateClose, _ = template.New("test").Parse("{\"template\":\"template_close\",\"id\":\"{{.ID}}\",\"cluster\":\"{{.Cluster}}\",\"group\":\"{{.Group}}\"}")
Expand Down

0 comments on commit e685792

Please sign in to comment.