diff --git a/engine/actions/trigger/trigger.go b/engine/actions/trigger/trigger.go index 07cb46f..1ec1774 100644 --- a/engine/actions/trigger/trigger.go +++ b/engine/actions/trigger/trigger.go @@ -28,6 +28,13 @@ func (a *action) String() string { func (a *action) Apply(state multistep.StateBag) { meta := state.Get("data").(types.Data) request := a.prepareRequest(meta) + a.logger.DebugWith(log.MetaFields{ + log.F("issue", meta.GetShortName()), + log.F("url", request.URL), + log.F("header", request.Header), + log.F("method", request.Method), + log.F("content-length", request.ContentLength), + }, "Prepared Request") response, e := a.client.Do(request) if e != nil { a.err = fmt.Errorf("Triggering to %s, resulted in error. %s", diff --git a/engine/actions/trigger/trigger_test.go b/engine/actions/trigger/trigger_test.go index 330d16f..af239f6 100644 --- a/engine/actions/trigger/trigger_test.go +++ b/engine/actions/trigger/trigger_test.go @@ -5,6 +5,8 @@ import ( "os" "testing" + "io/ioutil" + "github.com/bivas/rivi/mocks" "github.com/bivas/rivi/types" "github.com/bivas/rivi/util/log" @@ -150,3 +152,65 @@ func TestEnvironmentEndpoint(t *testing.T) { action.Apply(state.New(&mocks.MockConfiguration{}, meta)) assert.Nil(t, action.err, "error when sending trigger") } + +func TestContentType(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + meta := &mocks.MockData{ + Number: 1, + Title: "title1", + State: "tested", + Owner: "test", + Repo: "repo1", + Origin: types.Origin{User: "tester"}, + } + rule := &rule{ + Endpoint: "http://example.com/trigger", + ContentType: "foofoofoo", + } + rule.Defaults() + httpmock.RegisterResponder( + "POST", + "http://example.com/trigger", + func(req *http.Request) (*http.Response, error) { + assert.Equal(t, "trigger", req.Header.Get("X-Rivi-Event"), "missing correct event") + assert.Equal(t, "Rivi-Agent/1.0", req.UserAgent(), "user agent") + assert.Equal(t, "foofoofoo", req.Header.Get("Content-Type"), "content type") + return httpmock.NewStringResponse(200, ""), nil + }) + action := &action{rule: rule, client: http.DefaultClient, logger: log.Get("trigger.test")} + action.Apply(state.New(&mocks.MockConfiguration{}, meta)) + assert.Nil(t, action.err, "error when sending trigger") +} + +func TestBody(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + meta := &mocks.MockData{ + Number: 1, + Title: "title1", + State: "tested", + Owner: "test", + Repo: "repo1", + Origin: types.Origin{User: "tester"}, + } + rule := &rule{ + Endpoint: "http://example.com/trigger", + Body: `json='{"value":"{{.Origin.User}}"}'`, + } + rule.Defaults() + httpmock.RegisterResponder( + "POST", + "http://example.com/trigger", + func(req *http.Request) (*http.Response, error) { + assert.Equal(t, "trigger", req.Header.Get("X-Rivi-Event"), "missing correct event") + assert.Equal(t, "Rivi-Agent/1.0", req.UserAgent(), "user agent") + bs, err := ioutil.ReadAll(req.Body) + assert.NoError(t, err, "error in content") + assert.Equal(t, `json='{"value":"tester"}'`, string(bs), "content") + return httpmock.NewStringResponse(200, ""), nil + }) + action := &action{rule: rule, client: http.DefaultClient, logger: log.Get("trigger.test")} + action.Apply(state.New(&mocks.MockConfiguration{}, meta)) + assert.Nil(t, action.err, "error when sending trigger") +}