Skip to content

Commit

Permalink
Fix test errors
Browse files Browse the repository at this point in the history
  • Loading branch information
biilmann committed Oct 26, 2017
1 parent e13547c commit f9108e4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
4 changes: 2 additions & 2 deletions api/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestHookTimeout(t *testing.T) {
w := Webhook{
WebhookConfig: config,
}
b, err := w.trigger()
_, err := w.trigger()
require.Error(t, err)
herr, ok := err.(*HTTPError)
require.True(t, ok)
Expand All @@ -113,7 +113,7 @@ func TestHookNoServer(t *testing.T) {
w := Webhook{
WebhookConfig: config,
}
b, err := w.trigger()
_, err := w.trigger()
require.Error(t, err)
herr, ok := err.(*HTTPError)
require.True(t, ok)
Expand Down
21 changes: 17 additions & 4 deletions api/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (w *Webhook) trigger() (io.ReadCloser, error) {

req, err := http.NewRequest(http.MethodPost, w.URL, bytes.NewBuffer(w.payload))
if err != nil {
return internalServerError("Failed to make request object").WithInternalError(err)
return nil, internalServerError("Failed to make request object").WithInternalError(err)
}
req.Header.Set("Content-Type", "application/json")
watcher, req := watchForConnection(req)
Expand All @@ -80,13 +80,16 @@ func (w *Webhook) trigger() (io.ReadCloser, error) {
if terr, ok := err.(net.Error); ok && terr.Timeout() {
// timed out - try again?
if i == w.Retries-1 {
closeBody(rsp)
return nil, httpError(http.StatusGatewayTimeout, "Failed to perform webhook in time frame (%d seconds)", timeout.Seconds())
}
hooklog.Info("Request timed out")
continue
} else if watcher.gotConn {
closeBody(rsp)
return nil, internalServerError("Failed to trigger webhook to %s", w.URL).WithInternalError(err)
} else {
closeBody(rsp)
return nil, httpError(http.StatusBadGateway, "Failed to connect to %s", w.URL)
}
}
Expand All @@ -98,7 +101,11 @@ func (w *Webhook) trigger() (io.ReadCloser, error) {
switch rsp.StatusCode {
case http.StatusOK, http.StatusNoContent, http.StatusAccepted:
rspLog.Infof("Finished processing webhook in %s", dur)
return rsp.Body, nil
var body io.ReadCloser
if rsp.ContentLength > 0 {
body = rsp.Body
}
return body, nil
default:
rspLog.Infof("Bad response for webhook %d in %s", rsp.StatusCode, dur)
}
Expand All @@ -117,9 +124,15 @@ func (w *Webhook) generateSignature() (string, error) {
return tokenString, nil
}

func closeBody(rsp *http.Response) {
if rsp != nil && rsp.Body != nil {
rsp.Body.Close()
}
}

func triggerSignupHook(user *models.User, instanceID, jwtSecret string, hconfig *conf.WebhookConfig) (io.ReadCloser, error) {
if hconfig.URL == "" {
return nil
return nil, nil
}

payload := struct {
Expand All @@ -133,7 +146,7 @@ func triggerSignupHook(user *models.User, instanceID, jwtSecret string, hconfig
}
data, err := json.Marshal(&payload)
if err != nil {
return internalServerError("Failed to serialize the data for signup webhook").WithInternalError(err)
return nil, internalServerError("Failed to serialize the data for signup webhook").WithInternalError(err)
}
w := Webhook{
WebhookConfig: hconfig,
Expand Down
2 changes: 1 addition & 1 deletion api/signup.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ func (a *API) signupNewUser(ctx context.Context, params *SignupParams, aud strin
if err != nil {
return nil, err
}
defer body.Close()
if body != nil {
defer body.Close()
webhookRsp := &WebhookResponse{}
decoder := json.NewDecoder(body)
if err = decoder.Decode(webhookRsp); err != nil {
Expand Down

0 comments on commit f9108e4

Please sign in to comment.