Skip to content

Commit

Permalink
add trigger debug information (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
bivas authored Oct 2, 2017
1 parent dd0f728 commit 6461a8e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
7 changes: 7 additions & 0 deletions engine/actions/trigger/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
64 changes: 64 additions & 0 deletions engine/actions/trigger/trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}

0 comments on commit 6461a8e

Please sign in to comment.