Replace bytes.Buffer with strings.Builder where appropriate (#3347)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
To build strings more efficiently, use strings.Builder instead.
authored by hopehook · committed by GitHub · parent 8cd11c8 · Browse files · 4 files·+20 added−21 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+20 added−21 removed
- other:+20 added−21 removed
Provenance
- Commit
b2d4185- Tree
742cb8f- Author
hopehook <hopehook@qq.com>- Committer
GitHub <noreply@github.com>- Parent
8cd11c8
4 files changed+20 added−21 removed
modified:debug_test.go+2 added-2 removed
3 unmodified lines4 5 package gin6 7 import (8- "bytes"8 "errors"9 "fmt"10 "html/template"11 "io"12 "log"13 "os"14 "runtime"15+ "strings"16 "sync"17 "testing"18 19 "github.com/stretchr/testify/assert"117 unmodified lines137 out := make(chan string)138 wg := new(sync.WaitGroup)139 wg.Add(1)140 go func() {141- var buf bytes.Buffer141+ var buf strings.Builder142 wg.Done()143 _, err := io.Copy(&buf, reader)144 assert.NoError(t, err)145 out <- buf.String()21 unmodified linesmodified:githubapi_test.go+2 added-2 removed
3 unmodified lines4 5 package gin6 7 import (8- "bytes"8 "fmt"9 "math/rand"10 "net/http"11 "net/http/httptest"12 "os"13+ "strings"14 "testing"15 16 "github.com/stretchr/testify/assert"17 )382 unmodified lines400 }401 }402 403 func exampleFromPath(path string) (string, Params) {404- output := new(bytes.Buffer)404+ output := new(strings.Builder)405 params := make(Params, 0, 6)406 start := -1407 for i, c := range path {408 if c == ':' {66 unmodified linesmodified:logger_test.go+7 added-7 removed
3 unmodified lines4 5 package gin6 7 import (8- "bytes"8 "errors"9 "fmt"10 "net/http"11+ "strings"12 "testing"13 "time"14 15 "github.com/stretchr/testify/assert"3 unmodified lines19 SetMode(TestMode)20 }21 22 func TestLogger(t *testing.T) {23- buffer := new(bytes.Buffer)23+ buffer := new(strings.Builder)24 router := New()25 router.Use(LoggerWithWriter(buffer))26 router.GET("/example", func(c *Context) {})27 router.POST("/example", func(c *Context) {})55 unmodified lines83 assert.Contains(t, buffer.String(), "/notfound")84 }85 86 func TestLoggerWithConfig(t *testing.T) {87- buffer := new(bytes.Buffer)87+ buffer := new(strings.Builder)88 router := New()89 router.Use(LoggerWithConfig(LoggerConfig{Output: buffer}))90 router.GET("/example", func(c *Context) {})91 router.POST("/example", func(c *Context) {})55 unmodified lines147 assert.Contains(t, buffer.String(), "/notfound")148 }149 150 func TestLoggerWithFormatter(t *testing.T) {151- buffer := new(bytes.Buffer)151+ buffer := new(strings.Builder)152 153 d := DefaultWriter154 DefaultWriter = buffer155 defer func() {25 unmodified lines181 182 func TestLoggerWithConfigFormatting(t *testing.T) {183 var gotParam LogFormatterParams184 var gotKeys map[string]any185- buffer := new(bytes.Buffer)185+ buffer := new(strings.Builder)186 187 router := New()188 router.engine.trustedCIDRs, _ = router.engine.prepareTrustedCIDRs()189 191 unmodified lines381 assert.Equal(t, "hola!{\"error\":\"this is an error\"}", w.Body.String())382 }383 384 func TestLoggerWithWriterSkippingPaths(t *testing.T) {385- buffer := new(bytes.Buffer)385+ buffer := new(strings.Builder)386 router := New()387 router.Use(LoggerWithWriter(buffer, "/skipped"))388 router.GET("/logged", func(c *Context) {})389 router.GET("/skipped", func(c *Context) {})6 unmodified lines396 assert.Contains(t, buffer.String(), "")397 }398 399 func TestLoggerWithConfigSkippingPaths(t *testing.T) {400- buffer := new(bytes.Buffer)400+ buffer := new(strings.Builder)401 router := New()402 router.Use(LoggerWithConfig(LoggerConfig{403 Output: buffer,404 SkipPaths: []string{"/skipped"},31 unmodified linesmodified:recovery_test.go+9 added-10 removed
3 unmodified lines4 5 package gin6 7 import (8- "bytes"8 "fmt"9 "net"10 "net/http"11 "os"4 unmodified lines16 "github.com/stretchr/testify/assert"17 )18 19 func TestPanicClean(t *testing.T) {21- buffer := new(bytes.Buffer)20+ buffer := new(strings.Builder)21 router := New()22 password := "my-super-secret-password"23 router.Use(RecoveryWithWriter(buffer))24 router.GET("/recovery", func(c *Context) {23 unmodified lines48 }49 50 // TestPanicInHandler assert that panic has been recovered.51 func TestPanicInHandler(t *testing.T) {53- buffer := new(bytes.Buffer)52+ buffer := new(strings.Builder)53 router := New()54 router.Use(RecoveryWithWriter(buffer))55 router.GET("/recovery", func(_ *Context) {56 panic("Oupps, Houston, we have a problem")63 unmodified lines120 }121 122 for errno, expectMsg := range expectMsgs {123 t.Run(expectMsg, func(t *testing.T) {125- var buf bytes.Buffer124+ var buf strings.Builder125 126 router := New()127 router.Use(RecoveryWithWriter(&buf))128 router.GET("/recovery", func(c *Context) {14 unmodified lines143 }144 }145 146 func TestCustomRecoveryWithWriter(t *testing.T) {148- errBuffer := new(bytes.Buffer)149- buffer := new(bytes.Buffer)147+ errBuffer := new(strings.Builder)148+ buffer := new(strings.Builder)149 router := New()150 handleRecovery := func(c *Context, err any) {151 errBuffer.WriteString(err.(string))152 c.AbortWithStatus(http.StatusBadRequest)24 unmodified lines177 SetMode(TestMode)178 }179 180 func TestCustomRecovery(t *testing.T) {182- errBuffer := new(bytes.Buffer)183- buffer := new(bytes.Buffer)181+ errBuffer := new(strings.Builder)182+ buffer := new(strings.Builder)183 router := New()184 DefaultErrorWriter = buffer185 handleRecovery := func(c *Context, err any) {186 errBuffer.WriteString(err.(string))25 unmodified lines212 SetMode(TestMode)213 }214 215 func TestRecoveryWithWriterWithCustomRecovery(t *testing.T) {217- errBuffer := new(bytes.Buffer)218- buffer := new(bytes.Buffer)216+ errBuffer := new(strings.Builder)217+ buffer := new(strings.Builder)218 router := New()219 DefaultErrorWriter = buffer220 handleRecovery := func(c *Context, err any) {221 errBuffer.WriteString(err.(string))27 unmodified lines