Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
Signed-off-by: albertteoh <albert.teoh@logz.io>
  • Loading branch information
albertteoh committed Mar 27, 2021
1 parent a591cc8 commit 9b216a0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
4 changes: 1 addition & 3 deletions cmd/query/app/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,7 @@ func (aH *APIHandler) writeJSON(w http.ResponseWriter, r *http.Request, response
return
}
w.Header().Set("Content-Type", "application/json")
if b, err := w.Write(resp); err != nil {
if _, err := w.Write(resp); err != nil {
aH.handleError(w, fmt.Errorf("failed writing HTTP response: %w", err), http.StatusInternalServerError)
} else {
aH.logger.Debug("Successfully wrote HTTP response", zap.Int("bytes", b))
}
}
22 changes: 6 additions & 16 deletions cmd/query/app/http_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ func TestLogOnServerError(t *testing.T) {
assert.Equal(t, e, (*l.logs)[0].f[0].Interface)
}

// httpResponseErrWriter implements the http.ResponseWriter interface
// that returns an error on Write.
// httpResponseErrWriter implements the http.ResponseWriter interface that returns an error on Write.
type httpResponseErrWriter struct{}

func (h *httpResponseErrWriter) Write([]byte) (int, error) {
Expand All @@ -197,13 +196,6 @@ func (h *httpResponseErrWriter) WriteHeader(statusCode int) {}
func (h *httpResponseErrWriter) Header() http.Header {
return http.Header{}
}

// ErrHandlerFunc injects the httpResponseErrWriter into the HTTP handler.
type ErrHandlerFunc func(http.ResponseWriter, *http.Request)

func (f ErrHandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f(&httpResponseErrWriter{}, r)
}
func TestWriteJSON(t *testing.T) {
testCases := []struct {
name string
Expand Down Expand Up @@ -250,19 +242,17 @@ func TestWriteJSON(t *testing.T) {
}

for _, testCase := range testCases {
t.Run(testCase.param, func(t *testing.T) {
t.Run(testCase.name, func(t *testing.T) {
apiHandler := &APIHandler{
logger: zap.NewNop(),
}
httpHandler := func(w http.ResponseWriter, r *http.Request) {
if testCase.httpWriteErr {
w = &httpResponseErrWriter{}
}
apiHandler.writeJSON(w, r, &testCase.data)
}
var server *httptest.Server
if testCase.httpWriteErr {
server = httptest.NewServer(ErrHandlerFunc(httpHandler))
} else {
server = httptest.NewServer(http.HandlerFunc(httpHandler))
}
server := httptest.NewServer(http.HandlerFunc(httpHandler))
defer server.Close()

out := get(server.URL + testCase.param)
Expand Down

0 comments on commit 9b216a0

Please sign in to comment.