Skip to content

Commit

Permalink
Fix digest auth http: ContentLength=xxx with Body length 0 (#730)
Browse files Browse the repository at this point in the history
  • Loading branch information
icepie committed Oct 14, 2023
1 parent 6242e6f commit 102a1bf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
11 changes: 11 additions & 0 deletions digest.go
Expand Up @@ -55,6 +55,17 @@ func (dt *digestTransport) RoundTrip(req *http.Request) (*http.Response, error)
req2.Header[k] = s
}

// Fix http: ContentLength=xxx with Body length 0
if req2.Body == nil {
req2.ContentLength = 0
} else if req2.GetBody != nil {
var err error
req2.Body, err = req2.GetBody()
if err != nil {
return nil, err
}
}

// Make a request to get the 401 that contains the challenge.
resp, err := dt.transport.RoundTrip(req)
if err != nil || resp.StatusCode != http.StatusUnauthorized {
Expand Down
19 changes: 19 additions & 0 deletions request_test.go
Expand Up @@ -722,6 +722,25 @@ func TestRequestDigestAuthFail(t *testing.T) {
logResponse(t, resp)
}

func TestRequestDigestAuthWithBody(t *testing.T) {
conf := defaultDigestServerConf()
ts := createDigestServer(t, nil)
defer ts.Close()

resp, err := dclr().
SetDigestAuth(conf.username, conf.password).
SetResult(&AuthSuccess{}).
SetHeader(hdrContentTypeKey, "application/json").
SetBody(map[string]interface{}{"zip_code": "00000", "city": "Los Angeles"}).
Post(ts.URL + conf.uri)

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

t.Logf("Result Success: %q", resp.Result().(*AuthSuccess))
logResponse(t, resp)
}

func TestFormData(t *testing.T) {
ts := createFormPostServer(t)
defer ts.Close()
Expand Down

0 comments on commit 102a1bf

Please sign in to comment.