1
0
mirror of https://github.com/golang/go synced 2024-11-05 18:26:10 -07:00

net/http: use strings.Builder

Change-Id: I754edcf21e003a3f4037fb2c5d8d06f2cd5f2fa3
Reviewed-on: https://go-review.googlesource.com/c/go/+/428267
Auto-Submit: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Run-TryBot: xie cui <523516579@qq.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
cuiweixie 2022-09-04 18:30:29 +08:00 committed by Damien Neil
parent 8fd2073694
commit 403e5f1cb1
10 changed files with 35 additions and 36 deletions

View File

@ -5,7 +5,6 @@
package http package http
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
@ -151,7 +150,7 @@ var writeSetCookiesTests = []struct {
func TestWriteSetCookies(t *testing.T) { func TestWriteSetCookies(t *testing.T) {
defer log.SetOutput(os.Stderr) defer log.SetOutput(os.Stderr)
var logbuf bytes.Buffer var logbuf strings.Builder
log.SetOutput(&logbuf) log.SetOutput(&logbuf)
for i, tt := range writeSetCookiesTests { for i, tt := range writeSetCookiesTests {
@ -482,7 +481,7 @@ func TestSetCookieDoubleQuotes(t *testing.T) {
func TestCookieSanitizeValue(t *testing.T) { func TestCookieSanitizeValue(t *testing.T) {
defer log.SetOutput(os.Stderr) defer log.SetOutput(os.Stderr)
var logbuf bytes.Buffer var logbuf strings.Builder
log.SetOutput(&logbuf) log.SetOutput(&logbuf)
tests := []struct { tests := []struct {
@ -514,7 +513,7 @@ func TestCookieSanitizeValue(t *testing.T) {
func TestCookieSanitizePath(t *testing.T) { func TestCookieSanitizePath(t *testing.T) {
defer log.SetOutput(os.Stderr) defer log.SetOutput(os.Stderr)
var logbuf bytes.Buffer var logbuf strings.Builder
log.SetOutput(&logbuf) log.SetOutput(&logbuf)
tests := []struct { tests := []struct {

View File

@ -1273,7 +1273,7 @@ func TestLinuxSendfile(t *testing.T) {
} }
defer os.Remove(filepath) defer os.Remove(filepath)
var buf bytes.Buffer var buf strings.Builder
child := exec.Command("strace", "-f", "-q", os.Args[0], "-test.run=TestLinuxSendfileChild") child := exec.Command("strace", "-f", "-q", os.Args[0], "-test.run=TestLinuxSendfileChild")
child.ExtraFiles = append(child.ExtraFiles, lnf) child.ExtraFiles = append(child.ExtraFiles, lnf)
child.Env = append([]string{"GO_WANT_HELPER_PROCESS=1"}, os.Environ()...) child.Env = append([]string{"GO_WANT_HELPER_PROCESS=1"}, os.Environ()...)

View File

@ -9,6 +9,7 @@ import (
"internal/race" "internal/race"
"reflect" "reflect"
"runtime" "runtime"
"strings"
"testing" "testing"
"time" "time"
) )
@ -105,7 +106,7 @@ var headerWriteTests = []struct {
} }
func TestHeaderWrite(t *testing.T) { func TestHeaderWrite(t *testing.T) {
var buf bytes.Buffer var buf strings.Builder
for i, test := range headerWriteTests { for i, test := range headerWriteTests {
test.h.WriteSubset(&buf, test.exclude) test.h.WriteSubset(&buf, test.exclude)
if buf.String() != test.expected { if buf.String() != test.expected {

View File

@ -416,7 +416,7 @@ func TestReadRequest(t *testing.T) {
req.Body = nil req.Body = nil
testName := fmt.Sprintf("Test %d (%q)", i, tt.Raw) testName := fmt.Sprintf("Test %d (%q)", i, tt.Raw)
diff(t, testName, req, tt.Req) diff(t, testName, req, tt.Req)
var bout bytes.Buffer var bout strings.Builder
if rbody != nil { if rbody != nil {
_, err := io.Copy(&bout, rbody) _, err := io.Copy(&bout, rbody)
if err != nil { if err != nil {

View File

@ -815,7 +815,7 @@ func TestStarRequest(t *testing.T) {
clientReq := *req clientReq := *req
clientReq.Body = nil clientReq.Body = nil
var out bytes.Buffer var out strings.Builder
if err := clientReq.Write(&out); err != nil { if err := clientReq.Write(&out); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -823,7 +823,7 @@ func TestStarRequest(t *testing.T) {
if strings.Contains(out.String(), "chunked") { if strings.Contains(out.String(), "chunked") {
t.Error("wrote chunked request; want no body") t.Error("wrote chunked request; want no body")
} }
back, err := ReadRequest(bufio.NewReader(bytes.NewReader(out.Bytes()))) back, err := ReadRequest(bufio.NewReader(strings.NewReader(out.String())))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -835,7 +835,7 @@ func TestStarRequest(t *testing.T) {
t.Errorf("Original request doesn't match Request read back.") t.Errorf("Original request doesn't match Request read back.")
t.Logf("Original: %#v", req) t.Logf("Original: %#v", req)
t.Logf("Original.URL: %#v", req.URL) t.Logf("Original.URL: %#v", req.URL)
t.Logf("Wrote: %s", out.Bytes()) t.Logf("Wrote: %s", out.String())
t.Logf("Read back (doesn't match Original): %#v", back) t.Logf("Read back (doesn't match Original): %#v", back)
} }
} }
@ -983,7 +983,7 @@ func TestMaxBytesReaderDifferentLimits(t *testing.T) {
wantErr: false, wantErr: false,
}, },
10: { /* Issue 54408 */ 10: { /* Issue 54408 */
limit: int64(1<<63-1), limit: int64(1<<63 - 1),
lenP: len(testStr), lenP: len(testStr),
wantN: len(testStr), wantN: len(testStr),
wantErr: false, wantErr: false,
@ -1172,7 +1172,7 @@ func testMultipartFile(t *testing.T, req *Request, key, expectFilename, expectCo
if fh.Filename != expectFilename { if fh.Filename != expectFilename {
t.Errorf("filename = %q, want %q", fh.Filename, expectFilename) t.Errorf("filename = %q, want %q", fh.Filename, expectFilename)
} }
var b bytes.Buffer var b strings.Builder
_, err = io.Copy(&b, f) _, err = io.Copy(&b, f)
if err != nil { if err != nil {
t.Fatal("copying contents:", err) t.Fatal("copying contents:", err)

View File

@ -629,7 +629,7 @@ func TestRequestWrite(t *testing.T) {
tt.Req.Header = make(Header) tt.Req.Header = make(Header)
} }
var braw bytes.Buffer var braw strings.Builder
err := tt.Req.Write(&braw) err := tt.Req.Write(&braw)
if g, e := fmt.Sprintf("%v", err), fmt.Sprintf("%v", tt.WantError); g != e { if g, e := fmt.Sprintf("%v", err), fmt.Sprintf("%v", tt.WantError); g != e {
t.Errorf("writing #%d, err = %q, want %q", i, g, e) t.Errorf("writing #%d, err = %q, want %q", i, g, e)
@ -649,7 +649,7 @@ func TestRequestWrite(t *testing.T) {
if tt.WantProxy != "" { if tt.WantProxy != "" {
setBody() setBody()
var praw bytes.Buffer var praw strings.Builder
err = tt.Req.WriteProxy(&praw) err = tt.Req.WriteProxy(&praw)
if err != nil { if err != nil {
t.Errorf("WriteProxy #%d: %s", i, err) t.Errorf("WriteProxy #%d: %s", i, err)
@ -815,7 +815,7 @@ func TestRequestWriteClosesBody(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
buf := new(bytes.Buffer) buf := new(strings.Builder)
if err := req.Write(buf); err != nil { if err := req.Write(buf); err != nil {
t.Error(err) t.Error(err)
} }

View File

@ -596,7 +596,7 @@ func TestReadResponse(t *testing.T) {
rbody := resp.Body rbody := resp.Body
resp.Body = nil resp.Body = nil
diff(t, fmt.Sprintf("#%d Response", i), resp, &tt.Resp) diff(t, fmt.Sprintf("#%d Response", i), resp, &tt.Resp)
var bout bytes.Buffer var bout strings.Builder
if rbody != nil { if rbody != nil {
_, err = io.Copy(&bout, rbody) _, err = io.Copy(&bout, rbody)
if err != nil { if err != nil {
@ -809,7 +809,7 @@ func TestResponseStatusStutter(t *testing.T) {
ProtoMajor: 1, ProtoMajor: 1,
ProtoMinor: 3, ProtoMinor: 3,
} }
var buf bytes.Buffer var buf strings.Builder
r.Write(&buf) r.Write(&buf)
if strings.Contains(buf.String(), "123 123") { if strings.Contains(buf.String(), "123 123") {
t.Errorf("stutter in status: %s", buf.String()) t.Errorf("stutter in status: %s", buf.String())
@ -829,7 +829,7 @@ func TestResponseContentLengthShortBody(t *testing.T) {
if res.ContentLength != 123 { if res.ContentLength != 123 {
t.Fatalf("Content-Length = %d; want 123", res.ContentLength) t.Fatalf("Content-Length = %d; want 123", res.ContentLength)
} }
var buf bytes.Buffer var buf strings.Builder
n, err := io.Copy(&buf, res.Body) n, err := io.Copy(&buf, res.Body)
if n != int64(len(shortBody)) { if n != int64(len(shortBody)) {
t.Errorf("Copied %d bytes; want %d, len(%q)", n, len(shortBody), shortBody) t.Errorf("Copied %d bytes; want %d, len(%q)", n, len(shortBody), shortBody)
@ -989,7 +989,7 @@ func TestResponseWritesOnlySingleConnectionClose(t *testing.T) {
t.Fatalf("ReadResponse failed %v", err) t.Fatalf("ReadResponse failed %v", err)
} }
var buf2 bytes.Buffer var buf2 strings.Builder
if err = res.Write(&buf2); err != nil { if err = res.Write(&buf2); err != nil {
t.Fatalf("Write failed %v", err) t.Fatalf("Write failed %v", err)
} }

View File

@ -5,7 +5,6 @@
package http package http
import ( import (
"bytes"
"io" "io"
"strings" "strings"
"testing" "testing"
@ -276,7 +275,7 @@ func TestResponseWrite(t *testing.T) {
for i := range respWriteTests { for i := range respWriteTests {
tt := &respWriteTests[i] tt := &respWriteTests[i]
var braw bytes.Buffer var braw strings.Builder
err := tt.Resp.Write(&braw) err := tt.Resp.Write(&braw)
if err != nil { if err != nil {
t.Errorf("error writing #%d: %s", i, err) t.Errorf("error writing #%d: %s", i, err)

View File

@ -148,7 +148,7 @@ func newHandlerTest(h Handler) handlerTest {
func (ht *handlerTest) rawResponse(req string) string { func (ht *handlerTest) rawResponse(req string) string {
reqb := reqBytes(req) reqb := reqBytes(req)
var output bytes.Buffer var output strings.Builder
conn := &rwTestConn{ conn := &rwTestConn{
Reader: bytes.NewReader(reqb), Reader: bytes.NewReader(reqb),
Writer: &output, Writer: &output,
@ -3742,7 +3742,7 @@ func TestAcceptMaxFds(t *testing.T) {
func TestWriteAfterHijack(t *testing.T) { func TestWriteAfterHijack(t *testing.T) {
req := reqBytes("GET / HTTP/1.1\nHost: golang.org") req := reqBytes("GET / HTTP/1.1\nHost: golang.org")
var buf bytes.Buffer var buf strings.Builder
wrotec := make(chan bool, 1) wrotec := make(chan bool, 1)
conn := &rwTestConn{ conn := &rwTestConn{
Reader: bytes.NewReader(req), Reader: bytes.NewReader(req),
@ -4544,7 +4544,7 @@ func TestNoContentLengthIfTransferEncoding(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
bs := bufio.NewScanner(c) bs := bufio.NewScanner(c)
var got bytes.Buffer var got strings.Builder
for bs.Scan() { for bs.Scan() {
if strings.TrimSpace(bs.Text()) == "" { if strings.TrimSpace(bs.Text()) == "" {
break break
@ -4633,7 +4633,7 @@ GET /should-be-ignored HTTP/1.1
Host: foo Host: foo
`) `)
var buf bytes.Buffer var buf strings.Builder
conn := &rwTestConn{ conn := &rwTestConn{
Reader: bytes.NewReader(req), Reader: bytes.NewReader(req),
Writer: &buf, Writer: &buf,
@ -6511,7 +6511,7 @@ func TestTimeoutHandlerSuperfluousLogs(t *testing.T) {
exitHandler <- true exitHandler <- true
} }
logBuf := new(bytes.Buffer) logBuf := new(strings.Builder)
srvLog := log.New(logBuf, "", 0) srvLog := log.New(logBuf, "", 0)
// When expecting to timeout, we'll keep the duration short. // When expecting to timeout, we'll keep the duration short.
dur := 20 * time.Millisecond dur := 20 * time.Millisecond
@ -6721,7 +6721,7 @@ func testQuerySemicolon(t *testing.T, query string, wantX string, allowSemicolon
} }
ts := httptest.NewUnstartedServer(h) ts := httptest.NewUnstartedServer(h)
logBuf := &bytes.Buffer{} logBuf := &strings.Builder{}
ts.Config.ErrorLog = log.New(logBuf, "", 0) ts.Config.ErrorLog = log.New(logBuf, "", 0)
ts.Start() ts.Start()
defer ts.Close() defer ts.Close()

View File

@ -2444,7 +2444,7 @@ func TestTransportCancelRequestInDial(t *testing.T) {
if testing.Short() { if testing.Short() {
t.Skip("skipping test in -short mode") t.Skip("skipping test in -short mode")
} }
var logbuf bytes.Buffer var logbuf strings.Builder
eventLog := log.New(&logbuf, "", 0) eventLog := log.New(&logbuf, "", 0)
unblockDial := make(chan bool) unblockDial := make(chan bool)
@ -2931,7 +2931,7 @@ func TestTransportIgnore1xxResponses(t *testing.T) {
defer cst.close() defer cst.close()
cst.tr.DisableKeepAlives = true // prevent log spam; our test server is hanging up anyway cst.tr.DisableKeepAlives = true // prevent log spam; our test server is hanging up anyway
var got bytes.Buffer var got strings.Builder
req, _ := NewRequest("GET", cst.ts.URL, nil) req, _ := NewRequest("GET", cst.ts.URL, nil)
req = req.WithContext(httptrace.WithClientTrace(context.Background(), &httptrace.ClientTrace{ req = req.WithContext(httptrace.WithClientTrace(context.Background(), &httptrace.ClientTrace{
@ -2949,7 +2949,7 @@ func TestTransportIgnore1xxResponses(t *testing.T) {
res.Write(&got) res.Write(&got)
want := "1xx: code=123, header=map[Foo:[bar]]\nHTTP/1.1 200 OK\r\nContent-Length: 5\r\nBar: baz\r\n\r\nHello" want := "1xx: code=123, header=map[Foo:[bar]]\nHTTP/1.1 200 OK\r\nContent-Length: 5\r\nBar: baz\r\n\r\nHello"
if got.String() != want { if got.String() != want {
t.Errorf(" got: %q\nwant: %q\n", got.Bytes(), want) t.Errorf(" got: %q\nwant: %q\n", got.String(), want)
} }
} }
@ -3015,7 +3015,7 @@ type proxyFromEnvTest struct {
} }
func (t proxyFromEnvTest) String() string { func (t proxyFromEnvTest) String() string {
var buf bytes.Buffer var buf strings.Builder
space := func() { space := func() {
if buf.Len() > 0 { if buf.Len() > 0 {
buf.WriteByte(' ') buf.WriteByte(' ')
@ -3537,7 +3537,7 @@ func TestRetryRequestsOnError(t *testing.T) {
var ( var (
mu sync.Mutex mu sync.Mutex
logbuf bytes.Buffer logbuf strings.Builder
) )
logf := func(format string, args ...any) { logf := func(format string, args ...any) {
mu.Lock() mu.Lock()
@ -4515,7 +4515,7 @@ func testTransportEventTrace(t *testing.T, h2 bool, noHooks bool) {
cst.tr.ExpectContinueTimeout = 1 * time.Second cst.tr.ExpectContinueTimeout = 1 * time.Second
var mu sync.Mutex // guards buf var mu sync.Mutex // guards buf
var buf bytes.Buffer var buf strings.Builder
logf := func(format string, args ...any) { logf := func(format string, args ...any) {
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
@ -4674,7 +4674,7 @@ func testTransportEventTrace(t *testing.T, h2 bool, noHooks bool) {
func TestTransportEventTraceTLSVerify(t *testing.T) { func TestTransportEventTraceTLSVerify(t *testing.T) {
var mu sync.Mutex var mu sync.Mutex
var buf bytes.Buffer var buf strings.Builder
logf := func(format string, args ...any) { logf := func(format string, args ...any) {
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
@ -4760,7 +4760,7 @@ func TestTransportEventTraceRealDNS(t *testing.T) {
c := &Client{Transport: tr} c := &Client{Transport: tr}
var mu sync.Mutex // guards buf var mu sync.Mutex // guards buf
var buf bytes.Buffer var buf strings.Builder
logf := func(format string, args ...any) { logf := func(format string, args ...any) {
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
@ -5978,7 +5978,7 @@ func TestTransportIgnores408(t *testing.T) {
// Not parallel. Relies on mutating the log package's global Output. // Not parallel. Relies on mutating the log package's global Output.
defer log.SetOutput(log.Writer()) defer log.SetOutput(log.Writer())
var logout bytes.Buffer var logout strings.Builder
log.SetOutput(&logout) log.SetOutput(&logout)
defer afterTest(t) defer afterTest(t)