net/http: populate Response.Request when using NewFileTransport

Fixes #51562

Change-Id: Ia6fe4728b1e3e0cf3a6462be99c1044260cadf31
Reviewed-on: https://go-review.googlesource.com/c/go/+/720822
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Mark Freeman <markfreeman@google.com>
This commit is contained in:
Sean Liao 2025-11-16 00:26:27 +00:00
parent 3e0a8e7867
commit 0f32fbc631
2 changed files with 6 additions and 2 deletions

View file

@ -57,7 +57,7 @@ func (t fileTransport) RoundTrip(req *Request) (resp *Response, err error) {
// sends our *Response on, once the *Response itself has been // sends our *Response on, once the *Response itself has been
// populated (even if the body itself is still being // populated (even if the body itself is still being
// written to the res.Body, a pipe) // written to the res.Body, a pipe)
rw, resc := newPopulateResponseWriter() rw, resc := newPopulateResponseWriter(req)
go func() { go func() {
t.fh.ServeHTTP(rw, req) t.fh.ServeHTTP(rw, req)
rw.finish() rw.finish()
@ -65,7 +65,7 @@ func (t fileTransport) RoundTrip(req *Request) (resp *Response, err error) {
return <-resc, nil return <-resc, nil
} }
func newPopulateResponseWriter() (*populateResponse, <-chan *Response) { func newPopulateResponseWriter(req *Request) (*populateResponse, <-chan *Response) {
pr, pw := io.Pipe() pr, pw := io.Pipe()
rw := &populateResponse{ rw := &populateResponse{
ch: make(chan *Response), ch: make(chan *Response),
@ -76,6 +76,7 @@ func newPopulateResponseWriter() (*populateResponse, <-chan *Response) {
Header: make(Header), Header: make(Header),
Close: true, Close: true,
Body: pr, Body: pr,
Request: req,
}, },
} }
return rw, rw.ch return rw, rw.ch

View file

@ -53,6 +53,9 @@ func TestFileTransport(t *testing.T) {
if string(slurp) != "Bar" { if string(slurp) != "Bar" {
t.Errorf("for %s, got content %q, want %q", urlstr, string(slurp), "Bar") t.Errorf("for %s, got content %q, want %q", urlstr, string(slurp), "Bar")
} }
if got := res.Request.URL.String(); got != urlstr {
t.Errorf("for %s, Response.Request.URL = %s, want = %s", urlstr, got, urlstr)
}
} }
const badURL = "file://../no-exist.txt" const badURL = "file://../no-exist.txt"