@@ -29,7 +29,8 @@ func TestRunApp(test *testing.T) {
2929 var capturedRequest []byte
3030 testServer := httptest .NewServer (http .HandlerFunc (func (writer http.ResponseWriter , request * http.Request ) {
3131 capturedRequest , _ = ioutil .ReadAll (request .Body )
32- writer .WriteHeader (400 )
32+ writer .Header ().Set ("Content-Type" , "application/json" )
33+ fmt .Fprintln (writer , `{"success":true}` )
3334 }))
3435 defer testServer .Close ()
3536 setEnvironmentVariable (test , "WEBHOOK" , testServer .URL )
@@ -134,6 +135,54 @@ func TestRun(test *testing.T) {
134135 assert .Equal (test , "general" , jsonRequest ["channel" ])
135136}
136137
138+ func TestRunSlackCallFailed (test * testing.T ) {
139+ cases := []int {
140+ 400 ,
141+ 500 ,
142+ }
143+
144+ for _ , statusCode := range cases {
145+ // Test HTTP server
146+ var capturedRequest []byte
147+ testServer := httptest .NewServer (http .HandlerFunc (func (writer http.ResponseWriter , request * http.Request ) {
148+ capturedRequest , _ = ioutil .ReadAll (request .Body )
149+ writer .WriteHeader (statusCode )
150+ }))
151+ defer testServer .Close ()
152+
153+ set := flag .NewFlagSet ("test" , 0 )
154+ set .String ("text" , "Build failed!" , "" )
155+ set .String ("color" , "red" , "" )
156+ set .String ("title" , "Build notification" , "" )
157+ set .String ("channel" , "general" , "" )
158+ set .String ("webhook" , testServer .URL , "" )
159+
160+ context := cli .NewContext (nil , set , nil )
161+ actual := run (context )
162+
163+ // Verify error
164+ assert .NotNil (test , actual )
165+ assert .Equal (test , "HTTP request to Slack failed" , actual .Error ())
166+
167+ // Verify request
168+ jsonRequest := make (map [string ]interface {})
169+ json .Unmarshal (capturedRequest , & jsonRequest )
170+ assert .Equal (test , 2 , len (jsonRequest ))
171+
172+ var attachments []interface {}
173+ attachments = jsonRequest ["attachments" ].([]interface {})
174+ var attachment map [string ]interface {}
175+ attachment = attachments [0 ].(map [string ]interface {})
176+
177+ assert .Equal (test , 1 , len (attachments ))
178+ assert .Equal (test , 3 , len (attachment ))
179+ assert .Equal (test , "Build failed!" , attachment ["text" ])
180+ assert .Equal (test , "red" , attachment ["color" ])
181+ assert .Equal (test , "Build notification" , attachment ["title" ])
182+ assert .Equal (test , "general" , jsonRequest ["channel" ])
183+ }
184+ }
185+
137186func TestBuildPayload (test * testing.T ) {
138187 cases := []struct {
139188 parameters map [string ]string
0 commit comments