Browse Source

ignore EOF error when json decoding empty response

In particular, this handles the case of trying to decode No Content
(204) responses.
Maksim Zhylinski 10 years ago
committed by Will Norris
parent
commit
c9c9f3beea
2 changed files with 21 additions and 0 deletions
  1. +3
    -0
      github/github.go
  2. +18
    -0
      github/github_test.go

+ 3
- 0
github/github.go View File

@ -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


+ 18
- 0
github/github_test.go View File

@ -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


Loading…
Cancel
Save