Skip to content

Commit 3a41bfa

Browse files
kevinburkeneild
authored andcommittedMar 11, 2024
net/http/httptest: add NewRequestWithContext
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 <[email protected]> Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent c8e4f8c commit 3a41bfa

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed
 

‎api/next/59473.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pkg net/http/httptest, func NewRequestWithContext(context.Context, string, string, io.Reader) *http.Request #59473
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The new NewRequestWithContext method creates an incoming request with
2+
a Context.

‎src/net/http/httptest/httptest.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@ package httptest
88
import (
99
"bufio"
1010
"bytes"
11+
"context"
1112
"crypto/tls"
1213
"io"
1314
"net/http"
1415
"strings"
1516
)
1617

17-
// NewRequest returns a new incoming server Request, suitable
18+
// NewRequest wraps NewRequestWithContext using context.Background.
19+
func NewRequest(method, target string, body io.Reader) *http.Request {
20+
return NewRequestWithContext(context.Background(), method, target, body)
21+
}
22+
23+
// NewRequestWithContext returns a new incoming server Request, suitable
1824
// for passing to an [http.Handler] for testing.
1925
//
2026
// The target is the RFC 7230 "request-target": it may be either a
@@ -37,14 +43,15 @@ import (
3743
//
3844
// To generate a client HTTP request instead of a server request, see
3945
// the NewRequest function in the net/http package.
40-
func NewRequest(method, target string, body io.Reader) *http.Request {
46+
func NewRequestWithContext(ctx context.Context, method, target string, body io.Reader) *http.Request {
4147
if method == "" {
4248
method = "GET"
4349
}
4450
req, err := http.ReadRequest(bufio.NewReader(strings.NewReader(method + " " + target + " HTTP/1.0\r\n\r\n")))
4551
if err != nil {
4652
panic("invalid NewRequest arguments; " + err.Error())
4753
}
54+
req = req.WithContext(ctx)
4855

4956
// HTTP/1.0 was used above to avoid needing a Host field. Change it to 1.1 here.
5057
req.Proto = "HTTP/1.1"

‎src/net/http/httptest/httptest_test.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package httptest
66

77
import (
8+
"context"
89
"crypto/tls"
910
"io"
1011
"net/http"
@@ -15,6 +16,26 @@ import (
1516
)
1617

1718
func TestNewRequest(t *testing.T) {
19+
got := NewRequest("GET", "/", nil)
20+
want := &http.Request{
21+
Method: "GET",
22+
Host: "example.com",
23+
URL: &url.URL{Path: "/"},
24+
Header: http.Header{},
25+
Proto: "HTTP/1.1",
26+
ProtoMajor: 1,
27+
ProtoMinor: 1,
28+
RemoteAddr: "192.0.2.1:1234",
29+
RequestURI: "/",
30+
}
31+
got.Body = nil // before DeepEqual
32+
want = want.WithContext(context.Background())
33+
if !reflect.DeepEqual(got, want) {
34+
t.Errorf("Request mismatch:\n got: %#v\nwant: %#v", got, want)
35+
}
36+
}
37+
38+
func TestNewRequestWithContext(t *testing.T) {
1839
for _, tt := range [...]struct {
1940
name string
2041

@@ -153,14 +174,15 @@ func TestNewRequest(t *testing.T) {
153174
},
154175
} {
155176
t.Run(tt.name, func(t *testing.T) {
156-
got := NewRequest(tt.method, tt.uri, tt.body)
177+
got := NewRequestWithContext(context.Background(), tt.method, tt.uri, tt.body)
157178
slurp, err := io.ReadAll(got.Body)
158179
if err != nil {
159180
t.Errorf("ReadAll: %v", err)
160181
}
161182
if string(slurp) != tt.wantBody {
162183
t.Errorf("Body = %q; want %q", slurp, tt.wantBody)
163184
}
185+
tt.want = tt.want.WithContext(context.Background())
164186
got.Body = nil // before DeepEqual
165187
if !reflect.DeepEqual(got.URL, tt.want.URL) {
166188
t.Errorf("Request.URL mismatch:\n got: %#v\nwant: %#v", got.URL, tt.want.URL)

0 commit comments

Comments
 (0)
Please sign in to comment.