Skip to content

Commit 102a1bf

Browse files
authored
Fix digest auth http: ContentLength=xxx with Body length 0 (#730)
1 parent 6242e6f commit 102a1bf

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

digest.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ func (dt *digestTransport) RoundTrip(req *http.Request) (*http.Response, error)
5555
req2.Header[k] = s
5656
}
5757

58+
// Fix http: ContentLength=xxx with Body length 0
59+
if req2.Body == nil {
60+
req2.ContentLength = 0
61+
} else if req2.GetBody != nil {
62+
var err error
63+
req2.Body, err = req2.GetBody()
64+
if err != nil {
65+
return nil, err
66+
}
67+
}
68+
5869
// Make a request to get the 401 that contains the challenge.
5970
resp, err := dt.transport.RoundTrip(req)
6071
if err != nil || resp.StatusCode != http.StatusUnauthorized {

request_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,25 @@ func TestRequestDigestAuthFail(t *testing.T) {
722722
logResponse(t, resp)
723723
}
724724

725+
func TestRequestDigestAuthWithBody(t *testing.T) {
726+
conf := defaultDigestServerConf()
727+
ts := createDigestServer(t, nil)
728+
defer ts.Close()
729+
730+
resp, err := dclr().
731+
SetDigestAuth(conf.username, conf.password).
732+
SetResult(&AuthSuccess{}).
733+
SetHeader(hdrContentTypeKey, "application/json").
734+
SetBody(map[string]interface{}{"zip_code": "00000", "city": "Los Angeles"}).
735+
Post(ts.URL + conf.uri)
736+
737+
assertError(t, err)
738+
assertEqual(t, http.StatusOK, resp.StatusCode())
739+
740+
t.Logf("Result Success: %q", resp.Result().(*AuthSuccess))
741+
logResponse(t, resp)
742+
}
743+
725744
func TestFormData(t *testing.T) {
726745
ts := createFormPostServer(t)
727746
defer ts.Close()

0 commit comments

Comments
 (0)