Skip to content

Commit

Permalink
net/http/httptest: add NewRequestWithContext
Browse files Browse the repository at this point in the history
This matches the net/http API.

Updates #59473.

Change-Id: I99917cef3ed42a0b4a2b39230b492be00da8bbfd
Reviewed-on: https://go-review.googlesource.com/c/go/+/548355
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
  • Loading branch information
kevinburke authored and neild committed Mar 11, 2024
1 parent c8e4f8c commit 3a41bfa
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
1 change: 1 addition & 0 deletions api/next/59473.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pkg net/http/httptest, func NewRequestWithContext(context.Context, string, string, io.Reader) *http.Request #59473
2 changes: 2 additions & 0 deletions doc/next/6-stdlib/99-minor/net/http/httptest/59473.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The new NewRequestWithContext method creates an incoming request with
a Context.
11 changes: 9 additions & 2 deletions src/net/http/httptest/httptest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ package httptest
import (
"bufio"
"bytes"
"context"
"crypto/tls"
"io"
"net/http"
"strings"
)

// NewRequest returns a new incoming server Request, suitable
// NewRequest wraps NewRequestWithContext using context.Background.
func NewRequest(method, target string, body io.Reader) *http.Request {
return NewRequestWithContext(context.Background(), method, target, body)
}

// NewRequestWithContext returns a new incoming server Request, suitable
// for passing to an [http.Handler] for testing.
//
// The target is the RFC 7230 "request-target": it may be either a
Expand All @@ -37,14 +43,15 @@ import (
//
// To generate a client HTTP request instead of a server request, see
// the NewRequest function in the net/http package.
func NewRequest(method, target string, body io.Reader) *http.Request {
func NewRequestWithContext(ctx context.Context, method, target string, body io.Reader) *http.Request {
if method == "" {
method = "GET"
}
req, err := http.ReadRequest(bufio.NewReader(strings.NewReader(method + " " + target + " HTTP/1.0\r\n\r\n")))
if err != nil {
panic("invalid NewRequest arguments; " + err.Error())
}
req = req.WithContext(ctx)

// HTTP/1.0 was used above to avoid needing a Host field. Change it to 1.1 here.
req.Proto = "HTTP/1.1"
Expand Down
24 changes: 23 additions & 1 deletion src/net/http/httptest/httptest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package httptest

import (
"context"
"crypto/tls"
"io"
"net/http"
Expand All @@ -15,6 +16,26 @@ import (
)

func TestNewRequest(t *testing.T) {
got := NewRequest("GET", "/", nil)
want := &http.Request{
Method: "GET",
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: "/",
}
got.Body = nil // before DeepEqual
want = want.WithContext(context.Background())
if !reflect.DeepEqual(got, want) {
t.Errorf("Request mismatch:\n got: %#v\nwant: %#v", got, want)
}
}

func TestNewRequestWithContext(t *testing.T) {
for _, tt := range [...]struct {
name string

Expand Down Expand Up @@ -153,14 +174,15 @@ func TestNewRequest(t *testing.T) {
},
} {
t.Run(tt.name, func(t *testing.T) {
got := NewRequest(tt.method, tt.uri, tt.body)
got := NewRequestWithContext(context.Background(), tt.method, tt.uri, tt.body)
slurp, err := io.ReadAll(got.Body)
if err != nil {
t.Errorf("ReadAll: %v", err)
}
if string(slurp) != tt.wantBody {
t.Errorf("Body = %q; want %q", slurp, tt.wantBody)
}
tt.want = tt.want.WithContext(context.Background())
got.Body = nil // before DeepEqual
if !reflect.DeepEqual(got.URL, tt.want.URL) {
t.Errorf("Request.URL mismatch:\n got: %#v\nwant: %#v", got.URL, tt.want.URL)
Expand Down

0 comments on commit 3a41bfa

Please sign in to comment.