Skip to content

Commit

Permalink
net/http: use strings.Builder
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
cuiweixie authored and neild committed Sep 7, 2022
1 parent 8fd2073 commit 403e5f1
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 36 deletions.
7 changes: 3 additions & 4 deletions src/net/http/cookie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package http

import (
"bytes"
"encoding/json"
"fmt"
"log"
Expand Down Expand Up @@ -151,7 +150,7 @@ var writeSetCookiesTests = []struct {

func TestWriteSetCookies(t *testing.T) {
defer log.SetOutput(os.Stderr)
var logbuf bytes.Buffer
var logbuf strings.Builder
log.SetOutput(&logbuf)

for i, tt := range writeSetCookiesTests {
Expand Down Expand Up @@ -482,7 +481,7 @@ func TestSetCookieDoubleQuotes(t *testing.T) {

func TestCookieSanitizeValue(t *testing.T) {
defer log.SetOutput(os.Stderr)
var logbuf bytes.Buffer
var logbuf strings.Builder
log.SetOutput(&logbuf)

tests := []struct {
Expand Down Expand Up @@ -514,7 +513,7 @@ func TestCookieSanitizeValue(t *testing.T) {

func TestCookieSanitizePath(t *testing.T) {
defer log.SetOutput(os.Stderr)
var logbuf bytes.Buffer
var logbuf strings.Builder
log.SetOutput(&logbuf)

tests := []struct {
Expand Down
2 changes: 1 addition & 1 deletion src/net/http/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,7 @@ func TestLinuxSendfile(t *testing.T) {
}
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.ExtraFiles = append(child.ExtraFiles, lnf)
child.Env = append([]string{"GO_WANT_HELPER_PROCESS=1"}, os.Environ()...)
Expand Down
3 changes: 2 additions & 1 deletion src/net/http/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"internal/race"
"reflect"
"runtime"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -105,7 +106,7 @@ var headerWriteTests = []struct {
}

func TestHeaderWrite(t *testing.T) {
var buf bytes.Buffer
var buf strings.Builder
for i, test := range headerWriteTests {
test.h.WriteSubset(&buf, test.exclude)
if buf.String() != test.expected {
Expand Down
2 changes: 1 addition & 1 deletion src/net/http/readrequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func TestReadRequest(t *testing.T) {
req.Body = nil
testName := fmt.Sprintf("Test %d (%q)", i, tt.Raw)
diff(t, testName, req, tt.Req)
var bout bytes.Buffer
var bout strings.Builder
if rbody != nil {
_, err := io.Copy(&bout, rbody)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions src/net/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -815,15 +815,15 @@ func TestStarRequest(t *testing.T) {
clientReq := *req
clientReq.Body = nil

var out bytes.Buffer
var out strings.Builder
if err := clientReq.Write(&out); err != nil {
t.Fatal(err)
}

if strings.Contains(out.String(), "chunked") {
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 {
t.Fatal(err)
}
Expand All @@ -835,7 +835,7 @@ func TestStarRequest(t *testing.T) {
t.Errorf("Original request doesn't match Request read back.")
t.Logf("Original: %#v", req)
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)
}
}
Expand Down Expand Up @@ -983,7 +983,7 @@ func TestMaxBytesReaderDifferentLimits(t *testing.T) {
wantErr: false,
},
10: { /* Issue 54408 */
limit: int64(1<<63-1),
limit: int64(1<<63 - 1),
lenP: len(testStr),
wantN: len(testStr),
wantErr: false,
Expand Down Expand Up @@ -1172,7 +1172,7 @@ func testMultipartFile(t *testing.T, req *Request, key, expectFilename, expectCo
if 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)
if err != nil {
t.Fatal("copying contents:", err)
Expand Down
6 changes: 3 additions & 3 deletions src/net/http/requestwrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ func TestRequestWrite(t *testing.T) {
tt.Req.Header = make(Header)
}

var braw bytes.Buffer
var braw strings.Builder
err := tt.Req.Write(&braw)
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)
Expand All @@ -649,7 +649,7 @@ func TestRequestWrite(t *testing.T) {

if tt.WantProxy != "" {
setBody()
var praw bytes.Buffer
var praw strings.Builder
err = tt.Req.WriteProxy(&praw)
if err != nil {
t.Errorf("WriteProxy #%d: %s", i, err)
Expand Down Expand Up @@ -815,7 +815,7 @@ func TestRequestWriteClosesBody(t *testing.T) {
if err != nil {
t.Fatal(err)
}
buf := new(bytes.Buffer)
buf := new(strings.Builder)
if err := req.Write(buf); err != nil {
t.Error(err)
}
Expand Down
8 changes: 4 additions & 4 deletions src/net/http/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ func TestReadResponse(t *testing.T) {
rbody := resp.Body
resp.Body = nil
diff(t, fmt.Sprintf("#%d Response", i), resp, &tt.Resp)
var bout bytes.Buffer
var bout strings.Builder
if rbody != nil {
_, err = io.Copy(&bout, rbody)
if err != nil {
Expand Down Expand Up @@ -809,7 +809,7 @@ func TestResponseStatusStutter(t *testing.T) {
ProtoMajor: 1,
ProtoMinor: 3,
}
var buf bytes.Buffer
var buf strings.Builder
r.Write(&buf)
if strings.Contains(buf.String(), "123 123") {
t.Errorf("stutter in status: %s", buf.String())
Expand All @@ -829,7 +829,7 @@ func TestResponseContentLengthShortBody(t *testing.T) {
if res.ContentLength != 123 {
t.Fatalf("Content-Length = %d; want 123", res.ContentLength)
}
var buf bytes.Buffer
var buf strings.Builder
n, err := io.Copy(&buf, res.Body)
if n != int64(len(shortBody)) {
t.Errorf("Copied %d bytes; want %d, len(%q)", n, len(shortBody), shortBody)
Expand Down Expand Up @@ -989,7 +989,7 @@ func TestResponseWritesOnlySingleConnectionClose(t *testing.T) {
t.Fatalf("ReadResponse failed %v", err)
}

var buf2 bytes.Buffer
var buf2 strings.Builder
if err = res.Write(&buf2); err != nil {
t.Fatalf("Write failed %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions src/net/http/responsewrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package http

import (
"bytes"
"io"
"strings"
"testing"
Expand Down Expand Up @@ -276,7 +275,7 @@ func TestResponseWrite(t *testing.T) {

for i := range respWriteTests {
tt := &respWriteTests[i]
var braw bytes.Buffer
var braw strings.Builder
err := tt.Resp.Write(&braw)
if err != nil {
t.Errorf("error writing #%d: %s", i, err)
Expand Down
12 changes: 6 additions & 6 deletions src/net/http/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func newHandlerTest(h Handler) handlerTest {

func (ht *handlerTest) rawResponse(req string) string {
reqb := reqBytes(req)
var output bytes.Buffer
var output strings.Builder
conn := &rwTestConn{
Reader: bytes.NewReader(reqb),
Writer: &output,
Expand Down Expand Up @@ -3742,7 +3742,7 @@ func TestAcceptMaxFds(t *testing.T) {

func TestWriteAfterHijack(t *testing.T) {
req := reqBytes("GET / HTTP/1.1\nHost: golang.org")
var buf bytes.Buffer
var buf strings.Builder
wrotec := make(chan bool, 1)
conn := &rwTestConn{
Reader: bytes.NewReader(req),
Expand Down Expand Up @@ -4544,7 +4544,7 @@ func TestNoContentLengthIfTransferEncoding(t *testing.T) {
t.Fatal(err)
}
bs := bufio.NewScanner(c)
var got bytes.Buffer
var got strings.Builder
for bs.Scan() {
if strings.TrimSpace(bs.Text()) == "" {
break
Expand Down Expand Up @@ -4633,7 +4633,7 @@ GET /should-be-ignored HTTP/1.1
Host: foo
`)
var buf bytes.Buffer
var buf strings.Builder
conn := &rwTestConn{
Reader: bytes.NewReader(req),
Writer: &buf,
Expand Down Expand Up @@ -6511,7 +6511,7 @@ func TestTimeoutHandlerSuperfluousLogs(t *testing.T) {
exitHandler <- true
}

logBuf := new(bytes.Buffer)
logBuf := new(strings.Builder)
srvLog := log.New(logBuf, "", 0)
// When expecting to timeout, we'll keep the duration short.
dur := 20 * time.Millisecond
Expand Down Expand Up @@ -6721,7 +6721,7 @@ func testQuerySemicolon(t *testing.T, query string, wantX string, allowSemicolon
}

ts := httptest.NewUnstartedServer(h)
logBuf := &bytes.Buffer{}
logBuf := &strings.Builder{}
ts.Config.ErrorLog = log.New(logBuf, "", 0)
ts.Start()
defer ts.Close()
Expand Down
18 changes: 9 additions & 9 deletions src/net/http/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2444,7 +2444,7 @@ func TestTransportCancelRequestInDial(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in -short mode")
}
var logbuf bytes.Buffer
var logbuf strings.Builder
eventLog := log.New(&logbuf, "", 0)

unblockDial := make(chan bool)
Expand Down Expand Up @@ -2931,7 +2931,7 @@ func TestTransportIgnore1xxResponses(t *testing.T) {
defer cst.close()
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 = req.WithContext(httptrace.WithClientTrace(context.Background(), &httptrace.ClientTrace{
Expand All @@ -2949,7 +2949,7 @@ func TestTransportIgnore1xxResponses(t *testing.T) {
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"
if got.String() != want {
t.Errorf(" got: %q\nwant: %q\n", got.Bytes(), want)
t.Errorf(" got: %q\nwant: %q\n", got.String(), want)
}
}

Expand Down Expand Up @@ -3015,7 +3015,7 @@ type proxyFromEnvTest struct {
}

func (t proxyFromEnvTest) String() string {
var buf bytes.Buffer
var buf strings.Builder
space := func() {
if buf.Len() > 0 {
buf.WriteByte(' ')
Expand Down Expand Up @@ -3537,7 +3537,7 @@ func TestRetryRequestsOnError(t *testing.T) {

var (
mu sync.Mutex
logbuf bytes.Buffer
logbuf strings.Builder
)
logf := func(format string, args ...any) {
mu.Lock()
Expand Down Expand Up @@ -4515,7 +4515,7 @@ func testTransportEventTrace(t *testing.T, h2 bool, noHooks bool) {
cst.tr.ExpectContinueTimeout = 1 * time.Second

var mu sync.Mutex // guards buf
var buf bytes.Buffer
var buf strings.Builder
logf := func(format string, args ...any) {
mu.Lock()
defer mu.Unlock()
Expand Down Expand Up @@ -4674,7 +4674,7 @@ func testTransportEventTrace(t *testing.T, h2 bool, noHooks bool) {

func TestTransportEventTraceTLSVerify(t *testing.T) {
var mu sync.Mutex
var buf bytes.Buffer
var buf strings.Builder
logf := func(format string, args ...any) {
mu.Lock()
defer mu.Unlock()
Expand Down Expand Up @@ -4760,7 +4760,7 @@ func TestTransportEventTraceRealDNS(t *testing.T) {
c := &Client{Transport: tr}

var mu sync.Mutex // guards buf
var buf bytes.Buffer
var buf strings.Builder
logf := func(format string, args ...any) {
mu.Lock()
defer mu.Unlock()
Expand Down Expand Up @@ -5978,7 +5978,7 @@ func TestTransportIgnores408(t *testing.T) {
// Not parallel. Relies on mutating the log package's global Output.
defer log.SetOutput(log.Writer())

var logout bytes.Buffer
var logout strings.Builder
log.SetOutput(&logout)

defer afterTest(t)
Expand Down

0 comments on commit 403e5f1

Please sign in to comment.