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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix digest auth http: ContentLength=xxx with Body length 0 #730

Merged
merged 2 commits into from
Oct 14, 2023
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
11 changes: 11 additions & 0 deletions digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@
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

Check warning on line 65 in digest.go

View check run for this annotation

Codecov / codecov/patch

digest.go#L65

Added line #L65 was not covered by tests
}
}

// 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
Original file line number Diff line number Diff line change
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