hookHandler: Bug with multiple hooks having the same ID

It was possible to have multiple hooks with the same ID, but the return
in the for/if of the hookHandler method were stopping the loop, thus making
it impossible to continue the executions of the other hooks.

This isn't the behaviour expected because a service might be down,
because you want to run more than one hook, or don't want to stop due to
one that failed. This commit fixes it, and add tests associated.

The hooks have been copied from the github one, slightly modified and
spread in the template file.

A "dummy" hook which is never called has also been added, just in case
it could mess with some other, and a test for non existing hook has also
been added to the tests.

This PR fixes: https://github.com/adnanh/webhook/issues/76
This commit is contained in:
Hugo Rosnet 2016-06-09 18:43:37 +02:00
parent 18b0573bc4
commit c3c7b5fc1a
3 changed files with 253 additions and 7 deletions

View file

@ -187,6 +187,8 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
}
// handle hook
executed := 0
for _, h := range matchedHooks {
err := h.ParseJSONParameters(&headers, &query, &payload)
@ -195,7 +197,7 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
log.Printf(msg)
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, msg)
return
continue
}
var ok bool
@ -209,7 +211,7 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
log.Printf(msg)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, msg)
return
continue
}
}
@ -227,15 +229,18 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
go handleHook(h, &headers, &query, &payload, &body)
fmt.Fprintf(w, h.ResponseMessage)
}
return
executed++
continue
}
}
// if none of the hooks got triggered
log.Printf("%s got matched (%d time(s)), but didn't get triggered because the trigger rules were not satisfied\n", matchedHooks[0].ID, len(matchedHooks))
fmt.Fprintf(w, "Hook rules were not satisfied.")
if executed == 0 {
log.Printf("%s got matched (%d time(s)), but didn't get triggered because the trigger rules were not satisfied\n", matchedHooks[0].ID, len(matchedHooks))
fmt.Fprintf(w, "Hook rules were not satisfied.")
} else if executed != len(matchedHooks) {
log.Printf("%s got matched (%d time(s)), but executed only %d times. Please review previous log messages\n", matchedHooks[0].ID, len(matchedHooks), executed)
}
} else {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Hook not found.")