@@ -38,6 +38,7 @@ func TestEventDispatcher(t *testing.T) {
38
38
Handler TestEventHandler
39
39
Options []DispatcherOption
40
40
Event string
41
+ Invalid bool
41
42
42
43
ResponseCode int
43
44
ResponseBody string
@@ -65,7 +66,7 @@ func TestEventDispatcher(t *testing.T) {
65
66
ResponseCode : 200 ,
66
67
CallCount : 1 ,
67
68
},
68
- "defaultErrorHandlerReturns500 " : {
69
+ "defaultErrorHandlerReturns500OnError " : {
69
70
Handler : TestEventHandler {
70
71
Types : []string {"pull_request" },
71
72
Fn : func (ctx context.Context , eventType , deliveryID string , payload []byte ) error {
@@ -77,6 +78,15 @@ func TestEventDispatcher(t *testing.T) {
77
78
ResponseBody : "Internal Server Error\n " ,
78
79
CallCount : 1 ,
79
80
},
81
+ "defaultErrorHandlerReturns400OnInvalid" : {
82
+ Handler : TestEventHandler {
83
+ Types : []string {"pull_request" },
84
+ },
85
+ Event : "pull_request" ,
86
+ Invalid : true ,
87
+ ResponseCode : 400 ,
88
+ ResponseBody : "Invalid webhook headers or payload\n " ,
89
+ },
80
90
"callsCustomErrorCallback" : {
81
91
Handler : TestEventHandler {
82
92
Types : []string {"pull_request" },
@@ -151,7 +161,7 @@ func TestEventDispatcher(t *testing.T) {
151
161
h := test .Handler
152
162
d := NewEventDispatcher ([]EventHandler {& h }, testHookSecret , test .Options ... )
153
163
154
- req := newSignedRequest (test .Event , name )
164
+ req := newHookRequest (test .Event , name , ! test . Invalid )
155
165
res := httptest .NewRecorder ()
156
166
d .ServeHTTP (res , req )
157
167
@@ -181,17 +191,19 @@ func TestSetAndGetResponder(t *testing.T) {
181
191
})
182
192
}
183
193
184
- func newSignedRequest (eventType , id string ) * http.Request {
194
+ func newHookRequest (eventType , id string , signed bool ) * http.Request {
185
195
body := []byte (fmt .Sprintf (`{"type":"%s"}` , eventType ))
186
196
187
197
req := httptest .NewRequest (http .MethodPost , "/api/github/hook" , bytes .NewReader (body ))
188
198
req .Header .Set ("Content-Type" , "application/json" )
189
199
req .Header .Set ("X-Github-Event" , eventType )
190
200
req .Header .Set ("X-Github-Delivery" , id )
191
201
192
- mac := hmac .New (sha1 .New , []byte (testHookSecret ))
193
- mac .Write (body )
194
- req .Header .Set ("X-Hub-Signature" , fmt .Sprintf ("sha1=%x" , mac .Sum (nil )))
202
+ if signed {
203
+ mac := hmac .New (sha1 .New , []byte (testHookSecret ))
204
+ mac .Write (body )
205
+ req .Header .Set ("X-Hub-Signature" , fmt .Sprintf ("sha1=%x" , mac .Sum (nil )))
206
+ }
195
207
196
208
log := zerolog .New (os .Stdout )
197
209
req = req .WithContext (log .WithContext (req .Context ()))
0 commit comments