set Content-Length on PATCH requests simliar to POST and PUT

This commit is contained in:
Segflow 2020-08-23 12:27:46 +01:00
parent 8acbe4c0b3
commit 12a3903f2b
2 changed files with 21 additions and 1 deletions

View File

@ -588,6 +588,26 @@ var reqWriteTests = []reqWriteTest{
},
WantError: errors.New("net/http: can't write control character in Request.URL"),
},
26: { // Request with nil body and PATCH method. Issue #40978
Req: Request{
Method: "PATCH",
URL: mustParseURL("/"),
Host: "example.com",
ProtoMajor: 1,
ProtoMinor: 1,
ContentLength: 0, // as if unset by user
},
Body: nil,
WantWrite: "PATCH / HTTP/1.1\r\n" +
"Host: example.com\r\n" +
"User-Agent: Go-http-client/1.1\r\n" +
"Content-Length: 0\r\n\r\n",
WantProxy: "PATCH / HTTP/1.1\r\n" +
"Host: example.com\r\n" +
"User-Agent: Go-http-client/1.1\r\n" +
"Content-Length: 0\r\n\r\n",
},
}
func TestRequestWrite(t *testing.T) {

View File

@ -258,7 +258,7 @@ func (t *transferWriter) shouldSendContentLength() bool {
return false
}
// Many servers expect a Content-Length for these methods
if t.Method == "POST" || t.Method == "PUT" {
if t.Method == "POST" || t.Method == "PUT" || t.Method == "PATCH" {
return true
}
if t.ContentLength == 0 && isIdentity(t.TransferEncoding) {