@@ -26,8 +26,9 @@ import (
2626 "sync"
2727 "testing"
2828 "time"
29+ "unsafe"
2930
30- "github.com/gofiber/utils/v2"
31+ utils "github.com/gofiber/utils/v2"
3132
3233 "github.com/stretchr/testify/assert"
3334 "github.com/stretchr/testify/require"
@@ -589,7 +590,7 @@ func Test_App_Use_UnescapedPath(t *testing.T) {
589590 body , err := io .ReadAll (resp .Body )
590591 require .NoError (t , err , "app.Test(req)" )
591592 // check the param result
592- require .Equal (t , "اختبار" , app .getString (body ))
593+ require .Equal (t , "اختبار" , app .toString (body ))
593594
594595 // with lowercase letters
595596 resp , err = app .Test (httptest .NewRequest (MethodGet , "/cr%C3%A9er/%D8%A7%D8%AE%D8%AA%D8%A8%D8%A7%D8%B1" , nil ))
@@ -625,7 +626,7 @@ func Test_App_Use_CaseSensitive(t *testing.T) {
625626 body , err := io .ReadAll (resp .Body )
626627 require .NoError (t , err , "app.Test(req)" )
627628 // check the detected path result
628- require .Equal (t , "/AbC" , app .getString (body ))
629+ require .Equal (t , "/AbC" , app .toString (body ))
629630}
630631
631632func Test_App_Not_Use_StrictRouting (t * testing.T ) {
@@ -981,6 +982,56 @@ func Test_App_Config(t *testing.T) {
981982 require .True (t , app .Config ().StrictRouting )
982983}
983984
985+ func Test_App_GetString (t * testing.T ) {
986+ t .Parallel ()
987+
988+ heap := string ([]byte ("fiber" ))
989+ appMutable := New ()
990+ same := appMutable .GetString (heap )
991+ if unsafe .StringData (same ) != unsafe .StringData (heap ) { //nolint:gosec // compare pointer addresses
992+ t .Errorf ("expected original string when immutable is disabled" )
993+ }
994+
995+ appImmutable := New (Config {Immutable : true })
996+ copied := appImmutable .GetString (heap )
997+ if unsafe .StringData (copied ) == unsafe .StringData (heap ) { //nolint:gosec // compare pointer addresses
998+ t .Errorf ("expected a copy for heap-backed string when immutable is enabled" )
999+ }
1000+
1001+ literal := "fiber"
1002+ sameLit := appImmutable .GetString (literal )
1003+ if unsafe .StringData (sameLit ) != unsafe .StringData (literal ) { //nolint:gosec // compare pointer addresses
1004+ t .Errorf ("expected original literal when immutable is enabled" )
1005+ }
1006+ }
1007+
1008+ func Test_App_GetBytes (t * testing.T ) {
1009+ t .Parallel ()
1010+
1011+ b := []byte ("fiber" )
1012+ appMutable := New ()
1013+ same := appMutable .GetBytes (b )
1014+ if unsafe .SliceData (same ) != unsafe .SliceData (b ) {
1015+ t .Errorf ("expected original slice when immutable is disabled" )
1016+ }
1017+
1018+ alias := make ([]byte , 10 )
1019+ copy (alias , b )
1020+ sub := alias [:5 ]
1021+ appImmutable := New (Config {Immutable : true })
1022+ copied := appImmutable .GetBytes (sub )
1023+ if unsafe .SliceData (copied ) == unsafe .SliceData (sub ) {
1024+ t .Errorf ("expected a copy for aliased slice when immutable is enabled" )
1025+ }
1026+
1027+ full := make ([]byte , 5 )
1028+ copy (full , b )
1029+ detached := appImmutable .GetBytes (full )
1030+ if unsafe .SliceData (detached ) == unsafe .SliceData (full ) {
1031+ t .Errorf ("expected a copy even when cap==len" )
1032+ }
1033+ }
1034+
9841035func Test_App_Shutdown (t * testing.T ) {
9851036 t .Parallel ()
9861037 t .Run ("success" , func (t * testing.T ) {
0 commit comments