Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SetAllowGetMethodPayload option ignored for io.Reader payload #541

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,21 @@ func TestClientAllowsGetMethodPayload(t *testing.T) {
assertEqual(t, payload, resp.String())
}

func TestClientAllowsGetMethodPayloadDisabled(t *testing.T) {
ts := createGetServer(t)
defer ts.Close()

c := dc()
c.SetAllowGetMethodPayload(false)

payload := bytes.NewReader([]byte("test-payload"))
resp, err := c.R().SetBody(payload).Get(ts.URL + "/get-method-payload-test")

assertError(t, err)
assertEqual(t, http.StatusOK, resp.StatusCode())
assertEqual(t, "", resp.String())
}

func TestClientRoundTripper(t *testing.T) {
c := NewWithClient(&http.Client{})
c.outputLogTo(ioutil.Discard)
Expand Down
15 changes: 4 additions & 11 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,7 @@ CL:

func createHTTPRequest(c *Client, r *Request) (err error) {
if r.bodyBuf == nil {
if reader, ok := r.Body.(io.Reader); ok {
r.RawRequest, err = http.NewRequest(r.Method, r.URL, reader)
} else if c.setContentLength || r.setContentLength {
if c.setContentLength || r.setContentLength {
r.RawRequest, err = http.NewRequest(r.Method, r.URL, http.NoBody)
} else {
r.RawRequest, err = http.NewRequest(r.Method, r.URL, nil)
Expand Down Expand Up @@ -442,14 +440,9 @@ func handleRequestBody(c *Client, r *Request) (err error) {
r.bodyBuf = nil

if reader, ok := r.Body.(io.Reader); ok {
if c.setContentLength || r.setContentLength { // keep backward compatibility
r.bodyBuf = acquireBuffer()
_, err = r.bodyBuf.ReadFrom(reader)
r.Body = nil
} else {
// Otherwise buffer less processing for `io.Reader`, sounds good.
return
}
r.bodyBuf = acquireBuffer()
_, err = r.bodyBuf.ReadFrom(reader)
r.Body = nil
} else if b, ok := r.Body.([]byte); ok {
bodyBytes = b
} else if s, ok := r.Body.(string); ok {
Expand Down