Browse Source

pass nil to http.NewRequest if body is nil

Will Norris 11 years ago
parent
commit
eb2a40b4d4
2 changed files with 19 additions and 1 deletions
  1. +2
    -1
      github/github.go
  2. +17
    -0
      github/github_test.go

+ 2
- 1
github/github.go View File

@ -151,8 +151,9 @@ func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Requ
u := c.BaseURL.ResolveReference(rel) u := c.BaseURL.ResolveReference(rel)
buf := new(bytes.Buffer)
var buf io.ReadWriter
if body != nil { if body != nil {
buf = new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(body) err := json.NewEncoder(buf).Encode(body)
if err != nil { if err != nil {
return nil, err return nil, err


+ 17
- 0
github/github_test.go View File

@ -225,6 +225,23 @@ func TestNewRequest_emptyUserAgent(t *testing.T) {
} }
} }
// If a nil body is passed to github.NewRequest, make sure that nil is also
// passed to http.NewRequest. In most cases, passing in io.Reader the returns
// no content is fine, since there is no difference between an HTTP request
// body that is an empty string versus one that is not set at all. However in
// certain cases, intermediate systems may treat these differently resulting in
// subtle errors.
func TestNewRequest_emptyBody(t *testing.T) {
c := NewClient(nil)
req, err := c.NewRequest("GET", "/", nil)
if err != nil {
t.Fatalf("NewRequest returned unexpected error: %v", err)
}
if req.Body != nil {
t.Fatalf("constructed request contains a non-nil Body")
}
}
func TestResponse_populatePageValues(t *testing.T) { func TestResponse_populatePageValues(t *testing.T) {
r := http.Response{ r := http.Response{
Header: http.Header{ Header: http.Header{


Loading…
Cancel
Save