Skip to content

Commit 3fabd3f

Browse files
authored
Fix trigger response handling for streamed 0 length response body (#345)
1 parent 381cbd3 commit 3fabd3f

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

api/hooks.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/hex"
88
"encoding/json"
99
"io"
10+
"io/ioutil"
1011
"net"
1112
"net/http"
1213
"net/http/httptrace"
@@ -239,8 +240,20 @@ func triggerHook(ctx context.Context, hookURL *url.URL, secret string, conn *sto
239240
}
240241
}()
241242
if err == nil && body != nil {
243+
// handle case where response from the trigger is streamed but has no
244+
// Body
245+
data, err := ioutil.ReadAll(body)
246+
if err != nil {
247+
return internalServerError("Webhook returned malformed BODY: %v", err).WithInternalError(err)
248+
}
249+
250+
if len(data) == 0 {
251+
return nil
252+
}
253+
254+
bodyReader := bytes.NewReader(data)
242255
webhookRsp := &WebhookResponse{}
243-
decoder := json.NewDecoder(body)
256+
decoder := json.NewDecoder(bodyReader)
244257
if err = decoder.Decode(webhookRsp); err != nil {
245258
return internalServerError("Webhook returned malformed JSON: %v", err).WithInternalError(err)
246259
}

api/signup_test.go

+2-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package api
33
import (
44
"bytes"
55
"encoding/json"
6-
"fmt"
76
"io/ioutil"
87
"net/http"
98
"net/http/httptest"
@@ -131,13 +130,6 @@ func (ts *SignupTestSuite) TestWebhookTriggered() {
131130

132131
w.WriteHeader(http.StatusOK)
133132
w.(http.Flusher).Flush() // needed so we don't set a content-length
134-
135-
pl := `{
136-
"app_metadata": {
137-
"roles": ["dev"]
138-
}
139-
}`
140-
fmt.Fprint(w, pl)
141133
}))
142134
defer svr.Close()
143135

@@ -168,10 +160,8 @@ func (ts *SignupTestSuite) TestWebhookTriggered() {
168160
assert.Equal(http.StatusOK, w.Code)
169161
assert.Equal(1, callCount)
170162

171-
var user models.User
172-
require.NoError(json.NewDecoder(w.Body).Decode(&user))
173-
174-
assert.EqualValues(models.JSONMap{"roles": []interface{}{"dev"}}, user.AppMetaData)
163+
// http stdlib doesn't set Body as http.NoBody if Content-Length = -1
164+
require.True(w.Result().Body != http.NoBody)
175165
}
176166

177167
func (ts *SignupTestSuite) TestFailingWebhook() {

0 commit comments

Comments
 (0)