|  | 
| 5 | 5 | 	"os" | 
| 6 | 6 | 	"testing" | 
| 7 | 7 | 
 | 
|  | 8 | +	"io/ioutil" | 
|  | 9 | + | 
| 8 | 10 | 	"github.com/bivas/rivi/mocks" | 
| 9 | 11 | 	"github.com/bivas/rivi/types" | 
| 10 | 12 | 	"github.com/bivas/rivi/util/log" | 
| @@ -150,3 +152,65 @@ func TestEnvironmentEndpoint(t *testing.T) { | 
| 150 | 152 | 	action.Apply(state.New(&mocks.MockConfiguration{}, meta)) | 
| 151 | 153 | 	assert.Nil(t, action.err, "error when sending trigger") | 
| 152 | 154 | } | 
|  | 155 | + | 
|  | 156 | +func TestContentType(t *testing.T) { | 
|  | 157 | +	httpmock.Activate() | 
|  | 158 | +	defer httpmock.DeactivateAndReset() | 
|  | 159 | +	meta := &mocks.MockData{ | 
|  | 160 | +		Number: 1, | 
|  | 161 | +		Title:  "title1", | 
|  | 162 | +		State:  "tested", | 
|  | 163 | +		Owner:  "test", | 
|  | 164 | +		Repo:   "repo1", | 
|  | 165 | +		Origin: types.Origin{User: "tester"}, | 
|  | 166 | +	} | 
|  | 167 | +	rule := &rule{ | 
|  | 168 | +		Endpoint:    "http://example.com/trigger", | 
|  | 169 | +		ContentType: "foofoofoo", | 
|  | 170 | +	} | 
|  | 171 | +	rule.Defaults() | 
|  | 172 | +	httpmock.RegisterResponder( | 
|  | 173 | +		"POST", | 
|  | 174 | +		"http://example.com/trigger", | 
|  | 175 | +		func(req *http.Request) (*http.Response, error) { | 
|  | 176 | +			assert.Equal(t, "trigger", req.Header.Get("X-Rivi-Event"), "missing correct event") | 
|  | 177 | +			assert.Equal(t, "Rivi-Agent/1.0", req.UserAgent(), "user agent") | 
|  | 178 | +			assert.Equal(t, "foofoofoo", req.Header.Get("Content-Type"), "content type") | 
|  | 179 | +			return httpmock.NewStringResponse(200, ""), nil | 
|  | 180 | +		}) | 
|  | 181 | +	action := &action{rule: rule, client: http.DefaultClient, logger: log.Get("trigger.test")} | 
|  | 182 | +	action.Apply(state.New(&mocks.MockConfiguration{}, meta)) | 
|  | 183 | +	assert.Nil(t, action.err, "error when sending trigger") | 
|  | 184 | +} | 
|  | 185 | + | 
|  | 186 | +func TestBody(t *testing.T) { | 
|  | 187 | +	httpmock.Activate() | 
|  | 188 | +	defer httpmock.DeactivateAndReset() | 
|  | 189 | +	meta := &mocks.MockData{ | 
|  | 190 | +		Number: 1, | 
|  | 191 | +		Title:  "title1", | 
|  | 192 | +		State:  "tested", | 
|  | 193 | +		Owner:  "test", | 
|  | 194 | +		Repo:   "repo1", | 
|  | 195 | +		Origin: types.Origin{User: "tester"}, | 
|  | 196 | +	} | 
|  | 197 | +	rule := &rule{ | 
|  | 198 | +		Endpoint: "http://example.com/trigger", | 
|  | 199 | +		Body:     `json='{"value":"{{.Origin.User}}"}'`, | 
|  | 200 | +	} | 
|  | 201 | +	rule.Defaults() | 
|  | 202 | +	httpmock.RegisterResponder( | 
|  | 203 | +		"POST", | 
|  | 204 | +		"http://example.com/trigger", | 
|  | 205 | +		func(req *http.Request) (*http.Response, error) { | 
|  | 206 | +			assert.Equal(t, "trigger", req.Header.Get("X-Rivi-Event"), "missing correct event") | 
|  | 207 | +			assert.Equal(t, "Rivi-Agent/1.0", req.UserAgent(), "user agent") | 
|  | 208 | +			bs, err := ioutil.ReadAll(req.Body) | 
|  | 209 | +			assert.NoError(t, err, "error in content") | 
|  | 210 | +			assert.Equal(t, `json='{"value":"tester"}'`, string(bs), "content") | 
|  | 211 | +			return httpmock.NewStringResponse(200, ""), nil | 
|  | 212 | +		}) | 
|  | 213 | +	action := &action{rule: rule, client: http.DefaultClient, logger: log.Get("trigger.test")} | 
|  | 214 | +	action.Apply(state.New(&mocks.MockConfiguration{}, meta)) | 
|  | 215 | +	assert.Nil(t, action.err, "error when sending trigger") | 
|  | 216 | +} | 
0 commit comments