Skip to content
Myr

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

4 files+20 added−21 removed

modified:debug_test.go+2 added-2 removed
3 unmodified lines
45package gin67import (8 "bytes"8 "errors"9 "fmt"10 "html/template"11 "io"12 "log"13 "os"14 "runtime"15 "strings"16 "sync"17 "testing"1819 "github.com/stretchr/testify/assert"
117 unmodified lines
137 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 lines
modified:githubapi_test.go+2 added-2 removed
3 unmodified lines
45package gin67import (8 "bytes"8 "fmt"9 "math/rand"10 "net/http"11 "net/http/httptest"12 "os"13 "strings"14 "testing"1516 "github.com/stretchr/testify/assert"17)
382 unmodified lines
400 }401}402403func 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 lines
modified:logger_test.go+7 added-7 removed
3 unmodified lines
45package gin67import (8 "bytes"8 "errors"9 "fmt"10 "net/http"11 "strings"12 "testing"13 "time"1415 "github.com/stretchr/testify/assert"
3 unmodified lines
19 SetMode(TestMode)20}2122func 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 lines
83 assert.Contains(t, buffer.String(), "/notfound")84}8586func 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 lines
147 assert.Contains(t, buffer.String(), "/notfound")148}149150func TestLoggerWithFormatter(t *testing.T) {151 buffer := new(bytes.Buffer)151 buffer := new(strings.Builder)152153 d := DefaultWriter154 DefaultWriter = buffer155 defer func() {
25 unmodified lines
181182func TestLoggerWithConfigFormatting(t *testing.T) {183 var gotParam LogFormatterParams184 var gotKeys map[string]any185 buffer := new(bytes.Buffer)185 buffer := new(strings.Builder)186187 router := New()188 router.engine.trustedCIDRs, _ = router.engine.prepareTrustedCIDRs()189
191 unmodified lines
381 assert.Equal(t, "hola!{\"error\":\"this is an error\"}", w.Body.String())382}383384func 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 lines
396 assert.Contains(t, buffer.String(), "")397}398399func 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 lines
modified:recovery_test.go+9 added-10 removed
3 unmodified lines
45package gin67import (8 "bytes"8 "fmt"9 "net"10 "net/http"11 "os"
4 unmodified lines
16 "github.com/stretchr/testify/assert"17)1819func 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 lines
48}4950// TestPanicInHandler assert that panic has been recovered.51func 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 lines
120 }121122 for errno, expectMsg := range expectMsgs {123 t.Run(expectMsg, func(t *testing.T) {125 var buf bytes.Buffer124 var buf strings.Builder125126 router := New()127 router.Use(RecoveryWithWriter(&buf))128 router.GET("/recovery", func(c *Context) {
14 unmodified lines
143 }144}145146func 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 lines
177 SetMode(TestMode)178}179180func 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 lines
212 SetMode(TestMode)213}214215func 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