Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"net/http"
"net/url"
"os"
"reflect"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -154,6 +155,10 @@ func getBodyReaderAndContentLength(rawBody interface{}) (ReaderFunc, int64, erro
var bodyReader ReaderFunc
var contentLength int64

if isNil(rawBody) {
return bodyReader, contentLength, nil
}

switch body := rawBody.(type) {
// If they gave us a function already, great! Use it.
case ReaderFunc:
Expand Down Expand Up @@ -772,3 +777,7 @@ func (c *Client) StandardClient() *http.Client {
Transport: &RoundTripper{Client: c},
}
}

func isNil(v interface{}) bool {
return v == nil || ((reflect.ValueOf(v).Kind() == reflect.Ptr || reflect.ValueOf(v).Kind() == reflect.Slice) && reflect.ValueOf(v).IsNil())
}
18 changes: 18 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,3 +842,21 @@ func TestClient_StandardClient(t *testing.T) {
t.Fatalf("expected %v, got %v", client, v)
}
}

func Test_NewRequest(t *testing.T) {
req, err := NewRequest("GET", "localhost:1234", nil)
if err != nil {
t.Fatalf("failed to create a request: %s", err)
}

var b []byte
b = nil
r, err := NewRequest("GET", "localhost:1234", b)
if err != nil {
t.Fatalf("failed to create a request: %s", err)
}

if req.body != nil || r.body != nil {
t.Fatalf("expected nil body for both requests, got:%v %v", req.body, r.body)
}
}