//
// An empty method means "GET".
//
-// The provided body may be nil. If the body is of type *bytes.Reader,
-// *strings.Reader, or *bytes.Buffer, the Request.ContentLength is
-// set.
+// The provided body may be nil. If the body is of type [bytes.Reader],
+// [strings.Reader], [bytes.Buffer], or the value [http.NoBody],
+// the Request.ContentLength is set.
//
// NewRequest panics on error for ease of use in testing, where a
// panic is acceptable.
default:
req.ContentLength = -1
}
+ if body == http.NoBody {
+ req.ContentLength = 0
+ }
if rc, ok := body.(io.ReadCloser); ok {
req.Body = rc
} else {
wantBody: "foo",
},
+ {
+ name: "Post with NoBody",
+ method: "POST",
+ uri: "/",
+ body: http.NoBody,
+ want: &http.Request{
+ Method: "POST",
+ Host: "example.com",
+ URL: &url.URL{Path: "/"},
+ Header: http.Header{},
+ Proto: "HTTP/1.1",
+ ProtoMajor: 1,
+ ProtoMinor: 1,
+ RemoteAddr: "192.0.2.1:1234",
+ RequestURI: "/",
+ },
+ },
+
{
name: "OPTIONS *",
method: "OPTIONS",