Skip to content
This repository has been archived by the owner on Sep 14, 2020. It is now read-only.

Commit

Permalink
Merge pull request #7 from wolfeidau/feat_add_closenotify_support
Browse files Browse the repository at this point in the history
feat(Response) Add CloseNotify support.
  • Loading branch information
tj committed Feb 12, 2018
2 parents 3794acc + ed0898b commit 0bee09a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
21 changes: 16 additions & 5 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ import (
// ResponseWriter implements the http.ResponseWriter interface
// in order to support the API Gateway Lambda HTTP "protocol".
type ResponseWriter struct {
out events.APIGatewayProxyResponse
buf bytes.Buffer
header http.Header
wroteHeader bool
out events.APIGatewayProxyResponse
buf bytes.Buffer
header http.Header
wroteHeader bool
closeNotifyCh chan bool
}

// NewResponse returns a new response writer to capture http output.
func NewResponse() *ResponseWriter {
return &ResponseWriter{}
return &ResponseWriter{
closeNotifyCh: make(chan bool, 1),
}
}

// Header implementation.
Expand Down Expand Up @@ -66,6 +69,11 @@ func (w *ResponseWriter) WriteHeader(status int) {
w.wroteHeader = true
}

// CloseNotify notify when the response is closed
func (w *ResponseWriter) CloseNotify() <-chan bool {
return w.closeNotifyCh
}

// End the request.
func (w *ResponseWriter) End() events.APIGatewayProxyResponse {
w.out.IsBase64Encoded = isBinary(w.header)
Expand All @@ -76,6 +84,9 @@ func (w *ResponseWriter) End() events.APIGatewayProxyResponse {
w.out.Body = w.buf.String()
}

// notify end
w.closeNotifyCh <- true

return w.out
}

Expand Down
1 change: 1 addition & 0 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestResponseWriter_Write_text(t *testing.T) {
assert.Equal(t, "hello world\n", e.Body)
assert.Equal(t, kind, e.Headers["Content-Type"])
assert.False(t, e.IsBase64Encoded)
assert.True(t, <-w.CloseNotify())
})
}
}
Expand Down

0 comments on commit 0bee09a

Please sign in to comment.