From c9c9f3beeac50d26ec5440b8da68efeb9506d25e Mon Sep 17 00:00:00 2001 From: Maksim Zhylinski Date: Fri, 27 Nov 2015 23:28:57 +0300 Subject: [PATCH] ignore EOF error when json decoding empty response In particular, this handles the case of trying to decode No Content (204) responses. --- github/github.go | 3 +++ github/github_test.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/github/github.go b/github/github.go index 76939f8..7e9471b 100644 --- a/github/github.go +++ b/github/github.go @@ -332,6 +332,9 @@ func (c *Client) Do(req *http.Request, v interface{}) (*Response, error) { io.Copy(w, resp.Body) } else { err = json.NewDecoder(resp.Body).Decode(v) + if err == io.EOF { + err = nil // ignore EOF errors caused by empty response body + } } } return response, err diff --git a/github/github_test.go b/github/github_test.go index 7ec4617..eac121e 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -430,6 +430,24 @@ func TestDo_rateLimit_errorResponse(t *testing.T) { } } +func TestDo_noContent(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNoContent) + }) + + var body json.RawMessage + + req, _ := client.NewRequest("GET", "/", nil) + _, err := client.Do(req, &body) + + if err != nil { + t.Fatalf("Do returned unexpected error: %v", err) + } +} + func TestSanitizeURL(t *testing.T) { tests := []struct { in, want string