| 1 | package utilsscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/ |
| 2 | |
| 3 | import ( |
| 4 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/" |
| 5 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 6 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 7 | |
| 8 | "github.com/stretchr/testify/requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/" |
| 9 | ) |
| 10 | |
| 11 | func jsonStringInputsscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/jsonStringInputs().() []string { |
| 12 | return []string{ |
| 13 | "", |
| 14 | "plain ascii value", |
| 15 | "with \"quotes\" and \\backslashes\\", |
| 16 | "tabs\tnewlines\nreturns\r", |
| 17 | "controls \x00\x01\x1f\x7f", |
| 18 | "html <b>bold</b> & more", |
| 19 | "café naïve 日本語", |
| 20 | "line\u2028and\u2029separators", |
| 21 | "invalid \xff\xfe utf8 \xc3(", |
| 22 | "trailing continuation \xc3", |
| 23 | stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Repeatscip-go gomod github.com/golang/go/src go1.25.0 strings/Repeat().("0123456789abcdef", 8), |
| 24 | stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Repeatscip-go gomod github.com/golang/go/src go1.25.0 strings/Repeat().("x", 7) + "\"" + stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Repeatscip-go gomod github.com/golang/go/src go1.25.0 strings/Repeat().("y", 9), |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func Test_AppendJSONStringscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/Test_AppendJSONString().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 29 | t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().() |
| 30 | for _, in := range jsonStringInputsscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/jsonStringInputs().() { |
| 31 | want, err := jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Marshal().(in) |
| 32 | requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/NoError().(t, err) |
| 33 | requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, string(want), string(AppendJSONStringscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/AppendJSONString().(nil, in)), "input %q", in) |
| 34 | requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, string(want), string(AppendJSONStringscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/AppendJSONString().(nil, []byte(in))), "input %q", in) |
| 35 | } |
| 36 | |
| 37 | // Every single byte value agrees with encoding/json. |
| 38 | for i := range 256 { |
| 39 | in := string([]byte{byte(i)}) |
| 40 | want, err := jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Marshal().(in) |
| 41 | requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/NoError().(t, err) |
| 42 | requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, string(want), string(AppendJSONStringscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/AppendJSONString().(nil, in)), "byte %#x", i) |
| 43 | } |
| 44 | |
| 45 | // Appending must preserve existing dst content. |
| 46 | got := AppendJSONStringscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/AppendJSONString().([]byte(`{"msg":`), "hi \"there\"") |
| 47 | requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, `{"msg":"hi \"there\""`, string(got)) //nolint:testifylint // byte-exact comparison is the contract; JSONEq would hide encoding differences |
| 48 | } |
| 49 | |
| 50 | func Benchmark_AppendJSONStringscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/Benchmark_AppendJSONString().(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) { |
| 51 | inputs := []struct { |
| 52 | name string |
| 53 | value string |
| 54 | }{ |
| 55 | {"clean-16B", "GET /index.html"}, |
| 56 | {"clean-64B", stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Repeatscip-go gomod github.com/golang/go/src go1.25.0 strings/Repeat().("clean-log", 7) + "x"}, |
| 57 | {"escaped-64B", stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Repeatscip-go gomod github.com/golang/go/src go1.25.0 strings/Repeat().(`says "hi" `, 6) + "done"}, |
| 58 | {"unicode-64B", stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Repeatscip-go gomod github.com/golang/go/src go1.25.0 strings/Repeat().("café 日本 ", 6) + "end"}, |
| 59 | } |
| 60 | for _, input := range inputs { |
| 61 | dst := make([]byte, 0, 3*len(input.value)) |
| 62 | b.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Run().(input.name+"/fiber", func(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) { |
| 63 | b.ReportAllocsscip-go gomod github.com/golang/go/src go1.25.0 testing/B#ReportAllocs().() |
| 64 | for b.Loopscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Loop().() { |
| 65 | AppendJSONStringscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/AppendJSONString().(dst, input.value) |
| 66 | } |
| 67 | }) |
| 68 | b.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Run().(input.name+"/default", func(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) { |
| 69 | b.ReportAllocsscip-go gomod github.com/golang/go/src go1.25.0 testing/B#ReportAllocs().() |
| 70 | for b.Loopscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Loop().() { |
| 71 | if _, err := jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Marshal().(input.value); err != nil { |
| 72 | b.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 73 | } |
| 74 | } |
| 75 | }) |
| 76 | } |
| 77 | } |